Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
In the Windows environment, ensuring the integrity and authenticity of scripts and executables is crucial for security. One way to achieve this is through code signing, which involves adding a digital signature to your code. In PowerShell, the Set-AuthenticodeSignature
cmdlet is used to sign scripts and other files with a digital certificate. This cmdlet helps verify that the script has not been tampered with and confirms the identity of the script's author. This article will guide you through the process of using Set-AuthenticodeSignature
in Windows PowerShell, including practical examples to illustrate its usage.
Examples:
1. Prerequisites:
Set-AuthenticodeSignature
, you need a code-signing certificate. You can obtain one from a trusted certificate authority (CA) or create a self-signed certificate for testing purposes.2. Creating a Self-Signed Certificate:
New-SelfSignedCertificate -Type CodeSigning -Subject "CN=MyCodeSigningCert" -CertStoreLocation Cert:\CurrentUser\My
3. Signing a Script:
MyScript.ps1
that you want to sign. First, retrieve the certificate: $cert = Get-ChildItem Cert:\CurrentUser\My -CodeSigningCert | Where-Object { $_.Subject -eq "CN=MyCodeSigningCert" }
Set-AuthenticodeSignature
cmdlet to sign the script: Set-AuthenticodeSignature -FilePath "C:\Path\To\MyScript.ps1" -Certificate $cert
MyScript.ps1
using the specified certificate.4. Verifying the Signature:
Get-AuthenticodeSignature
cmdlet: Get-AuthenticodeSignature -FilePath "C:\Path\To\MyScript.ps1"
5. Running Signed Scripts:
AllSigned
or RemoteSigned
: Set-ExecutionPolicy RemoteSigned