Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Use Set-AuthenticodeSignature in Windows PowerShell

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:



  • Before you can use 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:



  • Open PowerShell as an administrator and run the following command to create a self-signed certificate:
     New-SelfSignedCertificate -Type CodeSigning -Subject "CN=MyCodeSigningCert" -CertStoreLocation Cert:\CurrentUser\My

  • This command generates a certificate and stores it in the current user's personal certificate store.


3. Signing a Script:



  • Let's assume you have a PowerShell script named MyScript.ps1 that you want to sign. First, retrieve the certificate:
     $cert = Get-ChildItem Cert:\CurrentUser\My -CodeSigningCert | Where-Object { $_.Subject -eq "CN=MyCodeSigningCert" }

  • Next, use the Set-AuthenticodeSignature cmdlet to sign the script:
     Set-AuthenticodeSignature -FilePath "C:\Path\To\MyScript.ps1" -Certificate $cert

  • This command adds a digital signature to MyScript.ps1 using the specified certificate.


4. Verifying the Signature:



  • To verify the signature of a signed script, use the Get-AuthenticodeSignature cmdlet:
     Get-AuthenticodeSignature -FilePath "C:\Path\To\MyScript.ps1"

  • This command returns information about the signature, including its status (valid, invalid, or unknown).


5. Running Signed Scripts:



  • Ensure that your PowerShell execution policy allows running signed scripts. You can set the execution policy to AllSigned or RemoteSigned:
     Set-ExecutionPolicy RemoteSigned

  • This command allows running scripts that are signed by a trusted publisher.


To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.