Skip to content

Instantly share code, notes, and snippets.

@okieselbach
Last active March 8, 2023 12:36
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save okieselbach/4f11ba37a6848e08e8f82d9f2ffff516 to your computer and use it in GitHub Desktop.
Save okieselbach/4f11ba37a6848e08e8f82d9f2ffff516 to your computer and use it in GitHub Desktop.
Intune Management Extension PowerShell Template
<#
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"
$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"))" | Out-Null
# === 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: 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 | Out-Null
}
exit $exitCode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment