1

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
}
6
  • 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 MachineName Commented Sep 7, 2014 at 21:14
  • 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. Commented Sep 8, 2014 at 15:12
  • 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? Commented Sep 8, 2014 at 15:33
  • 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. Commented Sep 8, 2014 at 15:49
  • Sorry I wasnt clear enough. I wanted you to look at the contents of $unregisteredVMs to see if it looks as its supposed to. Commented Sep 8, 2014 at 15:52

3 Answers 3

1

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') { ... }
Sign up to request clarification or add additional context in comments.

Comments

1

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.

4 Comments

Bob - I understand the -eq and changed that. However regarding the $unregisteredVM.MachineName isn't that passed to the array already from setting $unregisteredVMs = Get-BrokerDesktop -RegistrationState Unregistered | select MachineName ? Either way I modified it to how you say and still not working. Keeps saying that it cannot find the Porticaservice which is not true because If I run Get-Service manually with the actual computer name it will find it.
Bob answer should work. Maybe it is not expanding the machinename? This is what -ExpandProperty was supposed to do. In Bobs answer maybe it needs to be -ComputerName $($unregisteredVM.MachineName). Both mine and bobs answer fix the error so im not sure what the issue is. Maybe put quotes on the service name?
Matt - I keep getting a "Cannot find any service with service name 'PorticaService' then below another Set-Content: A positional parameter cannot be found that accepts argument 'porticaservice' at line 49 char :4
@DarianJimenez have updated my answer. The Set-Service Cmdlet will attempt to run on localhost unless a "ComputerName" property is specified. Hope this helps
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.