简体   繁体   中英

How can I verify signature of a Powershell .ps1 script using C#?

I have some signed .ps1 script, I need to verify they are properly signed from a C# project, is there any algorithm or library to do this?

Thanks!

You could host the PowerShell engine to check this using the Get-AuthenticodeSignature cmdlet eg:

using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

private bool VerifyPowerShellScriptSignature()
{
    using (var runspaceInvoke = new RunspaceInvoke())
    {
        string path = "C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\" +
                      "Modules\\PSDiagnostics\\PSDiagnostics.psm1";
        Collection<PSObject> results =
            runspaceInvoke.Invoke("Get-AuthenticodeSignature " + path);
        Signature signature = results[0].BaseObject as Signature;
        return signature == null ? false : 
                                  (signature.Status == SignatureStatus.Valid);
    }
}

如果Get-AuthenticodeSignature cmdlet正确验证(即,如果无效,它将报告错误)并且目标系统上有PSH,则可以在C#应用程序中运行PSH。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM