Showing results for 
Search instead for 
Did you mean: 
Create Post
Level 7

Powershell Script and decision structure

Im monitoring a web service with powershell.

My webservice returns "ON" or "OFF" if a Hardware Controller device is Online or not.

My statistic is the time the script takes to find the controller (if ON) and it should have status DOWN on Orion when the web service returns OFF.

Im new to powershell, my script calls the web service, get the status back and present the statistic,, but I cannot get it to exit properly.

My script is:


$control = New-WebServiceProxy -uri "" -Namespace ns1 -Class class1;

$sw = [Diagnostics.Stopwatch]::StartNew();

$status = $control.getMachineX('', 'Passwd1','1').controlResponse.status;


[decimal]$elapsed = [Math]::Round(($sw.Elapsed.TotalSeconds), 3);

Write-Host 'Statistic.Elapsed: ' $elapsed;

[bool]$state = $false;

if ($status -eq 'ON') {

    $state = $true;


else {$state = $false};

Write-Host $state;

if ($state -eq $true) {

   write-host IS TRUE;

   Exit 0


else {Write-Host IS FALSE;

    Exit 1



The problem is the last 7 lines.

I dont know how to convert the status (ON or OFF) to boolean, and then make the script exit with (0) or (1) depending of the boolean (true or false)

My question boils to:

- How can I make the script exit (0) or exit (1) depending on the value returned from a boolean variable.?

Thanks guys!!


0 Kudos
7 Replies
Product Manager
Product Manager

I recommend taking a look at the Exchange 2010 Database Availability Group template. It contains examples of how to convert string values to Boolean using PowerShell.

0 Kudos

I did,, but I didndt understand,, I was wondering if I could get specific help for a specific problem without having to read the whole lot of examples.

(many of them not on Powershell)

Do you know how to write a decision structure with Boolean?

Im trying to discard if Im doing something wrong, before I ask Solarwinds support for help.


0 Kudos

Some of the Script Guru's here on Thwack may be able to provide you with guidance but there are other forums on the internet that are be better suited for helping people write PowerShell scripts.

0 Kudos

Steven Klassen, MVP and Guru, might be able to help with this.  I believe he's familiar with PowerShell and works mostly with SAM.

0 Kudos

You're missing the required Write-Host with a numeric statistic.  If you don't have any interesting numbers to report back to the SAM template, use zero as I've done below.  To aLTeReGo's point, make sure you're use a valid credential and checking the box below that that says "impersonate" so it's running with the right credentials.

That being said, you needn't bother with the boolean and then the true/false check.  You're already doing fine with the string equality provided the status that's getting returned it literally 'ON' or [something other than ON].  If not, your -eq isn't going to work.  If it has a little space beforehand or some other string in it, you'll want to use -match instead.  Here's the simplified version, assuming that you're getting ON/OFF without anything else.

Also, you can dispense with all the semi-colons.  This isn't perl.   You only need them if you're a) putting two statements on the same line, or b) ending a switch case like I'm doing below (without a default).  There may be other times, but they're few and far between in powershell.

$control = New-WebServiceProxy -uri "" -Namespace ns1 -Class class1

$sw = [Diagnostics.Stopwatch]::StartNew()

$status = $control.getMachineX('', 'Passwd1','1').controlResponse.status


[decimal]$elapsed = [Math]::Round(($sw.Elapsed.TotalSeconds), 3)

Write-Host 'Statistic.Elapsed: ' $elapsed

Write-Host "Statistic: 0"  # required by SAM, even if it's not a useful value

switch ($status) {

    'ON' {

        Write-Host "The status was $status..."  # for console debugging




        Write-Host "The status was $status..."  # for console debugging




0 Kudos

The problem is that my script works perfect outside Orion APM.

But when I run it inside, it behaves weird,, exiting with (1) no matter the state of the decision variable.

I crosscheck with outside forums, the script is fine,, but I dont know why it doesnt work properly on APM.

Always retuning DOW on the component.

I wanted to check inside Thwack before I open a support ticket.


0 Kudos

If your PowerShell script executes properly when run under your user account, but doesn't when run from SAM the most likely issue is permissions. By default all locally run PowerShell scripts run under the "system" user context. You may want to ensure you've enabled impersonation for your PowerShell component monitors in SAM so the scripts are run under a user account that has adequate permissions.

I also recommend looking through the SAM PowerShell guide which explains in detail many of the various options/settings of the PowerShell component monitor.

0 Kudos