mercredi 13 septembre 2017

My first Powershell funciton... Sign-Script

I'm starting to play more with powershell and for some need that we have I had to start signing script. I found many place on the internet  that explain how to get the code signing certificate from our corporate CA and then sign the script using timestamp so that script still works when the certificate expire as the timestamp certify that the certificate was valid at the time it was signed. This generates a pretty long command so I wrote my first powershell function... Sign-Script to leverage this, you can place this in your powershell profile (profile.ps1) to have it available each time you run powershell

Powershell profile ref: 

Code signing ref:

So without further delay here it comes...

#Begin copy after this line

   Sign the specified script with the first code signing certificate of the current user
   and with a default TimeStamping URL.

   This function allow you so sign a script with a timestamp so that your script will be
   valid even when the signing certificate expire. It also allow you to not have to type
   the full path to the signing certificate since it will use the first
   "Code Signing Certificate" available in the local User certificate store. This funciton
   also use a default "time stamping URL" so that you don't need to

.PARAMETER scriptname
    The script name including the full path that you want to sign

    A path to a code signing certificate.  The default is the first code signing script of
    the local user.
.PARAMETER TimeStampingServer
    URL to a time stamping server. The default is "".
   Sign-cert c:\script\test.ps1

   Sign-cert -scriptname c:\script\test.ps1 -cert gci cert:\CurrentUser\My -codesigning | where -Filter {$_.FriendlyName -eq "Thawte Code Signing"} -IncludeChain All -TimeStampServer ""
function Sign-Script
       # Description d’aide param1
                   ParameterSetName='Script name')]

        # Description d’aide param2
        $cert=(GCI cert:currentuser\my\ -CodeSigningCert)[0],
        # Description d’aide param3

    Set-AuthenticodeSignature $scriptname $cert -TimestampServer $TimeStampingServer

#End copy above this line

Aucun commentaire:

Publier un commentaire