Important Notice: On February 29th, this community was put into read-only mode. All existing posts will remain but customers are unable to add new posts or comment on existing. Please feel to join our Community Discord for any questions and discussions.

Is it possible to set custom return codes with powershell steps?

I have a PowerShell script that deploys signatures.

fail - unable to access computer

return 0 - nothing to deploy (signature is already there)

return 1 - deployed signature, see output log for more info.

  • Is there an output I can set at the end of the powershell script to identify how it went on a specific machine?

 

Here's the script: 

$ErrorActionPreference = "Continue"
$LatestSignatureFolder = "\\pdq\Repository\Signatures"
foreach ($PCUsername in $(Get-ChildItem c:\users)){
$PCUsernameADready = $PCUsername -replace '.comp',''
if($PCusername -like "*pdq.user*"){continue}
$aduser = (([adsisearcher]"(&(objectCategory=User)(samaccountname=$PCUsernameADready))").findall()).properties.userprincipalname
if(!($aduser)){Write-Debug "Can't find $PCUsernameADready in AD, skipping to next user";continue}
if($aduser -like "*@compsolutions.ca"){$signame = "NewcompSigSept2018"}
if($aduser -like "*@compandassociates.com"){$signame = "NewcompASig2018"}
$UserSignatureLocation = "C:\Users\$PCUsername\AppData\Roaming\Microsoft\Signatures"
if(Test-Path "$LatestSignatureFolder\$aduser"){
if(Test-Path $UserSignatureLocation){
Write-Debug "Trying to find OUTLOOK SIGNATURE for $PCUsernameADready to use - $LatestSignatureFolder\$aduser"
if(!(Test-Path "$UserSignatureLocation\$signame.htm")){Copy-Item "$LatestSignatureFolder\$aduser\$signame.htm" "$UserSignatureLocation\$signame.htm" -Verbose -Force}
#if(!(Test-Path "$UserSignatureLocation\$signame.htm")){Copy-Item "$LatestSignatureFolder\$aduser\*" "$UserSignatureLocation" -Verbose -Force -Recurse -Container}
if(!(Compare-Object -ReferenceObject $(Get-Content "$LatestSignatureFolder\$aduser\$signame.htm") -DifferenceObject $(Get-Content "$UserSignatureLocation\$signame.htm"))){Write-Debug "Signature for $aduser has not changed since last deployment";Write-Output "Nothing Deployed";continue}
Copy-Item "$LatestSignatureFolder\$aduser\$signame.htm" "$UserSignatureLocation\$signame.htm" -Verbose -Force
#Copy-Item "$LatestSignatureFolder\$aduser\*" "$UserSignatureLocation" -Verbose -Force -Recurse -Container
}else{Write-debug "Cannot find $UserSignatureLocation"
#Copy-Item "$LatestSignatureFolder\$aduser\$signame.htm" "$UserSignatureLocation\$signame.htm" -Verbose -Force
#Copy-Item "$LatestSignatureFolder\$aduser\*" "$UserSignatureLocation" -Verbose -Force -Recurse -Container
}
}else{Write-Debug "Can't find signature for $PCUsername"}
}
1

Comments

3 comments
Date Votes
  • Sure, just state -1 or -12 or whatever in your condition like 

     

    }else{Write-Debug "Can't find signature for $PCUsername"}
    -12
    }

     

    PDQ will dispay your error somthing as like "powershell returned -12" and you will know error 12 hapenned

     

    Hope this helps

    1
  • F V

    That didn't work in my test. I always use "Exit" to throw error codes:

    Exit -12
    2
  • Just checked my Script, You are right, you have to use Exit <error code>

     

    0