Set-AuthenticodeSignature
為 PowerShell 腳本或其他檔案新增 Authenticode 簽章。
語法
ByPath (預設值)
Set-AuthenticodeSignature
[-Certificate] <X509Certificate2>
[-FilePath] <String[]>
[-IncludeChain <String>]
[-TimestampServer <String>]
[-HashAlgorithm <String>]
[-Force]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
ByLiteralPath
Set-AuthenticodeSignature
[-Certificate] <X509Certificate2>
-LiteralPath <String[]>
[-IncludeChain <String>]
[-TimestampServer <String>]
[-HashAlgorithm <String>]
[-Force]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
ByContent
Set-AuthenticodeSignature
[-Certificate] <X509Certificate2>
-SourcePathOrExtension <String[]>
-Content <Byte[]>
[-IncludeChain <String>]
[-TimestampServer <String>]
[-HashAlgorithm <String>]
[-Force]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Description
此 Cmdlet 只能在 Windows 平臺上使用。
此 Set-AuthenticodeSignature cmdlet 會為任何支援主體介面套件(SIP)的檔案加入 Authenticode 簽名。
在 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 。
Note
使用 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 簽署腳本。 FilePath Set-AuthenticodeSignature 參數指定簽署腳本檔案的路徑,Cert 參數將包含憑證的變數傳遞$cert給 Set-AuthenticodeSignature。
如果憑證檔案有密碼保護,PowerShell 會提示你輸入密碼。
範例 3 - 新增包含根權限的簽名
此指令會新增一個數位簽章,包含信任鏈中的根權限,並由第三方時間戳記伺服器簽署。
$signingParameters = @{
FilePath = 'C:\scripts\Remodel.ps1'
Certificate = $cert
HashAlgorithm = 'SHA256'
IncludeChain = 'All'
TimestampServer = 'http://timestamp.fabrikam.com/scripts/timstamper.dll'
}
Set-AuthenticodeSignature @signingParameters
指令使用 FilePath 參數來指定要簽署的腳本,並用 Certificate 參數指定該變數中儲存 $cert 的憑證。 它使用 IncludeChain 參數來包含信任鏈中的所有簽名,包括根權限。 它也會使用 TimeStampServer 參數來為簽名加上時間戳。
這樣可以防止憑證過期時腳本失敗。
參數
-Certificate
指定用於簽署腳本或檔案的憑證。 輸入一個變數,儲存代表憑證的物件或取得憑證的表達式。
要找到憑證,請使用Get-PfxCertificateGet-ChildItem憑證Cert:磁碟中的 cmdlet。 如果憑證無效或沒有 code-signing 權限,指令就會失敗。
參數屬性
| 類型: | X509Certificate2 |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | 1 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Confirm
在執行 Cmdlet 之前,提示您進行確認。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | False |
| 支援萬用字元: | False |
| 不要顯示: | False |
| 別名: | 參見 |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Content
這個參數會出現在語法清單中,因為它是在所衍生的基底類別 Set-AuthenticodeSignature 中定義的。 然而,該參數的支援並未在 Set-AuthenticodeSignature中實作。
參數屬性
| 類型: | Byte[] |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
ByContent
| Position: | Named |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-FilePath
指定正在簽署檔案的路徑。
參數屬性
| 類型: | String[] |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
ByPath
| Position: | 1 |
| 必要: | True |
| 來自管線的值: | True |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-Force
允許指令小程式將簽章附加到唯讀檔案上。 即使使用 Force 參數,Cmdlet 也無法覆蓋安全性限制。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | False |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-HashAlgorithm
指定 Windows 用來計算檔案數位簽章的雜湊演算法。
PowerShell 7.3 的預設是 SHA256,這是 Windows 預設的雜湊演算法。 早期版本的預設是 SHA1。 使用不同雜湊演算法簽署的檔案,可能在其他系統上無法被辨識。 支援的演算法取決於作業系統的版本。
關於可能的值列表,請參見 HashAlgorithmName 結構體。
參數屬性
| 類型: | String |
| 預設值: | SHA256 |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-IncludeChain
決定哪些憑證在憑證信任鏈中包含在數位簽章中。 NotRoot 是預設。
有效值為:
-
Signer:僅包含簽署人證明書。 -
NotRoot:包含憑證鏈中所有憑證,唯獨不包含根權威機構。 -
All:包含憑證鏈中的所有憑證。
參數屬性
| 類型: | String |
| 預設值: | NotRoot |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-LiteralPath
指定正在簽署檔案的路徑。 與 FilePath 不同的是, LiteralPath 參數的值是完全按照它輸入的。 不會將任何字元解譯為通配符。 如果路徑包含逸出字元,請以單引弧括住它。 單引號會告知PowerShell不要將任何字元解譯為逸出序列。
參數屬性
| 類型: | String[] |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
| 別名: | PSPath, LP |
參數集
ByLiteralPath
| Position: | Named |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-SourcePathOrExtension
這個參數會出現在語法清單中,因為它是在所衍生的基底類別 Set-AuthenticodeSignature 中定義的。 然而,該參數的支援並未在 Set-AuthenticodeSignature中實作。
參數屬性
| 類型: | String[] |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
ByContent
| Position: | Named |
| 必要: | True |
| 來自管線的值: | True |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-TimestampServer
使用指定的時間戳伺服器為簽名加上時間戳記。 輸入時間戳伺服器的 URL 作為字串。 URL 必須以 http:// 開頭。
時間戳代表憑證加入檔案的精確時間。 時間戳記可防止憑證過期時腳本失敗,因為使用者與程式可驗證憑證簽署時的有效性。
Note
PowerShell 7.3 新增了對 https:// 帶有此參數的 URL 的支援。 不過,底層 API 不支援 HTTPS。 如果你用 HTTPS,指令會回傳錯誤,但檔案是沒有時間戳記的。 更多資訊請參見 第#25130期。
參數屬性
| 類型: | String |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-WhatIf
顯示 Cmdlet 執行時會發生什麼事。 該 Cmdlet 未被執行。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | False |
| 支援萬用字元: | False |
| 不要顯示: | False |
| 別名: | 威斯康辛 |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
CommonParameters
此 cmdlet 支援常見參數:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 如需詳細資訊,請參閱 about_CommonParameters。
輸入
String
你可以用管道連接包含檔案路徑的字串到這個 cmdlet。
輸出
Signature
此 cmdlet 會回傳一個代表其設定值的 Signature 物件。
備註
此 Cmdlet 僅適用於 Windows 平臺。