Help with powershell script

I have an powershell commando that pulls all my machines of xenapp 7.6 that are registered.

But i want my script to show me output when an machine is unregistered. Can any one help me out with the if and else statement.

This is for registered machines:

Get-BrokerMachine | select MachineName, RegistrationState | Where { $_.RegistrationState -eq 'Registered' }

Th is for Unregistered servers:

Get-BrokerMachine | select MachineName, RegistrationState | Where { $_.RegistrationState -eq 'UnRegistered' }

Output is like this:

MachineName                                                                                           RegistrationState
-----------                                                                                           -----------------
XXX\wsgrswxa01                                                                                                Registered
XXX\wsgrswxa02                                                                                                Registered
XXX\wsgrswxa03                                                                                                Registered

I tried a little bit with if and else statement. But i don't know how to do it.

(example):

if(?)
 {
 echo OK status  All XenApp machines or OK
exit 0 #Return OK status
 }
 else
 {
 echo CRITICAL status  These Machines(xxxx,xxxx) are Unregistered
exit 2 #returns critical status
 }

Can anyone help me out?

February 20th, 2015 12:04am

Hi,

Instead of piping through where, you can just return everything at once:

Get-BrokerMachine |
    Select MachineName,RegistrationState |
        Sort RegistrationState,MachineName

The sort should help to keep registered/unregistered machines together in your output.

Free Windows Admin Tool Kit Click here and download it now
February 20th, 2015 12:08am

But how do i build the if and else statement. When a machine is unregistered my else statement must give critical and the machinename that is unregistered.
February 20th, 2015 12:23am

Hi,

just to be sure: You're trying to create a monitoring script that some monitoring service uses who watches for the exit-codes to determine whether it's an alert, right?

if ((Get-BrokerMachine | Where { $_.RegistrationState -eq 'UnRegistered' } | Measure-Object).Count -gt 0)
{
    exit 2
}
else
{
    exit 0
}

Cheers,
Fred

Free Windows Admin Tool Kit Click here and download it now
February 20th, 2015 12:38am

Yes for Nagios .

February 20th, 2015 12:40am

But how do you get the Unregistered machine name in the exit 2 when it is Unregistered.

 echo CRITICAL status These Machines(XXX\wsgrswxa02,XXX\wsgrswxa03) are Unregistered
exit 2 #returns critical status

Free Windows Admin Tool Kit Click here and download it now
February 20th, 2015 12:51am

Hi,

you do that by 1) saving the query for unregistered machines, 2) Checking whether there are any unregistered Machines from that variable and 3) Writing down the names (again from the variables).

Cheers,
Fred

 

oh, ok, here you go :)

$Machines = Get-BrokerMachine | Where { $_.RegistrationState -eq 'UnRegistered' }
if (($Machines | Measure-Object).Count -gt 0)
{
	Write-Output "CRITICAL status  These Machines ($(($Machines | Select-Object -ExpandProperty MachineName) -join ",")) are Unregistered"
	exit 2
}
else
{
	Write-Output "OK status  All XenApp machines or OK"
	exit 0
}

February 20th, 2015 1:19am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics