Last active
November 1, 2024 04:06
-
-
Save okieselbach/4f11ba37a6848e08e8f82d9f2ffff516 to your computer and use it in GitHub Desktop.
Intune Management Extension PowerShell Template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
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