2016-03-27 1 views
0

habe ich ein Zertifikat und es gilt:Wie kann ich mein ps-Skript signieren, ohne es zuvor zu leeren?

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

Jetzt möchte ich mein Skript LoadAndParse.ps1 unterzeichnen.
Aber wenn ich versuche:

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 

Wenn ich etwas durch Hinzufügen eines Befehls meine Abfolge von Befehlen ändern und ich bekommen es unterzeichnet - aber jetzt ist es leer :(

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 

nun das Skript hat nur der key-block - naja es ist eine überraschende methode, ein script zu sichern !!

Wie unterzeichne ich ein skript, das etwas kann, weil der code unberührt bleibt?
Vielen dank im voraus,
Gooly

Antwort

0

Die Lösung bekommen. PS-ISE speichert in Unicode Big Endian, das Set-AuthenticodeSignature nicht versteht. Verwenden Sie Notepad ++, um das Skript in UTF-8 zu speichern, und alles ist in Ordnung.

Seufz, wenn entweder der Fehlerhinweis in die richtige Richtung zeigen würde oder Powershell seinen eigenen Code verstehen würde - es hätte mich mehrere Stunden gerettet!

Hier ist mein Skript, das Sie über diese Falle erinnert:

<# 
    .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 

Ich konnte mir nicht vorstellen eine nicht MS-produt verwenden, um ein MS-Skript ausgeführt werden soll :(

Verwandte Themen