將 Authenticode 簽章新增至 Windows PowerShell 指令碼或其他檔案。

語法

Set-AuthenticodeSignature [-FilePath] <string[]> [-Certificate] <X509Certificate2> [-Force] [-HashAlgorithm <string>] [-IncludeChain <string>] [-TimestampServer <string>] [-Confirm] [-WhatIf] [<CommonParameters>]

描述

Set-AuthenticodeSignature Cmdlet 會將 Authenticode 簽章新增至任何支援 Subject Interface Package (SIP) 的檔案。

在 Windows PowerShell 指令碼檔案中,簽章是以文字區塊的形式出現,可指出指令碼所執行之指令的結尾。如果執行此 Cmdlet 時檔案中已經有簽章,會移除該簽章。

參數

-Certificate <X509Certificate2>

指定用來簽署指令碼或檔案的憑證。輸入已儲存代表憑證之物件的變數,或是可取得憑證的運算式。

若要尋找憑證,請使用 Get-PfxCertificate 或在 Certificate (Cert:) 磁碟機中使用 Get-ChildItem Cmdlet。如果憑證無效或未持有程式碼簽署授權,命令便會失敗。

必要?

true

位置?

2

預設值

接受管線輸入?

false

接受萬用字元?

false

-FilePath <string[]>

指定要簽署的檔案所在路徑。

必要?

true

位置?

1

預設值

接受管線輸入?

true (ByValue, ByPropertyName)

接受萬用字元?

false

-Force

允許此 Cmdlet 將簽章附加至唯讀的檔案。即便使用 Force 參數,此 Cmdlet 也無法覆寫安全性限制。

必要?

false

位置?

named

預設值

接受管線輸入?

false

接受萬用字元?

false

-HashAlgorithm <string>

指定 Windows 用來計算檔案數位簽章的雜湊演算法。預設值為 SHA1,亦即 Windows 預設雜湊演算法。

使用不同雜湊演算法簽署的檔案可能無法在其他系統上辨識。

必要?

false

位置?

named

預設值

SHA1

接受管線輸入?

false

接受萬用字元?

false

-IncludeChain <string>

決定要將憑證信任鏈結上的哪些憑證包含在數位簽章中。預設值為 "NotRoot"。

有效的值包括:

-- Signer:只包含簽署者的憑證。

-- NotRoot:包含憑證鏈結中的所有憑證,唯獨根授權除外。

--All:包含憑證鏈結中的所有憑證。

必要?

false

位置?

named

預設值

接受管線輸入?

false

接受萬用字元?

false

-TimestampServer <string>

使用指定的時間戳記伺服器,將時間戳記新增到簽章。請以字串形式輸入時間戳記伺服器的 URL。

時間戳記表示憑證加入至檔案的確切時間。如果憑證過期,時間戳記可避免指令碼失敗,因為使用者和程式可以確認憑證在簽署時是有效的。

必要?

false

位置?

named

預設值

接受管線輸入?

false

接受萬用字元?

false

-Confirm

在執行命令前先提示確認。

必要?

false

位置?

named

預設值

接受管線輸入?

false

接受萬用字元?

false

-WhatIf

說明執行命令時將會發生何種情況,但不會實際執行命令。

必要?

false

位置?

named

預設值

接受管線輸入?

false

接受萬用字元?

false

<CommonParameters>

這個 Cmdlet 支援一般參數:-Verbose、-Debug、-ErrorAction、-ErrorVariable、-OutBuffer 和 -OutVariable。如需詳細資訊,請參閱 about_Commonparameters.

輸入和輸出

輸入型別是可經由管道輸出至 Cmdlet 的物件型別。傳回型別則是 Cmdlet 所傳回的物件型別。

輸入

System.String

您可經由管道將包含檔案路徑的字串輸出至 Set-AuthenticodeSignature。

輸出

System.Management.Automation.Signature

範例 1

C:\PS>$cert=Get-ChildItem -Path cert:\CurrentUser\my -CodeSigningCert

C:\PS>Set-AuthenticodeSignature -FilePath PsTestInternet2.ps1 -certificate $cert

描述
-----------
這些命令會從 Windows PowerShell Certificate 提供者擷取程式碼簽署憑證,並用它來簽署 Windows PowerShell 指令碼。

第一個命令會使用 Get-ChildItem Cmdlet 和 Windows PowerShell Certificate 提供者來取得憑證存放區之 Cert:\CurrentUser\My 子目錄中的憑證 (Cert: 磁碟機是 Certificate 提供者所公開的磁碟機)。CodeSigningCert 參數 (只有 Certificate 提供者才支援) 會將擷取的憑證限制為那些具有程式碼簽署授權的憑證。命令會在 $cert 變數中儲存結果。

第二個命令會使用 Set-AuthenticodeSignature Cmdlet 來簽署 PSTestInternet2.ps1 指令碼。它會使用 FilePath 參數指定指令碼的名稱,並使用 Certificate 參數指定要在 $cert 變數中儲存憑證。






範例 2

C:\PS>$cert = Get-PfxCertificate C:\Test\Mysign.pfx 

C:\PS>Set-AuthenticodeSignature -Filepath ServerProps.ps1 -Cert $cert

描述
-----------
這些命令會使用 Get-PfxCertificate Cmdlet 來尋找程式碼簽署憑證。然後,它們會使用此憑證來簽署 Windows PowerShell 指令碼。

第一個命令會使用 Get-PfxCertificate Cmdlet 來尋找 C:\Test\MySign.pfx 憑證,並將它儲存在 $cert 變數中。

第二個命令會使用 Set-AuthenticodeSignature 來簽署指令碼。Set-AuthenticodeSignature 的 FilePath 參數會指定所簽署之指令碼檔案的路徑,而 Cert 參數則將含有憑證的 $cert 變數傳遞給 Set-AuthenticodeSignature。

如果憑證檔案受到密碼保護,Windows PowerShell 會提示您提供密碼。






範例 3

C:\PS>Set-AuthenticodeSignature -filepath c:\scripts\Remodel.ps1 -certificate $cert -IncludeChain All -TimeStampServer "http://timestamp.fabrikam.com/scripts/timstamper.dll"

描述
-----------
這個命令會新增數位簽章,以便將根授權包含在信任鏈結中,並交由第三方時間戳記伺服器來簽署。

此命令會使用 FilePath 參數指定要簽署的指令碼,並使用 Certificate 參數指定要在 $cert 變數中儲存的憑證。它會使用 IncludeChain 參數來包含信任鏈結 (包括根授權單位) 中的所有簽章,也會使用 TimeStampServer 參數,將時間戳記新增到簽章。如此可避免指令碼在憑證過期時失敗。






請參閱




目錄