Friday, 17 January 2025

Powershell Script Sign

 #Parameters
$CertificateName = "PowerShell Code Signing"
 
#Create a Self-Signed SSL certificate
$Certificate = New-SelfSignedCertificate -CertStoreLocation Cert:\CurrentUser\My -Subject "CN=$CertificateName" -KeySpec Signature -Type CodeSigningCert
 
#Export the Certificate to "Documents" Folder in your computer
$CertificatePath = [Environment]::GetFolderPath("MyDocuments")+"\$CertificateName.cer"
Export-Certificate -Cert $Certificate -FilePath $CertificatePath
 
#Add Certificate to the "Trusted Root Store"
Get-Item $CertificatePath | Import-Certificate -CertStoreLocation "Cert:\LocalMachine\Root"
 
Write-host "Certificate Thumbprint:" $Certificate.Thumbprint






=====================================================
#Set Parameters
$CertificateThumbprint = "40F84ACFDE3CAC51651E0BE793E8AF4F2B338343"
$ScriptPath = "C:\Users\kumar-ajeet\Downloads\test.ps1"
 
#Get the Certificate from Cert Store
$CodeSignCert = Get-ChildItem -Path Cert:\CurrentUser\My | Where-Object {$_.Thumbprint -eq $CertificateThumbprint}
 
#Sign the PS1 file
Set-AuthenticodeSignature -FilePath $ScriptPath -Certificate $CodeSignCert


Get-AuthenticodeSignature C:\Users\kumar-ajeet\Downloads\test.ps1 | fl

No comments:

Post a Comment