I want to restart a service on a remote machine using PowerShell. The service has both a displayname and an instancename. Can I use the Restart-Service cmdlet to do this? I don't see an instancename parameter, and I can't seem to make the "displayname$instancename" syntax work.
This syntax does not work:
Invoke-Command -Session $session -ScriptBlock {Restart-Service -displayname 'DisplayName$InstanceName' -PassThru}
I thought at first the problem was the $ character, but I found that this syntax does work:
Get-Service 'displayname$instancename'
Try switching to single quotes in the first command. The double quotes are preforming variable substitution.
I use the following code to restart services on remote systems. You might need to add the
-credentials
tag if you don't have permissions. This is really nice because it works on Server 2003/Windows XP boxes as well.