|
| 1 | +<# |
| 2 | + |
| 3 | +.SYNOPSIS |
| 4 | + Test-HybridDevicesInternetConnectivity V2.1 PowerShell script. |
| 5 | +
|
| 6 | +.DESCRIPTION |
| 7 | + Test-HybridDevicesInternetConnectivity is a PowerShell script that helps to test the Internet connectivity to the following Microsoft resources under the system context to validate the connection status between the device that needs to be connected to Azure AD as hybrid Azure AD joined device and Microsoft resources that are used during device registration process: |
| 8 | +
|
| 9 | + https://login.microsoftonline.com |
| 10 | + https://device.login.microsoftonline.com |
| 11 | + https://enterpriseregistration.windows.net |
| 12 | +
|
| 13 | +
|
| 14 | +.AUTHOR: |
| 15 | + Mohammad Zmaili |
| 16 | +
|
| 17 | +.EXAMPLE |
| 18 | + .\Test-DeviceRegConnectivity |
| 19 | + |
| 20 | +#> |
| 21 | + |
| 22 | +Function RunPScript([String] $PSScript){ |
| 23 | + |
| 24 | +$GUID=[guid]::NewGuid().Guid |
| 25 | + |
| 26 | +$Job = Register-ScheduledJob -Name $GUID -ScheduledJobOption (New-ScheduledJobOption -RunElevated) -ScriptBlock ([ScriptBlock]::Create($PSScript)) -ArgumentList ($PSScript) -ErrorAction Stop |
| 27 | + |
| 28 | +$Task = Register-ScheduledTask -TaskName $GUID -Action (New-ScheduledTaskAction -Execute $Job.PSExecutionPath -Argument $Job.PSExecutionArgs) -Principal (New-ScheduledTaskPrincipal -UserID "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest) -ErrorAction Stop |
| 29 | + |
| 30 | +$Task | Start-ScheduledTask -AsJob -ErrorAction Stop | Wait-Job | Remove-Job -Force -Confirm:$False |
| 31 | + |
| 32 | +While (($Task | Get-ScheduledTaskInfo).LastTaskResult -eq 267009) {Start-Sleep -Milliseconds 150} |
| 33 | + |
| 34 | +$Job1 = Get-Job -Name $GUID -ErrorAction SilentlyContinue | Wait-Job |
| 35 | +$Job1 | Receive-Job -Wait -AutoRemoveJob |
| 36 | + |
| 37 | +Unregister-ScheduledJob -Id $Job.Id -Force -Confirm:$False |
| 38 | + |
| 39 | +Unregister-ScheduledTask -TaskName $GUID -Confirm:$false |
| 40 | +} |
| 41 | + |
| 42 | +Function checkProxy{ |
| 43 | +# Check Proxy settings |
| 44 | +Write-Host "Checking winHTTP proxy settings..." -ForegroundColor Yellow |
| 45 | +$ProxyServer="NoProxy" |
| 46 | +$winHTTP = netsh winhttp show proxy |
| 47 | +$Proxy = $winHTTP | Select-String server |
| 48 | +$ProxyServer=$Proxy.ToString().TrimStart("Proxy Server(s) : ") |
| 49 | +$global:Bypass = $winHTTP | Select-String Bypass |
| 50 | +$global:Bypass=$global:Bypass.ToString().TrimStart("Bypass List : ") |
| 51 | + |
| 52 | +if ($ProxyServer -eq "Direct access (no proxy server)."){ |
| 53 | + $ProxyServer="NoProxy" |
| 54 | + Write-Host "Access Type : DIRECT" |
| 55 | +} |
| 56 | + |
| 57 | +if ( ($ProxyServer -ne "NoProxy") -and (-not($ProxyServer.StartsWith("http://")))){ |
| 58 | + Write-Host " Access Type : PROXY" |
| 59 | + Write-Host "Proxy Server List :" $ProxyServer |
| 60 | + Write-Host "Proxy Bypass List :" $global:Bypass |
| 61 | + $ProxyServer = "http://" + $ProxyServer |
| 62 | +} |
| 63 | + |
| 64 | +$global:login= $global:Bypass.Contains("*.microsoftonline.com") -or $global:Bypass.Contains("login.microsoftonline.com") |
| 65 | + |
| 66 | +$global:device= $global:Bypass.Contains("*.microsoftonline.com") -or $global:Bypass.Contains("*.login.microsoftonline.com") -or $global:Bypass.Contains("device.login.microsoftonline.com") |
| 67 | + |
| 68 | +$global:enterprise= $global:Bypass.Contains("*.windows.net") -or $global:Bypass.Contains("enterpriseregistration.windows.net") |
| 69 | + |
| 70 | +return $ProxyServer |
| 71 | +} |
| 72 | + |
| 73 | +Function Test-DeviceRegConnectivity{ |
| 74 | +$ErrorActionPreference= 'silentlycontinue' |
| 75 | +'' |
| 76 | +$TestFailed=$false |
| 77 | + |
| 78 | +$ProxyServer = checkProxy |
| 79 | +'' |
| 80 | +'' |
| 81 | +Write-Host "Checking Internet Connectivity..." -ForegroundColor Yellow |
| 82 | +if ($ProxyServer -eq "NoProxy"){ |
| 83 | + $PSScript = "(Invoke-WebRequest -uri 'login.microsoftonline.com' -UseBasicParsing).StatusCode" |
| 84 | + $TestResult = RunPScript -PSScript $PSScript |
| 85 | + if ($TestResult -eq 200){ |
| 86 | + Write-Host "Connection to login.microsoftonline.com .............. Succeeded." -ForegroundColor Green |
| 87 | + }else{ |
| 88 | + $TestFailed=$true |
| 89 | + Write-Host "Connection to login.microsoftonline.com ................. failed." -ForegroundColor Red |
| 90 | + } |
| 91 | + $PSScript = "(Invoke-WebRequest -uri 'device.login.microsoftonline.com' -UseBasicParsing).StatusCode" |
| 92 | + $TestResult = RunPScript -PSScript $PSScript |
| 93 | + if ($TestResult -eq 200){ |
| 94 | + Write-Host "Connection to device.login.microsoftonline.com ...... Succeeded." -ForegroundColor Green |
| 95 | + }else{ |
| 96 | + $TestFailed=$true |
| 97 | + Write-Host "Connection to device.login.microsoftonline.com .......... failed." -ForegroundColor Red |
| 98 | + } |
| 99 | + |
| 100 | + $PSScript = "(Invoke-WebRequest -uri 'https://enterpriseregistration.windows.net/microsoft.com/discover?api-version=1.7' -UseBasicParsing -Headers @{'Accept' = 'application/json'; 'ocp-adrs-client-name' = 'dsreg'; 'ocp-adrs-client-version' = '10'}).StatusCode" |
| 101 | + $TestResult = RunPScript -PSScript $PSScript |
| 102 | + if ($TestResult -eq 200){ |
| 103 | + Write-Host "Connection to enterpriseregistration.windows.net ..... Succeeded." -ForegroundColor Green |
| 104 | + }else{ |
| 105 | + $TestFailed=$true |
| 106 | + Write-Host "Connection to enterpriseregistration.windows.net ........ failed." -ForegroundColor Red |
| 107 | + } |
| 108 | +}else{ |
| 109 | + if ($global:login){ |
| 110 | + $PSScript = "(Invoke-WebRequest -uri 'login.microsoftonline.com' -UseBasicParsing).StatusCode" |
| 111 | + $TestResult = RunPScript -PSScript $PSScript |
| 112 | + }else{ |
| 113 | + $PSScript = "(Invoke-WebRequest -uri 'login.microsoftonline.com' -UseBasicParsing -Proxy $ProxyServer).StatusCode" |
| 114 | + $TestResult = RunPScript -PSScript $PSScript |
| 115 | + } |
| 116 | + if ($TestResult -eq 200){ |
| 117 | + Write-Host "Connection to login.microsoftonline.com .............. Succeeded." -ForegroundColor Green |
| 118 | + }else{ |
| 119 | + $TestFailed=$true |
| 120 | + Write-Host "Connection to login.microsoftonline.com ................. failed." -ForegroundColor Red |
| 121 | + } |
| 122 | + |
| 123 | + if ($global:device){ |
| 124 | + $PSScript = "(Invoke-WebRequest -uri 'device.login.microsoftonline.com' -UseBasicParsing).StatusCode" |
| 125 | + $TestResult = RunPScript -PSScript $PSScript |
| 126 | + }else{ |
| 127 | + $PSScript = "(Invoke-WebRequest -uri 'device.login.microsoftonline.com' -UseBasicParsing -Proxy $ProxyServer).StatusCode" |
| 128 | + $TestResult = RunPScript -PSScript $PSScript |
| 129 | + } |
| 130 | + if ($TestResult -eq 200){ |
| 131 | + Write-Host "Connection to device.login.microsoftonline.com ...... Succeeded." -ForegroundColor Green |
| 132 | + }else{ |
| 133 | + $TestFailed=$true |
| 134 | + Write-Host "Connection to device.login.microsoftonline.com .......... failed." -ForegroundColor Red |
| 135 | + } |
| 136 | + |
| 137 | + if ($global:enterprise){ |
| 138 | + $PSScript = "(Invoke-WebRequest -uri 'https://enterpriseregistration.windows.net/microsoft.com/discover?api-version=1.7' -UseBasicParsing -Headers @{'Accept' = 'application/json'; 'ocp-adrs-client-name' = 'dsreg'; 'ocp-adrs-client-version' = '10'}).StatusCode" |
| 139 | + $TestResult = RunPScript -PSScript $PSScript |
| 140 | + }else{ |
| 141 | + $PSScript = "(Invoke-WebRequest -uri 'https://enterpriseregistration.windows.net/microsoft.com/discover?api-version=1.7' -UseBasicParsing -Proxy $ProxyServer -Headers @{'Accept' = 'application/json'; 'ocp-adrs-client-name' = 'dsreg'; 'ocp-adrs-client-version' = '10'}).StatusCode" |
| 142 | + $TestResult = RunPScript -PSScript $PSScript |
| 143 | + } |
| 144 | + if ($TestResult -eq 200){ |
| 145 | + Write-Host "Connection to enterpriseregistration.windows.net ..... Succeeded." -ForegroundColor Green |
| 146 | + }else{ |
| 147 | + $TestFailed=$true |
| 148 | + Write-Host "Connection to enterpriseregistration.windows.net ........ failed." -ForegroundColor Red |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +# If test failed |
| 153 | +if ($TestFailed){ |
| 154 | + '' |
| 155 | + '' |
| 156 | + Write-Host "Test failed: device is not able to communicate with MS endpoints under system account" -ForegroundColor red -BackgroundColor Black |
| 157 | + '' |
| 158 | + Write-Host "Recommended actions: " -ForegroundColor Yellow |
| 159 | + Write-Host "- Make sure that the device is able to communicate with the above MS endpoints successfully under the system account." -ForegroundColor Yellow |
| 160 | + Write-Host "- If the organization requires access to the internet via an outbound proxy, it is recommended to implement Web Proxy Auto-Discovery (WPAD)." -ForegroundColor Yellow |
| 161 | + Write-Host "- If you don't use WPAD, you can configure proxy settings with GPO by deploying WinHTTP Proxy Settings on your computers beginning with Windows 10 1709." -ForegroundColor Yellow |
| 162 | + Write-Host "- If the organization requires access to the internet via an authenticated outbound proxy, make sure that Windows 10 computers can successfully authenticate to the outbound proxy using the machine context." -ForegroundColor Yellow |
| 163 | +} |
| 164 | + |
| 165 | + '' |
| 166 | + '' |
| 167 | + Write-Host "Script completed successfully." -ForegroundColor Green -BackgroundColor Black |
| 168 | + '' |
| 169 | +} |
| 170 | + |
| 171 | +Function PSasAdmin{ |
| 172 | + $currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent()) $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) |
| 173 | +} |
| 174 | + |
| 175 | +$global:Bypass="" |
| 176 | +$global:login=$false |
| 177 | +$global:device=$false |
| 178 | +$global:enterprise=$false |
| 179 | +if (PSasAdmin){ |
| 180 | + # PS running as admin. |
| 181 | + Test-DeviceRegConnectivity |
| 182 | +}else{ |
| 183 | + '' |
| 184 | + Write-Host "PowerShell is NOT running with elevated privileges" -ForegroundColor Red -BackgroundColor Black |
| 185 | + '' |
| 186 | + Write-Host "Recommended action: This test needs to be running with elevated privileges" -ForegroundColor Yellow -BackgroundColor Black |
| 187 | + '' |
| 188 | + '' |
| 189 | + Write-Host "Script completed successfully." -ForegroundColor Green -BackgroundColor Black |
| 190 | + '' |
| 191 | + exit |
| 192 | +} |
|
0 commit comments