Search code examples
powershellurloffice365

Checking the if Outlook is reachable with Powershell (GCCH)


I need to check and see if certian endpoints are reachable every few minutes, just to monitor if a firewall change may have affected connections.

I am testing a few different GCCH endpoints but I am having difficulty with outlook. It appears that even to get a status code, it wants me to log in. I do not want to pass credentials or anything with this unattented script. Is there a better way of achieving testing reachability for outlook?

$ExchangeURL = Invoke-WebRequest -Uri https://outlook.office365.us -DisableKeepAlive -Method head | select StatusCode 
$TeamsURL = Invoke-WebRequest -Uri https://gov.teams.microsoft.us -DisableKeepAlive -Method head | select StatusCode
$OneDriveURL = Invoke-WebRequest -Uri https://redacted-my.sharepoint.us -DisableKeepAlive -Method head | select StatusCode
$SharepointURL = Invoke-WebRequest -Uri https://redacted-admin.sharepoint.us -DisableKeepAlive -Method head | select StatusCode
$AdminURL = Invoke-WebRequest -Uri https://portal.office365.us -DisableKeepAlive -Method head | select StatusCode


$ExchangeURL
$TeamsURL
$OneDriveURL
$SharepointURL
$AdminURL

This gives me:

Invoke-WebRequest : The remote server returned an error: (401) Unauthorized.
At C:\Users\jimbob\Desktop\Test_URL.ps1:3 char:16
+ ... changeURL = Invoke-WebRequest -Uri https://outlook.office365.us -Disa ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand


StatusCode
----------
       200
       200
       200
       200

Solution

  • You can ignore 401 error message and say it is available. Because 401 means that you reach server and server responses you. But if you get 400, 500, 502, 503 or something else then you can say there some problematic situation.

    Without credentials there is nothing much you can do. If you do not want to store credentials in the script then you can use ConvertFrom-SecureString and ConvertTo-SecureString cmdlets, create you environment and read it from some txt without clear data.

    You can check here to reference -> Powershell encryption 1

    Also here -> Powershell encryption 2