Set-AuthenticodeSignature
将 Authenticode 签名添加到 PowerShell 脚本或其他文件中。
语法
Set-AuthenticodeSignature
[-Certificate] <X509Certificate2>
[-IncludeChain <String>]
[-TimestampServer <String>]
[-HashAlgorithm <String>]
[-Force]
[-FilePath] <String[]>
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Set-AuthenticodeSignature
[-Certificate] <X509Certificate2>
[-IncludeChain <String>]
[-TimestampServer <String>]
[-HashAlgorithm <String>]
[-Force]
-LiteralPath <String[]>
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Set-AuthenticodeSignature
[-Certificate] <X509Certificate2>
[-IncludeChain <String>]
[-TimestampServer <String>]
[-HashAlgorithm <String>]
[-Force]
-SourcePathOrExtension <String[]>
-Content <Byte[]>
[-WhatIf]
[-Confirm]
[<CommonParameters>]
说明
此 cmdlet 仅在 Windows 平台上可用。
Set-AuthenticodeSignature
cmdlet 向任何支持使用者接口包(SIP)的文件添加验证码签名。
在 PowerShell 脚本文件中,签名采用文本块的形式,指示脚本中执行的指令的末尾。 如果此 cmdlet 运行时文件中存在签名,则会删除该签名。
示例
示例 1 - 使用本地证书存储中的证书对脚本进行签名
这些命令从 PowerShell 证书提供程序检索代码签名证书,并使用它对 PowerShell 脚本进行签名。
$cert = Get-ChildItem -Path Cert:\CurrentUser\My -CodeSigningCert
Set-AuthenticodeSignature -FilePath PsTestInternet2.ps1 -Certificate $cert
第一个命令使用 Get-ChildItem
cmdlet 和 PowerShell 证书提供程序来获取证书存储 Cert:\CurrentUser\My
子目录中的证书。 Cert:
驱动器是由证书提供程序公开的驱动器。 仅证书提供程序支持的 CodeSigningCert 参数将检索到的证书限制为具有代码签名机构。 该命令将结果存储在 $cert
变量中。
第二个命令使用 Set-AuthenticodeSignature
cmdlet 对 PSTestInternet2.ps1
脚本进行签名。 它使用 FilePath 参数指定脚本的名称和 Certificate 参数来指定证书存储在 $cert
变量中。
注意
使用 CodeSigningCert 参数与 Get-ChildItem
时,仅返回具有代码签名权限且包含私钥的证书。 如果没有私钥,则证书不能用于签名。
示例 2 - 使用 PFX 文件中的证书对脚本进行签名
这些命令使用 Get-PfxCertificate
cmdlet 加载代码签名证书。 然后,使用它对 PowerShell 脚本进行签名。
$cert = Get-PfxCertificate -FilePath C:\Test\Mysign.pfx
Set-AuthenticodeSignature -FilePath ServerProps.ps1 -Certificate $cert
第一个命令使用 Get-PfxCertificate
cmdlet 将 C:\Test\MySign.pfx
证书加载到 $cert
变量中。
第二个命令使用 Set-AuthenticodeSignature
对脚本进行签名。 Set-AuthenticodeSignature
的 FilePath 参数指定要签名的脚本文件的路径,Cert 参数将包含证书的 $cert
变量传递给 Set-AuthenticodeSignature
。
如果证书文件受密码保护,PowerShell 会提示输入密码。
示例 3 - 添加包含根机构的签名
此命令添加一个数字签名,其中包含信任链中的根颁发机构,并由第三方时间戳服务器签名。
$SignatureParams = @{
FilePath = "c:\scripts\Remodel.ps1"
Certificate = $cert
IncludeChain = "All"
TimestampServer = "https://timestamp.fabrikam.com/scripts/timstamper.dll"
}
Set-AuthenticodeSignature @SignatureParams
该命令使用 FilePath 参数指定正在签名的脚本和 Certificate 参数来指定保存在 $cert
变量中的证书。 它使用 IncludeChain 参数来包括信任链(包括根证书颁发机构)中所有的签名。 它还使用 TimeStampServer 参数将时间戳添加到签名。
这样可以防止脚本在证书过期时失败。
参数
-Certificate
指定将用于对脚本或文件进行签名的证书。 输入存储表示证书的对象或获取证书的表达式的变量。
若要查找证书,请使用 Get-PfxCertificate
或使用证书 Cert:
驱动器中的 Get-ChildItem
cmdlet。 如果证书无效或没有 code-signing
颁发机构,该命令将失败。
类型: | X509Certificate2 |
Position: | 1 |
默认值: | None |
必需: | True |
接受管道输入: | False |
接受通配符: | False |
-Confirm
在运行 cmdlet 之前,提示你进行确认。
类型: | SwitchParameter |
别名: | cf |
Position: | Named |
默认值: | False |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
-Content
此参数出现在语法列表中,因为它是在从 Set-AuthenticodeSignature
派生的基类中定义的。 但是,Set-AuthenticodeSignature
中未实现对此参数的支持。
类型: | Byte[] |
Position: | Named |
默认值: | None |
必需: | True |
接受管道输入: | True |
接受通配符: | False |
-FilePath
指定正在签名的文件的路径。
类型: | String[] |
Position: | 1 |
默认值: | None |
必需: | True |
接受管道输入: | True |
接受通配符: | False |
-Force
允许 cmdlet 将签名追加到只读文件。 即使使用 Force 参数,cmdlet 也无法替代安全限制。
类型: | SwitchParameter |
Position: | Named |
默认值: | False |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
-HashAlgorithm
指定 Windows 用于计算文件的数字签名的哈希算法。
对于 PowerShell 7.3,默认值为 SHA256,即 Windows 默认哈希算法。 对于早期版本,默认值为 SHA1。 在其他系统上可能无法识别使用其他哈希算法签名的文件。 支持哪些算法取决于作系统的版本。
有关可能值的列表,请参阅 HashAlgorithmName 结构。
类型: | String |
Position: | Named |
默认值: | SHA256 |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
-IncludeChain
确定证书信任链中的证书包含在数字签名中。 NotRoot 是默认值。
有效值为:
- 签名者:仅包括签名者的证书。
- NotRoot:包括证书链中的所有证书,根颁发机构除外。
- 全部:包括证书链中的所有证书。
类型: | String |
Position: | Named |
默认值: | NotRoot |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
-LiteralPath
指定正在签名的文件的路径。 与 FilePath 不同,LiteralPath 参数的值严格按照所键入的形式使用。 不会将任何字符解释为通配符。 如果路径包含转义字符,请将它括在单引号中。 单引号告知 PowerShell 不要将任何字符解释为转义序列。
类型: | String[] |
别名: | PSPath, LP |
Position: | Named |
默认值: | None |
必需: | True |
接受管道输入: | True |
接受通配符: | False |
-SourcePathOrExtension
此参数出现在语法列表中,因为它是在从 Set-AuthenticodeSignature
派生的基类中定义的。 但是,Set-AuthenticodeSignature
中未实现对此参数的支持。
类型: | String[] |
Position: | Named |
默认值: | None |
必需: | True |
接受管道输入: | True |
接受通配符: | False |
-TimestampServer
使用指定的时间戳服务器向签名添加时间戳。 键入时间戳服务器的 URL 作为字符串。 URL 必须以 https://
或 http://
开头。
时间戳表示证书添加到文件的确切时间。 如果证书过期,时间戳可防止脚本失败,因为用户和程序可以验证证书在签名时是否有效。
类型: | String |
Position: | Named |
默认值: | None |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
-WhatIf
显示 cmdlet 运行时会发生什么情况。 cmdlet 未运行。
类型: | SwitchParameter |
别名: | wi |
Position: | Named |
默认值: | False |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
输入
可以通过管道将包含文件路径的字符串传递给此 cmdlet。
输出
此 cmdlet 返回表示所设置的值的 Signature 对象。
备注
此 cmdlet 仅在 Windows 平台上可用。