2016-03-27 9 views
0

私は、証明書を作成し、それが有効である:これまでにpsスクリプトを空にすることなく署名する方法はありますか?

Thumbprint      Subject 
----------      ------- 
0562....................4944E CN=Callis PowerShell 

今、私は私のスクリプトLoadAndParse.ps1を署名します。
しかし、私はしようとした場合:

PS C:\...\X> $cert = @(Get-ChildItem cert:\CurrentUser\My -codesigning)[0] 
PS C:\...\X> Set-AuthenticodeSignature LoadAndParse.ps1 $cert 
Directory: C:\...\ 
SignerCertificate    Status   Path 
-----------------    ------   ---- 
            UnknownError LoadAndParse.ps1 

私は少しつのコマンドを追加することにより、注文の私の順序を変更し、私はそれが署名を取得した場合 - しかし、今、それが空である:(

PS C:\...\X> echo get-location > LoadAndParse.ps1 
    PS C:\...\X> $cert = @(Get-ChildItem cert:\CurrentUser\My -codesigning)[0] 
    PS C:\...\X> Set-AuthenticodeSignature LoadAndParse.ps1 $cert 

    Directory: C:\...\X 

    SignerCertificate    Status  Path 
    -----------------    ------  ---- 
    056260.............E24944E Valid  LoadAndParse.ps1 

今スクリプトがありキーブロックのみ - スクリプトを確保するには驚くべき方法ですよね。

コードが変更されないために何かできるスクリプトに署名するにはどうすればいいですか?
ありがとうございます。 Gooly

答えて

0

解決策を得ました。 PS-ISEは、Set-AuthenticodeSignatureが理解できないUnicode Big Endianを保存します。 メモ帳++を使用してスクリプトをUTF-8で保存しても問題ありません。

一言、エラーヒントが正しい方向を指しているか、Powershellがそれ自身のコードであると理解していれば、数時間節約できました!ここで

はこのトラップについてあなたを思い出させる私のスクリプトです:

<# 
    .SYNOPSIS 
    check whether the file is in UTF8 and sign it - otherwise give hint what to do 
    but this file has to be signed too eventually 
    #> 
    [CmdletBinding()] Param (
     [Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True)] [string]$Path, 
     [Parameter(Mandatory = $False, ValueFromPipelineByPropertyName = $True)] [int]$Idx=0 
) 

    function isUTF8 ([string]$Path, [int]$Idx) 
    { 
     [byte[]]$byte = get-content -Encoding byte -ReadCount 4 -TotalCount 4 -Path $Path 

     if ($byte[0] -eq 0xef -and $byte[1] -eq 0xbb -and $byte[2] -eq 0xbf) { 
     Write-Output "$Path`r`nis UTF8, going to sign it using cert. index: $Idx" 
     sign $Path $Idx 
     } else { 
     [console]::beep(500,600) 
     Write-Output "$Path is NOT UTF8!`r`nLoad it in Notepad++ and save it in UTF8 - otherwise it can't be signed" 
     } 
    } 


    function sign ([string]$Path, [int]$Idx) { 
    $cert = @(gci cert:\currentuser\my -codesigning)[$Idx] 
    Set-AuthenticodeSignature $Path $cert 
    } 

    isUTF8 $Path $Idx 

私はMS-スクリプトを実行するために、非MS-produtを使用することを想像することができませんでした:(

関連する問題