Antwort

1

Dies ist ein moderner Ansatz für selbstsignierte PowerShell-Skripts. Ich habe festgestellt, ältere Versionen von PowerShell ISE (nur bestätigt 2.0) wird Ihre Skripte in Big Endian vs UTF-8 kodieren und Probleme mit der Unterzeichnung verursachen. Mit dieser Methode sollten Sie nicht darauf stoßen, da wir auf v4 + hier sind.
Anforderung: PoSH 4.0+.

Diese Funktion prüft: ob ein Pfx-Zertifikat existiert und importiert es in LocalMachine\TrustedPublisher; Überprüfen Sie, ob ein Zertifikat an es übergeben wurde, exportieren Sie es in ein Pfx-Zertifikat und importieren Sie es; oder erstellen Sie das Zertifikat zu LocalMachine\Personal, exportieren Sie es und importieren Sie es. Ich war nicht in der Lage, die Erlaubnis zu bekommen, mit mir zu arbeiten, die Geschäfte außerhalb von \My (Personal) zu verwenden.

$ErrorActionPreference = 'Stop' 

Function New-SelfSignedCertificate 
{ 
    Param([Parameter(Mandatory=$True)]$PfxCertPath,$CertObj) 

    # Creates a SecureString object 
    $Cred = (Get-Credential).Password 

    If (Test-Path $PfxCertPath) 
    { 
     Try { 
      Import-PfxCertificate -FilePath $PfxCertPath -Password $Cred -CertStoreLocation Cert:\LocalMachine\TrustedPublisher 
      Write "$($PfxCertPath.FriendlyName) exists and is valid. Imported certificate to TrustedPublishers" 
     } Catch { 
      Write "Type mismatch or improper permission. Ensure your PFX cert is formed properly." 
      Write "[$($_.Exception.GetType().FullName)] $($_.Exception.Message)" 
     } 
    } ElseIf ($CertObj) { 
     Try { 
      Export-PfxCertificate -Cert $CertObj -FilePath $PfxCertPath -Password $Cred -Force 
      Import-PfxCertificate -FilePath $PfxCertPath -Password $Cred -CertStoreLocation Cert:\LocalMachine\TrustedPublisher 
     } Catch { 
      Write "[$($_.Exception.GetType().FullName)] $($_.Exception.Message)" 
     } 
    } Else { 
     Try { 
      $DNS = "$((GWMI Win32_ComputerSystem).DNSHostName).$((GWMI Win32_ComputerSystem).Domain)" 
      $CertObj = New-SelfSignedCertificate -CertStoreLocation Cert:\LocalMachine\My -DnsName $DNS -Type CodeSigningCert -FriendlyName 'Self-Sign' 
      Export-PfxCertificate -Cert $CertObj -FilePath $PfxCertPath -Password $Cred -Force 
      Import-PfxCertificate -FilePath $PfxCertPath -Password $Cred -CertStoreLocation Cert:\LocalMachine\TrustedPublisher 
     } Catch { 
      Write "[$($_.Exception.GetType().FullName)] $($_.Exception.Message)" 
     } 
    } 
} 

# Can be called like: 
# Sign-Script -File C:\Script.ps1 -Certificate (GCI Cert:\LocalMachine\TrustedPublisher -CodeSigningCert) 
# 
# After the cert is imported to TrustedPublisher, you can use the 
# exported pfx cert to sign on the machine instead of this method 
Function Sign-Script 
{ 
    Param($File,$Cert) 
    If($Cert-is[String]){Try{$Cert=Get-PfxCertificate("$Cert")}Catch{}} 
    Set-AuthenticodeSignature -FilePath $File -Certificate $Cert -Force 
} 
Function Check-SignedScript 
{ 
    Param($File) 
    Get-AuthenticodeSignature -FilePath $File 
} 

Nachdem alles gesagt und getan ist, können Sie Set-ExecutionPolicy AllSigned als Administrator ausführen und dieses Skript verwenden, um alle Ihre Skripte unterschreiben. Check-SignedScript wird Ihnen sagen, ob das Zeichen gültig ist und Sie können sagen, ob Sign-Script funktioniert, da Ihre Datei # SIG # Begin signature block am Ende haben wird. Alle Änderungen an einem signierten Skript müssen neu signiert werden, um ausgeführt werden zu können.

Verwandte Themen