mirror of
https://github.com/MicrosoftDocs/windows-itpro-docs.git
synced 2025-05-29 13:47:23 +00:00
Fix error handling and retrieve event logs if an error occurs
This commit is contained in:
parent
e8e3149ffc
commit
82064a8085
@ -128,7 +128,27 @@ $namespaceName="root\cimv2\mdm\dmmap"
|
|||||||
$className="MDM_AssignedAccess"
|
$className="MDM_AssignedAccess"
|
||||||
$obj = Get-CimInstance -Namespace $namespaceName -ClassName $className
|
$obj = Get-CimInstance -Namespace $namespaceName -ClassName $className
|
||||||
$obj.ShellLauncher = [System.Net.WebUtility]::HtmlEncode($shellLauncherConfiguration)
|
$obj.ShellLauncher = [System.Net.WebUtility]::HtmlEncode($shellLauncherConfiguration)
|
||||||
$obj = Set-CimInstance -CimInstance $obj
|
$obj = Set-CimInstance -CimInstance $obj -ErrorVariable cimSetError -ErrorAction SilentlyContinue
|
||||||
|
if($cimSetError) {
|
||||||
|
Write-Output "An ERROR occurred. Displaying error record and attempting to retrieve error logs...`n"
|
||||||
|
Write-Error -ErrorRecord $cimSetError[0]
|
||||||
|
|
||||||
|
$timeout = New-TimeSpan -Seconds 30
|
||||||
|
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
|
||||||
|
do{
|
||||||
|
$events = Get-WinEvent -FilterHashtable $eventLogFilterHashTable -ErrorAction Ignore
|
||||||
|
} until ($events.Count -or $stopwatch.Elapsed -gt $timeout) # wait for the log to be available
|
||||||
|
|
||||||
|
if($events.Count) {
|
||||||
|
$events | ForEach-Object {
|
||||||
|
Write-Output "$($_.TimeCreated) [$($_.LevelDisplayName.ToUpper())] $($_.Message -replace "`n|`r")"
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Write-Warning "Timed-out attempting to retrieve event logs..."
|
||||||
|
}
|
||||||
|
|
||||||
|
Exit 1
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
[!INCLUDE [powershell-wmi-bridge-2](../../../includes/configure/powershell-wmi-bridge-2.md)]
|
[!INCLUDE [powershell-wmi-bridge-2](../../../includes/configure/powershell-wmi-bridge-2.md)]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user