Hi,
I am hoping someone can shed some light on what seems an incredibly simple thing but is driving me mad :-)
I have a PoSH script which I am delivering through InTune as kind of a 'First Setup' script for an endpoint coming in via AutoPilot. It does a bunch of stuff and I like to segment out each section with 'Try/Catch' and logging so that I can pinpoint if and when any errors occur.
Now this script all works fine however I am now building out error handling to ensure that when something goes wrong (lets face it when not if! :-) ) The script can handle the error accordingly and react. I have a huge amount of experience in MEM and in a TS this is really quite easy so I am trying to fathom why InTune is finding this so hard.
Here is a really simple example of a Try Catch where I am trapping the error right at the bottom so that it is the last thing passed out (I have tried many many permutations by the way)
FYI - There is a function to support the logging!
Create Simple Logs Directory
Try {
New-item -ItemType Directory -Force -Path "C:\Logs" -ErrorAction Stop
Write-Log -logFile $logFile -type "[INFO]" -message "Basic Log directory created, lets get cracking."
New-item -ItemType Directory -Force -Path "Q:\Temp" -ErrorAction Stop
Write-log -logFile $logFile -type "[INFO]" -message "Block Completion with exit code $LASTEXITCODE"
}
Catch {
Write-Log -logFile $logFile -type "[INFO]" -message "Script ran into an instant error, close out"
Write-Log -logFile $logFile -type "[INFO]" -message "Re-Define the Exit Code for block continuation"
$LASTEXITCODE = "1603"
Write-Log -logFile $logFile -type "[INFO]" -message "Script Exit Code is now: $LASTEXITCODE"
Write-Error -Message "Could not create one or more directories" -Category OperationStopped
$LASTEXITCODE
}
Right at the bottom is a simple verification that the code is still trapped and the script should close out with a 1603!
Write-Log -logFile $logFile -type "[INFO]" -message "Passing back to Windows InTune: $LASTEXITCODE"
$LASTEXITCODE
Now the above is super basic and the purposeful error is the fact there is no Q Drive. So I want it to fail, jump into the 'catch' and my most recent attempt prior to posting here was to set the error, use a 'write-message' (as InTune struggles with error codes in the PowerShell Script Area apparently'), allow it out of the Try/Catch then jump all the way down the code and get to the bottom before closing out with an exit code.
This is followed up with the InTune Win 32 App setup and that a return code set as a failure for 1603!
Now when this is deployed it just seems to be blissfully unaware of this failure and it just constantly complains that the Apps identifier was not found after a successful install. Even though it never completed to start with.
This seems like a really basic feature of error handling so am really keen to see if anyone has got round this with error handling?
I have come across a few blogs which touch on the topic and seem to suggest its possible but nothing directly around passing codes back to InTune.
As a last point, in the IME log file I can track the App executing but all I can seem to see is that it just cant get an exit/return code back and it appears 'null' and it eventually makes that a success but it will fail as the App identifier block is at the end of the PoSH script.
Thanks in advance.