答えて

1

これは、自己署名PowerShellスクリプトの現代的なアプローチです。旧バージョンのPowerShell ISE(確認済み2.0のみ)がBig EndianとUTF-8のスクリプトをエンコードし、署名に問題が発生することが判明しました。この方法では、ここではv4 +を使用しているので、これに慣れるべきではありません。
要件:PoSH 4.0+。

この機能は、Pfx証明書が存在するかどうかをチェックし、それをLocalMachine\TrustedPublisherにインポートします。証明書が渡されたかどうかを確認し、Pfx証明書にエクスポートしてインポートします。証明書をLocalMachine\Personalに作成し、エクスポートしてインポートします。 \My(Personal)以外のCert:\CurrentUserストアを使用する権限を私に与えることができませんでした。すべてが言わおよび実行された後

$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 
} 

、あなたは管理者としてSet-ExecutionPolicy AllSignedを実行し、すべてのスクリプトを署名するこのスクリプトを使用することができます。 Check-SignedScriptは、標識が有効かどうかを教えてくれるので、ファイルとしてSign-Scriptが働いているかどうかを確認するには、最後に# SIG # Begin signature blockがあるはずです。実行するためには、署名されたスクリプトの編集をすべて再署名する必要があります。

関連する問題