2

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'
dthrasher
  • 207
  • 2
  • 7

2 Answers2

1

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

Mike Shepard
  • 748
  • 3
  • 7
  • 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
0

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()
Nick
  • 256
  • 1
  • 5
  • 20