PowerShell Scripting Signing

If you ever need to get in the script signing business the need to sign multiple scripts can be done via some of the commercially available development tools but it can also be done without any of them. Here’s a short script to simplify the script signing process.

 

if($args[0] -ne $NULL)
{
    $myfile = $args[0]
    $cert = @(Get-ChildItem cert:\CurrentUser\My -codesigning)[0]
    if($cert.Verify() -eq $true)
    {
        Write-Host "Beginning to sign: " $myfile
        Set-AuthenticodeSignature "$myfile" $cert
    }
    else
    {
        Write-Host "Error: The current user does not have a valid code-signing certificate"
        Write-Host
        Write-Host "Raw cert dump:"
        Write-Host $cert | FL *
    }
}
else
{
   Write-host "The script needs to be called in the format: .\ScriptSigner.ps1 file-path-of-script-to-sign"
   Write-Host
   Write-Host "All scripts are signed according to the current user's code signing certificate"
   $error = 1
}

Leave a comment

Your email address will not be published. Required fields are marked *