如何轻松签署 PowerShell 脚本?

Vil*_*nen 16 windows security script powershell code-signing

我更喜欢在 PowerShell 中使用 AllSigned 执行策略,但自签名我的脚本似乎需要数百兆字节的下载和安装,并且签名过程似乎很麻烦。

是否有比文档中描述的更简单的方法来签署 PowerShell 脚本?

Ric*_*ard 7

要进行签名,您可以使用Set-AuthenticodeSignaturecmdlet。当然,这需要证书。如果您拥有能够创建代码签名证书的证书颁发机构(不太可能)。此外,还有各种工具可以创建自签名证书。

您将证书安装在您的证书存储中(在 Windows 资源管理器中打开.cer.pfx文件来执行此操作),然后将其传递给Set-AuthenticodeSignaturecert:提供者/驱动器可以访问您存储中的证书)。

help about_signing
Run Code Online (Sandbox Code Playgroud)

或该帮助主题的在线版本以获取详细信息(包括使用 Windows SDK 工具[1]创建自签名证书)。

[1]我认为这是您所指的大下载:您可以只安装所需的位,或使用其他工具(OpenSSL 包括证书生成)。为此,获取 SDK 是一项一次性活动。


Bra*_*tch 6

我使用这个 PowerShell 脚本:

## sign-script.ps1
## Sign a powershell script with a Thawte certificate and 
## timestamp the signature
##
## usage: ./sign-script.ps1 c:\foo.ps1 

param([string] $file=$(throw “Please specify a script filepath.”)) 

$certFriendlyName = "Thawte Code Signing"
$cert = gci cert:\CurrentUser\My -codesigning | where -Filter 
  {$_.FriendlyName -eq $certFriendlyName}

# https://www.thawte.com/ssl-digital-certificates/technical-  
#   support/code/msauth.html#timestampau
# We thank VeriSign for allowing public use of their timestamping server.
# Add the following to the signcode command line: 
# -t http://timestamp.verisign.com/scripts/timstamp.dll 
$timeStampURL = "http://timestamp.verisign.com/scripts/timstamp.dll"

if($cert) {
    Set-AuthenticodeSignature -filepath $file -cert $cert -IncludeChain All -   
      TimeStampServer $timeStampURL
}
else {
    throw "Did not find certificate with friendly name of `"$certFriendlyName`""
}
Run Code Online (Sandbox Code Playgroud)


小智 6

获得证书后,我想在我的用户帐户上使用导入。这个注册码在上下文菜单中给了我一个选项Sign(我使用的是 Windows 7)。如果要签名的证书以不同方式存储,只需在最后更改PowerShell命令。

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Classes\Microsoft.PowerShellScript.1]

[HKEY_CURRENT_USER\Software\Classes\Microsoft.PowerShellScript.1\Shell]

[HKEY_CURRENT_USER\Software\Classes\Microsoft.PowerShellScript.1\Shell\Sign]

[HKEY_CURRENT_USER\Software\Classes\Microsoft.PowerShellScript.1\Shell\Sign\Command]
@="C:\\\Windows\\\System32\\\WindowsPowerShell\\\v1.0\\\powershell.exe -command Set-AuthenticodeSignature '%1' @(Get-ChildItem cert:\\\CurrentUser\\\My -codesigning)[0]"
Run Code Online (Sandbox Code Playgroud)