Search code examples
powershellcertificatedigital-signature

Digitally signing all identified powershell scripts in TFS Azure Devops


I am working on digitally signing of all powershell scripts in a folder from a repo in the TFS azure devops.

Using below command I can sign all the scripts under a specific folder:

signtool.exe sign /f MyCertificate.pfx /p Mypassword "C:\folder\repo\powershellscripts\*.ps1

So here I need to implement a logic in powershell script like if the MyCertificate.pfx is not found in the specified path the signing process should fail saying required certificate not found to sign.

I know the below code to check above executed code returns 0 success, 1 fail. But to check whether the certificate is present is the specified path or not I need help.

 if (! $?) {
       Write-Host "required certificate not found to sign" -ForegroundColor Red
       exit
   }

Solution

  • you do not specify a path for MyCertificate.pfx, so it must be present in the current workdirectory and so you can do

    If (Test-Path .\MyCertificate.pfx){
    signtool.exe sign /f MyCertificate.pfx /p Mypassword "C:\folder\repo\powershellscripts\*.ps1
    }
    Else {
    write-error "required certificate not found to sign"
    return #exit would cause to exit the process, what you probably not want to do
    }