Share via


Set-AuthenticodeSignature

將 Authenticode 簽章新增至 PowerShell 腳本或其他檔案。

Syntax

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>]

Description

Cmdlet 會將 Set-AuthenticodeSignature Authenticode 簽章新增至任何支持主體介面套件 (SIP) 的檔案。

在 PowerShell 腳本檔案中,簽章會採用文字區塊的形式,指出腳本中執行的指令結尾。 如果此 Cmdlet 執行時檔案中有簽章,則會移除該簽章。

範例

範例 1 - 使用來自本機證書存儲的憑證簽署腳本

這些命令會從PowerShell憑證提供者擷取程式代碼簽署憑證,並用它來簽署PowerShell腳本。

$cert=Get-ChildItem -Path Cert:\CurrentUser\My -CodeSigningCert
$signingParameters = @{
    FilePath      = 'PsTestInternet2.ps1'
    Certificate   = $cert
    HashAlgorithm = 'SHA256'
}
Set-AuthenticodeSignature @signingParameters

第一個命令會使用 Get-ChildItem Cmdlet 和 PowerShell 憑證提供者,在證書存儲的子目錄中取得憑證 Cert:\CurrentUser\My 。 磁碟 Cert: 驅動器是由憑證提供者公開的磁碟驅動器。 只有憑證提供者支援的 CodeSigningCert 參數會將擷取的憑證限製為具有程式代碼簽署授權單位的憑證。 命令會將結果儲存在變數中 $cert

第二個命令會將$signingParameters變數定義為 HashTable,其中包含 Cmdlet 簽署PSTestInternet2.ps1腳本的參數Set-AuthenticodeSignature 它會使用 FilePath 參數來指定腳本的名稱、指定憑證儲存在變數中的 $cert Certificate 參數,以及 HashAlgorithm 參數將哈希演算法設定為 SHA256。

第三個命令會藉由將 中 $signingParameters定義的參數展開來簽署腳本。

注意

使用 CodeSigningCert 參數時Get-ChildItem,只會傳回具有程式代碼簽署授權單位且包含私鑰的憑證。 如果沒有私鑰,就無法使用憑證進行簽署。

範例 2 - 使用 PFX 檔案中的憑證簽署腳本

這些命令會使用 Get-PfxCertificate Cmdlet 來載入程式代碼簽署憑證。 然後,使用它來簽署PowerShell腳本。

$cert = Get-PfxCertificate -FilePath C:\Test\Mysign.pfx
$signingParameters = @{
    FilePath      = 'ServerProps.ps1'
    Certificate   = $cert
    HashAlgorithm = 'SHA256'
}
Set-AuthenticodeSignature @signingParameters

第一個命令會 Get-PfxCertificate 使用 Cmdlet 將 C:\Test\MySign.pfx 憑證 $cert 載入變數。

第二個命令會將$signingParameters變數定義為 HashTable,其中包含 Cmdlet 簽署ServerProps.ps1腳本的參數Set-AuthenticodeSignature 它會使用 FilePath 參數來指定腳本的名稱、指定憑證儲存在變數中的 $cert Certificate 參數,以及 HashAlgorithm 參數將哈希演算法設定為 SHA256。

第三個命令會藉由將 中 $signingParameters定義的參數展開來簽署腳本。

如果憑證檔案受到密碼保護,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

第一個命令會將$signingParameters變數定義為 HashTable,其中包含 Cmdlet 簽署腳本的參數Set-AuthenticodeSignature 它會使用 FilePath 參數來指定腳本的路徑、指定憑證儲存在變數中的 $cert Certificate 參數,以及 HashAlgorithm 參數將哈希演算法設定為 SHA256。 它會使用 IncludeChain 參數,在信任鏈結中包含所有簽章,包括根授權單位。 它也會使用 TimeStampServer 參數,將時間戳新增至簽章。 這可防止腳本在憑證到期時失敗。

第二個命令會藉由展開 中 $signingParameters定義的參數來簽署腳本。

參數

-Certificate

指定將用來簽署腳本或檔案的憑證。 輸入變數,此變數會儲存代表憑證的物件或取得憑證的表達式。

若要尋找憑證,請使用 Get-PfxCertificate 或使用 Get-ChildItem 憑證 Cert: 磁碟驅動器中的 Cmdlet。 如果憑證無效或沒有 code-signing 授權單位,命令會失敗。

Type:X509Certificate2
Position:1
Default value:None
Required:True
Accept pipeline input:False
Accept wildcard characters:False

-Confirm

執行 Cmdlet 之前先提示您確認。

Type:SwitchParameter
Aliases:cf
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Content

此參數會出現在語法清單中,因為它定義於衍生自 的基類 Set-AuthenticodeSignature 中。 不過,在 中 Set-AuthenticodeSignature不會實作此參數的支援。

Type:Byte[]
Position:Named
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-FilePath

指定正在簽署之檔案的路徑。

Type:String[]
Position:1
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-Force

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

Type:SwitchParameter
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-HashAlgorithm

指定 Windows 用來計算檔案數字簽名的哈希演算法。

預設為 SHA1。 在其他系統上可能無法辨識以不同哈希演算法簽署的檔案。 支援的演算法取決於作業系統的版本。

如需可能值的清單,請參閱 HashAlgorithmName 結構

Type:String
Position:Named
Default value:Null
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-IncludeChain

決定數字簽名中包含憑證信任鏈結中的憑證。 NotRoot 是預設值。

有效值為:

  • 簽署者:只包含簽署者的憑證。
  • NotRoot:包含憑證鏈結中的所有憑證,但根授權單位除外。
  • 全部:包含憑證鏈結中的所有憑證。
Type:String
Position:Named
Default value:NotRoot
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-LiteralPath

指定正在簽署之檔案的路徑。 與 FilePath 不同,LiteralPath 參數的值會與輸入時完全相同。 不會將任何字元解譯為通配符。 如果路徑包含逸出字元,請以單引弧括住它。 單引號會告知PowerShell不要將任何字元解譯為逸出序列。

Type:String[]
Aliases:PSPath
Position:Named
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-SourcePathOrExtension

此參數會出現在語法清單中,因為它定義於衍生自 的基類 Set-AuthenticodeSignature 中。 不過,在 中 Set-AuthenticodeSignature不會實作此參數的支援。

Type:String[]
Position:Named
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-TimestampServer

使用指定的時間戳伺服器,將時間戳新增至簽章。 輸入時間戳伺服器的 URL 做為字串。

時間戳代表憑證新增至檔案的確切時間。 時間戳可防止腳本在憑證到期時失敗,因為使用者和程式可以驗證憑證在簽署時是否有效。

Type:String
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-WhatIf

顯示執行 Cmdlet 後會發生的情況。 未執行 Cmdlet。

Type:SwitchParameter
Aliases:wi
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

輸入

String

您可以使用管線傳送包含此 Cmdlet 檔案路徑的字串。

輸出

Signature

此 Cmdlet 會傳 回 Signature 物件,代表它設定的值。