Ok, I’m stuck. I am trying to check if a service is running on a remote machine, and my script is able to tell if the service is running on the remote machine or if it is stopped. But it cannot start the service. Below is my code:<\/p>\n
#--- VARIABLES ---#\n#region variables\n$ServiceName = 'Service Name'\n$name = \"WORKSTATION1\"\n#end region variables\n\n#--- SCRIPT ---#\n#region script\n\n$arrService = Get-Service -ComputerName $name -Name $ServiceName\n\nwhile ($arrService.Status -ne 'Running')\n{\n\n Start-Service $ServiceName\n write-host $arrService.status\n write-host 'Service starting'\n Start-Sleep -seconds 30\n $arrService.Refresh()\n if ($arrService.Status -eq 'Running')\n {\n scriptres:\"Ok,Running\"\n }\n\n}\n<\/code><\/pre>\n
Advertisement
But PS just loops and loops, and the service on the remote machine does not even attempt to restart. I can restart the service on the remote machine thru Services.msc. And I have tried both the Service Name and Display Name. And the Description of the Service is “Handle .NET Remoting Objects.” I have a PS script that I converted to an executable so that users can restart this service on their own, but it has to be run manually. I would like to be able to start this service if it is stopped without any user interaction, but I do not want to restart the service if its Running. Any help is greatly appreciated!<\/p>","upvoteCount":7,"answerCount":7,"datePublished":"2019-11-20T22:53:25.000Z","author":{"@type":"Person","name":"briangorton5132","url":"https://community.spiceworks.com/u/briangorton5132"},"acceptedAnswer":{"@type":"Answer","text":"
Advertisement
Problem is here:<\/p>\n
$arrService = Get-Service -ComputerName $name -Name $ServiceName\n\nwhile ($arrService.Status -ne 'Running')\n{\n\n Start-Service $ServiceName\n<\/code><\/pre>\nSo you got the remote service in $arrService (why are you calling it an array??)–also, we don’t use the Hungarian notation in PowerShell, but you can if you want to.<\/p>\n
Then, when you “Start-Service” you aren’t remoting, you’re just starting the service on the local machine. Since that won’t affect $arrService you never break out of the loop. Interestingly, Start-Service doesn’t have a -ComputerName parameter! So remoting directly from it is impossible. But we can still do it thanks to the pipeline.<\/p>\n
$arrService | Start-Service\n<\/code><\/pre>\nSince the remote information is in the service object, Start-Service will consume that and start the remote service up. Give it a shot.<\/p>","upvoteCount":4,"datePublished":"2019-11-21T09:30:25.000Z","url":"https://community.spiceworks.com/t/powershell-problems-starting-service-on-remote-computer/740065/6","author":{"@type":"Person","name":"martin9700","url":"https://community.spiceworks.com/u/martin9700"}},"suggestedAnswer":[{"@type":"Answer","text":"
Ok, I’m stuck. I am trying to check if a service is running on a remote machine, and my script is able to tell if the service is running on the remote machine or if it is stopped. But it cannot start the service. Below is my code:<\/p>\n
#--- VARIABLES ---#\n#region variables\n$ServiceName = 'Service Name'\n$name = \"WORKSTATION1\"\n#end region variables\n\n#--- SCRIPT ---#\n#region script\n\n$arrService = Get-Service -ComputerName $name -Name $ServiceName\n\nwhile ($arrService.Status -ne 'Running')\n{\n\n Start-Service $ServiceName\n write-host $arrService.status\n write-host 'Service starting'\n Start-Sleep -seconds 30\n $arrService.Refresh()\n if ($arrService.Status -eq 'Running')\n {\n scriptres:\"Ok,Running\"\n }\n\n}\n<\/code><\/pre>\nBut PS just loops and loops, and the service on the remote machine does not even attempt to restart. I can restart the service on the remote machine thru Services.msc. And I have tried both the Service Name and Display Name. And the Description of the Service is “Handle .NET Remoting Objects.” I have a PS script that I converted to an executable so that users can restart this service on their own, but it has to be run manually. I would like to be able to start this service if it is stopped without any user interaction, but I do not want to restart the service if its Running. Any help is greatly appreciated!<\/p>","upvoteCount":7,"datePublished":"2019-11-20T22:53:25.000Z","url":"https://community.spiceworks.com/t/powershell-problems-starting-service-on-remote-computer/740065/1","author":{"@type":"Person","name":"briangorton5132","url":"https://community.spiceworks.com/u/briangorton5132"}},{"@type":"Answer","text":"
Run it in verbose mode or do a transcript while running to see what happens. Your while loop will off course make the script look like it’s hanging. The start fails and the while detects the svc isn’t started so it keeps trying until pigs can fly or you stop it.
\nTry an if then and not a perpetuous loop to debug and then later if it works you can think of looping it again which I don’t think is a good idea.<\/p>","upvoteCount":0,"datePublished":"2019-11-20T23:01:57.000Z","url":"https://community.spiceworks.com/t/powershell-problems-starting-service-on-remote-computer/740065/2","author":{"@type":"Person","name":"edwineekelaers2","url":"https://community.spiceworks.com/u/edwineekelaers2"}},{"@type":"Answer","text":"
Permission issues maybe? I tested it with the w32time service as a test and you do need to run it from an elevated powershell session (run as Administrator).<\/p>\n
When your script runs do you get any output? You have a few write-host statements that should produce some output (consider using write-output which is usually more correct to use - https://www.madwithpowershell.com/2015/11/write-host-is-not-as-bad-in-powershell-5.html<\/a> )<\/p>","upvoteCount":1,"datePublished":"2019-11-20T23:59:44.000Z","url":"https://community.spiceworks.com/t/powershell-problems-starting-service-on-remote-computer/740065/3","author":{"@type":"Person","name":"gerardbeekmans","url":"https://community.spiceworks.com/u/gerardbeekmans"}},{"@type":"Answer","text":"Have you tried using something simpler like PS Tools/Sysinternals psservice tool to start/stop/query status etc?? I pair that up with powershell scripts and it makes my life a LOT easier<\/p>\n