AutopilotMonitor.ps1
<#PSScriptInfo
.VERSION 1.11 .GUID 1b6608bd-f562-4cd3-9726-5ded493f5263 .AUTHOR Piotr Gardy .COMPANYNAME .COPYRIGHT .TAGS Windows AutoPilot .LICENSEURI .PROJECTURI .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES Version 1.0: Original published version #> <# .SYNOPSIS A sample script that monitors connectivity to Intune and shows errors and win32app info for c:\ProgramData\Microsoft\IntuneManagementExtension\Logs\IntuneManagementExtension.log log file .DESCRIPTION A sample script that monitors connectivity to Intune and shows errors and win32app info for c:\ProgramData\Microsoft\IntuneManagementExtension\Logs\IntuneManagementExtension.log log file .EXAMPLE .\AutopilotMonitor.ps1 Run the script #> #Let's define parameters $SleepInSeconds = 10 $LogFilePath = "c:\ProgramData\Microsoft\IntuneManagementExtension\Logs\IntuneManagementExtension.log" $LinesNumber = 50 Function ShowCMLog ($sLine) { $reLine = ([regex]'<!\[LOG\[(.+)\]LOG\]!>').matches($sLine); if ($reline.count -gt 0 ) { $body = $reLine[0].Groups[1].Value } $reLine = ([regex]'<time="(.+)" date="(.+)" component').matches($sLine); if ($reline.count -gt 0 ) { $DateTime = $reLine[0].Groups[2].Value + " " + $reLine[0].Groups[1].Value } $oLog = New-Object System.Object; $oLog | Add-Member -type NoteProperty -name DateTime -value $DateTime; $oLog | Add-Member -type NoteProperty -name Message -value $body $oLog = $oLog | Sort-Object 'DateTime' if ($reline.count -gt 0 ) { $oLog } } Function ShowFilteredContent { $global:content = $global:content | select-string -Pattern "BackgroundWorker is checking at" -NotMatch $global:content = $global:content | select-string -Pattern "Total valid AAD User session count is" -NotMatch $global:content = $global:content | select-string -Pattern "ESP checker found 0 session for user" -NotMatch $global:content = $global:content | select-string -Pattern "active user sessions" -NotMatch $global:content = $global:content | Select-Object -last $LinesNumber $diff = $global:content3.InputObject #| Sort-Object $lines = @() foreach ($line in ($diff) ) { $lines += ShowCMLog $line } $lines = $lines | Sort-Object 'DateTime' foreach ($oLog in $lines) { if ($oLog.Message -ilike "*exception*") { write-host -Foreground Yellow "$($oLog.DateTime) $($oLog.Message)" } else { write-host "$($oLog.DateTime) $($oLog.Message)" } } } Function ProcessLog { $global:content = @() $FileContent = get-content $LogFilePath $FileContent2 = @() for ($i = 0; $i -lt $FileContent.Length; $i++) { if ($null -ne ([regex]'<!\[LOG\[(.+)\]LOG\]!>').matches($FileContent[$i]).Success) { $FileContent2 += $FileContent[$i] } else { if ($null -ne ([regex]'<!\[LOG\[(.+)').matches($FileContent[$i]).Success) { $merged = $false [string]$str = $FileContent[$i] while (!$merged) { $i++; $str += $FileContent[$i] if ($null -ne ([regex]'\]LOG\]!>').matches($FileContent[$i]).Success) { $merged = $true $FileContent2 += $str } } } } } $global:content += $FileContent2 | Select-String -pattern "\[Win32App\]" $global:content += $FileContent2 | Select-String -pattern "WebException" ShowFilteredContent | Sort-Object } function CheckHttp ($url, $UseMachineCert = $false) { $RequestSucceded = $false $errorMessage = "" if ($UseMachineCert) { $certToUse = $null $CertOK = $false $ValidIssuer = "CN=Microsoft Intune MDM Device CA" #1.3.6.1.5.5.7.3.2 => Client Authentication $certs = Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object { ($_.Issuer -ieq $ValidIssuer) -and ($_.NotAfter -gt (Get-Date)) -and $($_.EnhancedKeyUsageList.ObjectId -contains "1.3.6.1.5.5.7.3.2") } foreach ($cert in $certs) { $CertOK = $true $certToUse = $cert break } } write-host -NoNewline "$($url) =====>" try { if ($UseMachineCert -and ($null -ne $certToUse)) { $result = Invoke-WebRequest -Uri $url -CertificateThumbprint ($cert.Thumbprint) -UseBasicParsing } else { $result = Invoke-WebRequest -Uri $url -UseBasicParsing } } catch { $errorMessage = $errorMessage + " " + $_.Exception.Message $a = 1 } if ($null -ne $result.StatusCode) { if ($result.StatusCode -ieq 200) { $RequestSucceded = $true write-host -ForegroundColor Green "OK" } } if ($RequestSucceded -eq $false) { for ($i = 1; $i -le 5; $i++) { [console]::beep() } write-host -ForegroundColor RED "FAILED: $($errorMessage)" } } while ($true) { cls CheckHttp "https://manage.microsoft.com" #CheckHttp "https://fef.XXXX.manage.microsoft.com/TrafficGateway/TrafficRoutingService/SideCar/StatelessSideCarGatewayService" $true write-host write-host write-host #ProcessLog Start-Sleep -Seconds $SleepInSeconds } |