简体   繁体   English

使用 C# /.NetCore 签署 Powershellscript

[英]Sign a Powershellscript with C# / .NetCore

for my complex system i am looking for a way to sign a powershellscript not with powershell but with C# /.NetCore对于我的复杂系统,我正在寻找一种方法来签署 powershellscript 不是用powershell而是用C# /.NetCore

here the Powershell version:这里是Powershell版本:

$cert=Get-ChildItem -Path Cert:\CurrentUser\My -CodeSigningCert
Set-AuthenticodeSignature -FilePath PsTestInternet2.ps1 -Certificate $cert

My current solution starts a Powershell console with我当前的解决方案使用Powershell 控制台启动

process.start

Why do I want to sign with C# and not Powershell?为什么我要签 C# 而不是 Powershell?

  1. my system is an intranet service我的系统是 Intranet 服务
  2. the user can compile a script from snippets in the app and select additional installation files用户可以从应用程序中的片段和 select 附加安装文件编译脚本
  3. now the script is signed and compiled with the installation files to an exe (C# console project - compiled at runtime with dotnet)现在脚本已使用安装文件进行签名并编译为 exe(C# 控制台项目 - 在运行时使用 dotnet 编译)
  4. this exe can be controlled by the user (the exe has switches to examine the update)此 exe 可由用户控制(该 exe 具有检查更新的开关)
  5. now this exe (update) is distributed via wsus and arrives on user computers and installs the installation files with the script.现在这个 exe(更新)通过 wsus 分发并到达用户计算机并使用脚本安装安装文件。

All this is a nice system, but it would be even nicer if I would not have to save the script first to sign it with所有这些都是一个不错的系统,但如果我不必先保存脚本来签名它会更好

process-start => powershell进程启动 => powershell

As you can read above I am not looking for someone to do my homework, but someone who is experienced enough to tell me how to sign the script with C# without using a powershellpipe.正如您在上面所读到的,我不是在找人来做我的作业,而是有足够经验告诉我如何在不使用 powershellpipe 的情况下使用 C# 签署脚本的人。

Thanks a lot非常感谢
Best regards此致

Now that I've invested more time and figured out the underlying technology, I found the solution on GitHub in Microsoft's code.既然投入了更多的时间,摸清了底层技术,在微软的代码中找到了GitHub上的解决方案。

Up to now it was also claimed here on stackoverflow that it is not possible.到目前为止,stackoverflow 上还声称这是不可能的。

Here is, how they do it https://github.com/PowerShell/PowerShell/blob/d8f8f0a8bcbadb357f9eaafbb797278ebe07d7cc/src/System.Management.Automation/security/Authenticode.cs这是,他们是如何做到的

In this file you can find the signaturehelper class with the following function:在此文件中,您可以找到带有以下 function 的签名助手 class:

internal static Signature SignFile(SigningOption option,
                                           string fileName,
                                           X509Certificate2 certificate,
                                           string timeStampServerUrl,
                                           string hashAlgorithm)

Sorry if anyone minds, I found the solution so quickly.对不起,如果有人介意,我很快就找到了解决方案。 At least now I've shared it.至少现在我已经分享过了。

Edit:编辑:

Because of the underlying function, the file must still be saved first.因为底层function,还是要先保存文件。 So the advantage is marginal.所以优势是微乎其微的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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