This project is read-only.

Powershell Monitor Issues

Topics: Developer Forum, PolyMon 2.x, User Forum
Sep 22, 2010 at 11:44 PM
Edited Sep 22, 2010 at 11:47 PM

I have created a PowerShell monitor based on the discussions on this list to test my Web site uptime. I can test it successfully, manually test it by clicking the green run, but when it runs in the executive it throws a "Object reference not set to an instance of an object".  My script is below, any help would be appreciated

I would like to add that the service is running as the same credentials as when I test it sucessfully. 

 

function New-TrustAllWebClient {

# Create a compilation environment

$Provider=New-Object Microsoft.CSharp.CSharpCodeProvider

$Compiler=$Provider.CreateCompiler()

$Params=New-Object System.CodeDom.Compiler.CompilerParameters

$Params.GenerateExecutable=$False

$Params.GenerateInMemory=$True

$Params.IncludeDebugInformation=$False

$Params.ReferencedAssemblies.Add("System.DLL") > $null

$TASource=@'

namespace Local.ToolkitExtensions.Net.CertificatePolicy {

public class TrustAll : System.Net.ICertificatePolicy {

public TrustAll() {

}

public bool CheckValidationResult(System.Net.ServicePoint sp,

System.Security.Cryptography.X509Certificates.X509Certificate cert,

System.Net.WebRequest req, int problem) {

return true;

}

}

}

'@

$TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)

$TAAssembly=$TAResults.CompiledAssembly

## We now create an instance of the TrustAll and attach it to the ServicePointManager

$TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")

[System.Net.ServicePointManager]::CertificatePolicy=$TrustAll

## The ESX Upload requires the Preauthenticate value to be true which is not the default

## for the System.Net.WebClient class which has very simple-to-use downloadFile and uploadfile

## methods. We create an override class which simply sets that Preauthenticate value.

## After creating an instance of the Local.ToolkitExtensions.Net.WebClient class, we use it just

## like the standard WebClient class.

$WCSource=@'

namespace Local.ToolkitExtensions.Net {

class WebClient : System.Net.WebClient {

protected override System.Net.WebRequest GetWebRequest(System.Uri uri) {

System.Net.WebRequest webRequest = base.GetWebRequest(uri);

webRequest.PreAuthenticate = true;

webRequest.Timeout = System.Threading.Timeout.Infinite;

return webRequest;

}

}

}

'@

$WCResults=$Provider.CompileAssemblyFromSource($Params,$WCSource)

$WCAssembly=$WCResults.CompiledAssembly

## Now return the custom WebClient. It behaves almost like a normal WebClient.

$WebClient=$WCAssembly.CreateInstance("Local.ToolkitExtensions.Net.WebClient")

return $WebClient

}

$URI = New-Object System.Uri("http://www.msn.com")

&{#TRY

$wc = New-TrustAllWebClient

$WebStream = $wc.OpenRead($URI)

$WebStream.Close()

$Status.StatusText="OK."

$Status.StatusID=1

$Counters.Add("Uptime", 1)

}

##CATCH

trap [SystemException]

{

$Status.StatusText="Fail."

$Status.StatusID=3

Write-Host "Failed because $_"

$Counters.Add("Uptime", 3)

}

 

Sep 23, 2010 at 2:11 PM

If I recall, I had this same issue (twice) during the setup and tesing of PolyMon for the first time.

  1. The first issue I had was that the PolyMon service didn't have sufficient permissions to run powershell scripts.  Please check into your account running the executive service.
  2. Often the test environment and server running the scripts are different.  Example; I have the executive and database on a server and I run the manager on my machine.  I can't test the scripts from my machine, because I didn't install the PoshMon addin (see #3 below).  So I have to test them using the server, which has all of the necessary components installed.
  3. Make sure you have the PoshMon powershell snapin installed on the machine running that you are running the executive on

 

 

Sep 23, 2010 at 9:15 PM
Edited Sep 23, 2010 at 9:15 PM

Installing PoshMon and restarting the executive fixed the exception. Thanks.