powershell - 如何检查 powershell 脚本是否从托管 API 签名?

标签 powershell

我想使用 .net Powershell SDK 执行 powershell。我有这个工作正常。 在执行它之前,我想检查脚本是否已由我的代码签名证书签名——这很容易在 powershell 内部使用 Get-AuthenticodeSignature 完成,但我想在选择执行此脚本之前在代码中执行此操作。

解决方法:

        Runspace runSpace = RunspaceFactory.CreateRunspace();
        runSpace.Open();

        Pipeline shell = runSpace.CreatePipeline();
        shell.Commands.AddScript(String.Format("Get-AuthenticodeSignature '{0}'", Filename));

        Signature sig = (shell.Invoke()[0]).BaseObject as Signature;
        bool isValid = sig.Status == SignatureStatus.Valid;

最佳答案

我能想到的最简单的方法是仍然使用 powershell,但是在托管代码中:

using System.Management.Automation;

void Foo(string path) {
   PowerShell shell = PowerShell.Create();
   shell.AddScript(String.Format("Get-AuthenticodeSignature {0}", path));

   Signature sig = shell.Invoke()[0] as Signature; // returns collection
   bool isValid = sig.Valid;
}

(凭内存,所以语法上可能不完全正确)

关于powershell - 如何检查 powershell 脚本是否从托管 API 签名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2746186/

相关文章:

windows - Powershell 快捷方式创建不起作用

powershell - 变量内每行的Powershell

powershell - 如何比较文件中的两个顺序字符串

Powershell 转换为 F# 可区分联合

node.js - 消息 "the term ' ng' 未被识别为 cmdlet 的名称”

linux - 在 PowerShell 中使用 New-SshSession(使用私钥)远程登录 Linux 失败并显示 "Invalid private key file"

powershell - 如何循环遍历哈希表中的数组 - 根据从 CSV 文件读取的值传递参数

c# - 序列化 WPF DataTemplates 和 {Binding Expressions}(来自 PowerShell?)

c# - 错误包管理器控制台

arrays - 平均阵列Powershell的一部分