Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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'
share|improve this question

2 Answers 2

Try switching to single quotes in the first command. The double quotes are preforming variable substitution.

share|improve this answer
    
I just tried with it with single quotes... no luck. I'll update my question to remove the double quotes. –  dthrasher Aug 17 '12 at 18:52
    
He is saying use double quotes instead of single quotes. If you use double quotes, it will substitute the value of a variable. If you use single quotes, it treats it just as a string of characters, without substituting values. –  Nick Aug 17 '12 at 20:48

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.

$Service = "InstanceNameOfService"
$Computer = "MyComputer"
$varService = Get-WmiObject -computername $Computer -class win32_service | Where-Object { $_.Name -eq $Service }
$varService.restartservice()
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.