This discussion has been locked. The information referenced herein may be inaccurate due to age, software updates, or external references.
You can no longer post new replies to this discussion. If you have a similar question you can start a new discussion in this forum.

Issues with Powershell Monitors

Good Afternoon from the Midwest,

I am having a problem getting the output from my script to act the way that i want it to.

Here is the script:

$ErrorActionPreference = "silentlycontinue";

$service =  Get-Service -name MSSQL$*, MSSQLSERVER*, SQLAGENT$*, ReportWriter$*, SQLSERVERAGENT -Exclude ReportWriter$CRM_DEV, ReportWriter$CRM_DEV02, MSSQLServerOLAPService, MSSQLServerADHelper100 | Select-Object DisplayName

$status =   Get-Service -name MSSQL$*, MSSQLSERVER*, SQLAGENT$*, ReportWriter$*, SQLSERVERAGENT -Exclude ReportWriter$CRM_DEV, ReportWriter$CRM_DEV02, MSSQLServerOLAPService, MSSQLServerADHelper100 | Select-Object Status

$Error.Clear();

if ( ($service -eq "SQL SERVER") -and $status -eq "Running" )

{

Write-Host "Message: $Service in this $status";

exit 0;

}

if ( ($service -eq "SQL SERVER") -and $status -ne "Running" )

{

Write-Host "Message: $Service is not Running, please log into the Server to see why. ";

exit 1;

}

if ( ($service -eq "SQL SERVER AGENT") -and $status -eq "Running" )

{

Write-Host "Message: $Service in this $status";

exit 0;

}

if ( ($service -eq "SQL SERVER AGENT") -and $status -ne "Running" )

{

Write-Host "Message: $Service is not Running, please log into the Server to see why.";

exit 1;

}

  if ( ($service -eq "SQL Server Reporting Services") -and $status -eq "Running" )

{

Write-Host "Message: $Service in this $status";

exit 0;

}

if ( ($service -eq "SQL Server Reporting Services") -and $status -ne "Running" )

{

Write-Host "Message: $Service is not Running, please log into the Server to see why.";

exit 1;

}

if ($Error.Count -eq 0) {

Write-Host "Message: All Services are running correctly.";

exit 0;

}

Write-Host "Message: $($Error[0])";

exit 1;

What i am trying to figure out is twofold. One, can you only return numbers or statistics and not Statuses like Running Stopped etc, and if you can return Statuses then how do I format it to get the data i need in the Get Script Output section of the Custom Powershell Monitor.

The variables have the powershell i used get the services i was looking for on a VM and it worked beautifully.

  • Hello,

    a script monitor has to return a statistic. This is a numeric value used to determine how the monitor compares to its set thresholds. This must be an integer value and is required. The message is optional.

    There is a limit of 10 statistic and message pairs for the script and each statistic and message output pair of your script requires a unique identifier.

    So you have to transform the status (running or not) into an integer value and output the statistic and message pair.

    For example:

    Write-Host "Statistic.service1: $status_service1";

    Write-Host "Message.service1: $Service1 is not Running, please log into the Server to see why";

    Write-Host "Statistic.service2: $status_service2";

    Write-Host "Message.service2: $Service2 is not Running, please log into the Server to see why";

    and so on ......

    Additional to that I would not exit the script directly if one of the checked services is not running as the rest of the script would not be executed and you would not get any output for the other services.

    I hope this was helpful. Let me know if you have more questions.

  • This is a great answer to what I am looking for. Now the only question I have is how to turn the Status which is a String (i.e. Running, Stopped) to an integer (i.e. 1,2,3)?@

  • You can output the status of each service in the message and just add something like 'write-host "Statistic.serviceX: 1";'

    Then set the critical threshold to 'greater than 0' and your component will be in critical state if there is a service which is not running.

    For example:

    if ( ($service -eq "SQL SERVER") -and $status -eq "Running" )

    {

    Write-Host "Message.service1: $Service in this $status";

    write-host "Statistic.service1: 0";

    }

    if ( ($service -eq "SQL SERVER") -and $status -ne "Running" )

    {

    Write-Host "Message.service1: $Service is not Running, please log into the Server to see why. ";

    write-host "Statistic.service1: 1";

    }

    Another solution could be to output just one message and statistic pair. The statistic returns the value of how many services are "running" and as you know how many services are expected to be running you could set the threshold to "less than X".

    Take a look at this Windows Multi-Service Monitor (Powershell)  It may suit your needs as it is or just take it as an example to play around with.

  • Thank you so much for this script link... This makes a lot more sense and now I have a template to build my future monitors with.

  • $ServiceNames = $args.get(0).split('|')

    = New-Object System.Collections.Generic.List[System.Object]

    CheckServiceExist ($ServiceName)

    if (!(Get-Service -Name $ServiceName -ErrorAction SilentlyContinue))

    $RunResults.Add("WARNING: $ServiceName does not exist")

    return $false

    else{return $true}

    CheckServiceState ($ServiceName)

    $ServiceState = (Get-Service -Name $ServiceName).Status

    if(!($ServiceState -eq 'Running')){

    $RunResults.Add("CRITICAL: $ServiceName -> $ServiceState")

    else{$RunResults.Add("OK: $ServiceName -> $ServiceState")}

    ($ServiceName in $ServiceNames)

    $srv_exist = (CheckServiceExist -ServiceName $ServiceName)

    if ($srv_exist){

    CheckServiceState -ServiceName $ServiceName

    ($RunResults -match "WARNING:"){$resultcode = 2}

    ($RunResults -match "CRITICAL:"){$resultcode = 3}

    = ($RunResults | select-string -pattern "OK:").length

    = ($RunResults | select-string -pattern "WARNING:").length

    = ($RunResults | select-string -pattern "CRITICAL:").length

    = $count_warning + $count_critical

    = $RunResults.Count



    = @()

    ( $i = 1 ; $i -le $RunResults.Count; $i++ )

    $lines += "<br/>"

    $lines += $RunResults[$RunResults.Count - $i]

    "Statistic: $total_errors"



    ($resultcode -eq 2)

    write-host "Message: $count_ok of $total_results monitored services running $lines"

    exit $resultcode

    ($resultcode -eq 3)

    write-host "Message: $count_ok of $total_results monitored services running $lines"

    exit $resultcode

    ($resultcode -eq 0)

    write-host "Message: $count_ok of $total_results monitored services running $lines"

    exit $resultcode

    1

    The problem I have with the script is that I run it on my powershell monitor and it does what it is supposed to do as far as the separate statistic and message, but it is not reading the services correctly. I honestly think the Else Statement is not doing its job but I am not a programmer enough to see how to fix it. Any ideas.

    EDIT: I figured out the problem! I didn't turn on the remote option to run the script. D'Oh This script is the truth by the way!