I am using Invoke-WebRequest
in a SCOM PowerShell script to periodically monitor the availability of a URI. My script is fairly simple (since I have very little knowledge of PS :-) ):
$scomapi = new-object -comObject "MOM.ScriptAPI"
$scompb = $scomapi.CreatePropertyBag()
$fullHostName = "https://" + <full path to monitored web endpoint>
$result = Invoke-WebRequest $fullHostName
if($result.content) {
$scompb.AddValue("ConfigurationReachable",$true);
} else {
$scompb.AddValue("ConfigurationReachable",$false);
}
$scomapi.AddItem($scompb)
$scomapi.ReturnItems()
In order to test this script, I did manual changes in the hosts
file on the client running the SCOM agent where I want to do the monitoring. Interestingly, the script succeeds in fetching the web endpoint even after the host is unreachable (tested this by pinging from that machine).
I did some further tests directly from the command line, and nothing changes. Even though I have no ping to the remote address, Invoke-WebRequest
still succeeds and fetches the web page. So what am I doing wrong here?