Skip to content

Commit

Permalink
handle error output, to signal Intune failed script exec
Browse files Browse the repository at this point in the history
  • Loading branch information
okieselbach committed Feb 16, 2018
1 parent 766644f commit c2fc2b7
Showing 1 changed file with 39 additions and 20 deletions.
59 changes: 39 additions & 20 deletions ManagementExtension-Samples/IntunePSTemplate.ps1
Original file line number Diff line number Diff line change
@@ -1,51 +1,70 @@
# Author: Oliver Kieselbach
# Date: 02/10/2018
# Description: Intune Management Extension - PowerShell template with logging, error codes, and x64 PowerShell execution

# The script is provided "AS IS" with no warranties.
<#
Version: 1.1
Author: Oliver Kieselbach
Script: IntunePSTemplate.ps1
Description:
Intune Management Extension - PowerShell script template with logging,
error codes, standard error output handling and x64 PowerShell execution.
Release notes:
Version 1.0: Original published version.
Version 1.1: Added standard error output handling.
The script is provided "AS IS" with no warranties.
#>

$exitCode = 0

if (![System.Environment]::Is64BitProcess)
{
# start new PowerShell as x64 bit process, wait for it and gather exit code and standard error output
$sysNativePowerShell = "$($PSHOME.ToLower().Replace("syswow64", "sysnative"))\powershell.exe"

# start new PowerShell as x64 bit process, wait for it and gather exit code
$exitCode = $(Start-Process $sysNativePowerShell -ArgumentList "-ex bypass -file `"$PSCommandPath`"" -WindowStyle Hidden -Wait -PassThru).ExitCode
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = $sysNativePowerShell
$pinfo.Arguments = "-ex bypass -file `"$PSCommandPath`""
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.CreateNoWindow = $true
$pinfo.UseShellExecute = $false
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null

$exitCode = $p.ExitCode

$stderr = $p.StandardError.ReadToEnd()

if ($stderr) { Write-Error -Message $stderr }
}
else
{
# start logging to TEMP in file "scriptname".log
Start-Transcript -Path "$env:TEMP\$($(Split-Path $PSCommandPath -Leaf).ToLower().Replace(".ps1",".log"))"
Start-Transcript -Path "$env:TEMP\$($(Split-Path $PSCommandPath -Leaf).ToLower().Replace(".ps1",".log"))" | Out-Null

# === variant 1 ===
#
# => use "-ErrorAction Stop" for the command-lets and catch in case of errors
#
# === variant 1: use try/catch with ErrorAction stop -> use write-error to signal Intune failed execution
# example:
#
# try
# {
# Set-ItemProperty ... -ErrorAction Stop
# }
# catch
# {
# {
# Write-Error -Message "Could not write regsitry value" -Category OperationStopped
# $exitCode = -1
# }

# === variant 2 ===
#
# => use "-ErrorVariable err -ErrorAction SilentlyContinue" and catch error and check err variable
#
# === variant 2: ErrorVariable and check error variable -> use write-error to signal Intune failed execution
# example:
#
# Start-Process ... -ErrorVariable err -ErrorAction SilentlyContinue
# if ($err)
# {
# Write-Error -Message "Could not write regsitry value" -Category OperationStopped
# $exitCode = -1
# }

Stop-Transcript
Stop-Transcript | Out-Null
}

exit $exitCode

0 comments on commit c2fc2b7

Please sign in to comment.