1
votes

I got this script from a blog and modified it to suit my environment. However I am having problems iterating through the foreach loop and getting the service status or the remote computers ($unregisteredVM).

It seems as if the Get-Service was trying to query the computer executing the script instead of the remote computers.

Also I tried running the portion of the script which grabs the status of the computer and set that variable as the -Computername parameter and it doesn't seem to take it.

$icaservicestate = Get-Service PorticaService -ComputerName $unregisteredVM | select status

What am i missing? Below is the entire code.

##Load Citrix Modules
asnp Citrix.*

#Variables for email
[string[]]$recipients = "[email protected]"
$fromEmail = "[email protected]"
$server = "itopia-us.mail.protection.outlook.com"
$date= Get-Date

##Check for VMs on and unregistered
$unregisteredVMs = Get-BrokerDesktop -RegistrationState Unregistered | Select MachineName
[string]$emailVMs = Get-BrokerDesktop -RegistrationState Unregistered | Select MachineName | ft -wrap -autosize | Out-String

IF (!$unregisteredVMs) {
##Send all clear email
[string]$emailBody = "There were no powered on desktops in an unregistered state."
send-mailmessage -from $fromEmail -to $recipients -subject " XenDesktop Daily Check - $date" -body $emailBody -priority High -smtpServer $server
}
Else {
##If powered-on and unregistered, perform a forceful restart
foreach ($unregisteredVM in $unregisteredVMs)
{
 $icaservicestate = Get-Service PorticaService -ComputerName $unregisteredVM | select status
IF ($icaservicestate = Stopped) {
Set-Service -Name PorticaService -Status Running
}
Else {
sc \\$unregisteredVM start PorticaService
}
 #Write-Host "Hello, I am unregistered: $unregisteredVM"
}

#Send an email report of VMs to be restarted due to being in an unregistered state
[string]$emailBody = "The following desktops were forcefully restarted due to not registering with the DDCs in a timely manner. `n $emailVMs"
send-mailmessage -from $fromEmail -to $recipients -subject " XenDesktop Daily Check - $date" -body $emailBody -priority High -smtpServer $server
}
3
I think this might help. You have are returning an object array when you just want a list of names. Instead of $unregisteredVMs = Get-BrokerDesktop -RegistrationState Unregistered | Select MachineName try $unregisteredVMs = Get-BrokerDesktop -RegistrationState Unregistered | Select -ExpandProperty MachineNameMatt
Matt - I tried your suggestion and while I understand the array issue, when adding the expand property and assigning the $testicaserver = Get-Service PorticaService $unregisteredVM | select status it says "cannot find service with service name "PorticaService" I think it's trying to run it on the local VM instead of the remote.Darian Jimenez
Just before that line what do you have for $unregisteredVMs? It should just be a list of machine names in which case it should work. Is it possible that service is not running?Matt
I have the following - $unregistedVMs = Get-BrokerDesktop -RegistrationState Unregistered | select -ExpandPropoerty MachineName - It can't be that the service is running because if I run the powershell command Get-Service PorticaService -Computername Machinename it will actually retrieve the state.Darian Jimenez
Sorry I wasnt clear enough. I wanted you to look at the contents of $unregisteredVMs to see if it looks as its supposed to.Matt

3 Answers

1
votes

I'm not sure if this is your entire problem but I don't think this is what you want:

IF ($icaservicestate = Stopped)

That assigns the result of executing a command named Stopped to the variable $icaservicestate. I doubt you have a command named Stopped. For equality comparison use -eq and quote Stopped:

IF ($icaservicestate -eq 'Stopped') { ... }
1
votes

There are two potential reasons you may not be getting the result you want.

First: In Powershell a single equals sign is the assignment operator.
Your if statement line 24 is re-assigning the value of $icaservicestate to an error

$icaservicestate = Get-Service PorticaService -ComputerName $unregisteredVM | select status
IF ($icaservicestate = Stopped) {
    Set-Service -Name PorticaService -Status Running
}

Use IF ($icaservice -eq 'stopped') {


Second: The line 23 Cmdlet Get-Service maybe failing due to line 11 the object you are passing to it

$unregisteredVMs = Get-BrokerDesktop -RegistrationState Unregistered | Select MachineName

In the above line you've generated an [array] of objects with each object having a single property called MachineName.
On line 23 you are trying to pass the objects to the ComputerName parameter of the Get-Service Cmdlet. To work around this you can use $unregisteredVM.MachineName in the Get-Service cmdlet

$icaservicestate = Get-Service PorticaService -ComputerName $unregisteredVM.MachineName | select status

You can read more about the way property values are passed in the pipeline by typing at the prompt:
help about_piplines and looking for 'ByValue' and 'ByPropertyName'

UPDATE:

if ($icaservicestate -eq 'Stopped') {
    Get-Service -Name PorticaService -ComputerName $unregisteredVM.MachineName | Set-Service -Status Running
}

Previously, the line Set-Service -Name PorticaService -Status Running would be attempting to start the service on your local machine.

First we grab a reference to the PorticaService on the remote machine, then pipe that object to the Set-Service Cmdlet and set the status to running.

0
votes

I think your script doesn't work because the machinename you get back from "get-brokerdesktop" is in the format domainname\vdiname. For "get-service -computername" you only need to enter the hostname. To "isolate" the hostname I'd do: $unregisteredVM=$unregisteredVM.machinename -replace ("domainname\," ") $unregisteredVM=$unregsiteredVM.trim()

Now $unregisteredVM should contain only the hostname.