Quite often, I will find a computer where the service for one of our screen viewing software is having troubles. I have a batch file to fix it, but just trying to speed the whole process up as noticed Powershell accomplishes this faster.<\/p>\n
I first tried stopping the service and then restarting it. But I know sometimes the service has just stopped, so all it needs is to be started. After a quick bit of research I found a script online that checks to see if a specific service is running or not, if it isn’t it’ll start it and if it is running already it displays a message on screen saying it was already running.<\/p>\n
I need to be able to restart the service if it’s running, but just start it if it’s not running. I altered the script slightly and got this;<\/p>\n
$svc = get-service -name icas -computername 70-01\nif ($svc.Status -eq \"Stopped\") {$svc.start()} {Write-Host \"Service was stopped but is now starting\"}\nelseif ($svc.status -eq \"Running\") {$svc.stop()} {$svc.start()} {Write-Host \"Service restarted\"}\n<\/code><\/pre>\nBut the problem is, I’m getting this error;<\/p>\n
elseif : The term 'elseif' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.\nAt line:3 char:1\n+ elseif ($svc.status -eq \"Running\") {$svc.stop()} {$svc.start()} {Writ ...\n+ ~~~~~~\n + CategoryInfo : ObjectNotFound: (elseif:String) [], CommandNotFoundException\n + FullyQualifiedErrorId : CommandNotFoundException\n<\/code><\/pre>\nAfter some more research online, most of the time this error is apparently due to an incorrect/missing bracket, but everything looks fine to me.<\/p>","upvoteCount":3,"answerCount":17,"datePublished":"2018-02-28T14:59:35.000Z","author":{"@type":"Person","name":"craig582","url":"https://community.spiceworks.com/u/craig582"},"acceptedAnswer":{"@type":"Answer","text":"\n\n
<\/div>\n
Craig582:<\/div>\n
\nThe script works most of the time, but when it doesn’t work, I get the error saying it can’t start the service after stopping<\/p>\n<\/blockquote>\n<\/aside>\n
Did you verify it stopped?<\/p>\n
Some services take a while to stop, and if you try starting it in between, it will not be able to start it if it is still stopping<\/p>\n
Maybe put a loop in that waits for the service to stop for sure?<\/p>\n
$host.ui.RawUI.WindowTitle = \"Restarting the iTALC Service on $(Computer:TARGET)\"\n$svc = get-service -name icas -computername '$(Computer:TARGET)'\n\nif ($svc.Status -eq \"Stopped\") {\n Start-Service -Name icas -Verbose -Comp\n Write-Host \"Serviced started\"\n} \nelseif ($svc.status -eq \"Running\") {\n get-service -name icas -computername '$(Computer:TARGET)' | stop-Service -Force\n Write-Host \"Service Stopped\"\n do {\n $svc = get-service -name icas -computername '$(Computer:TARGET)'\n Start-sleep -seconds 5\n }until($svc.status -eq 'stopped')\n get-service -name icas -computername '$(Computer:TARGET)' | Start-Service -Verbose\n Write-Host \"Service Restarted\"\n}\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2018-03-15T14:26:17.000Z","url":"https://community.spiceworks.com/t/restart-service-via-powershell-having-issues/637507/16","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},"suggestedAnswer":[{"@type":"Answer","text":"Quite often, I will find a computer where the service for one of our screen viewing software is having troubles. I have a batch file to fix it, but just trying to speed the whole process up as noticed Powershell accomplishes this faster.<\/p>\n
I first tried stopping the service and then restarting it. But I know sometimes the service has just stopped, so all it needs is to be started. After a quick bit of research I found a script online that checks to see if a specific service is running or not, if it isn’t it’ll start it and if it is running already it displays a message on screen saying it was already running.<\/p>\n
I need to be able to restart the service if it’s running, but just start it if it’s not running. I altered the script slightly and got this;<\/p>\n
$svc = get-service -name icas -computername 70-01\nif ($svc.Status -eq \"Stopped\") {$svc.start()} {Write-Host \"Service was stopped but is now starting\"}\nelseif ($svc.status -eq \"Running\") {$svc.stop()} {$svc.start()} {Write-Host \"Service restarted\"}\n<\/code><\/pre>\nBut the problem is, I’m getting this error;<\/p>\n
elseif : The term 'elseif' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.\nAt line:3 char:1\n+ elseif ($svc.status -eq \"Running\") {$svc.stop()} {$svc.start()} {Writ ...\n+ ~~~~~~\n + CategoryInfo : ObjectNotFound: (elseif:String) [], CommandNotFoundException\n + FullyQualifiedErrorId : CommandNotFoundException\n<\/code><\/pre>\nAfter some more research online, most of the time this error is apparently due to an incorrect/missing bracket, but everything looks fine to me.<\/p>","upvoteCount":3,"datePublished":"2018-02-28T14:59:35.000Z","url":"https://community.spiceworks.com/t/restart-service-via-powershell-having-issues/637507/1","author":{"@type":"Person","name":"craig582","url":"https://community.spiceworks.com/u/craig582"}},{"@type":"Answer","text":"
No, it looks definitely weird.<\/p>\n
write it out , no compact as you did, then you see they are weird, I assume they are supposed to be all in the same loop?<\/p>\n
Also why did you use elseif and not just else if you just have 2 conditions?<\/p>\n
$svc = get-service -name icas -computername '70-01'\n\nif ($svc.Status -eq \"Stopped\") {\n $svc.start()\n} \n\n{Write-Host \"Service was stopped but is now starting\"}\n\nelseif ($svc.status -eq \"Running\") {\n $svc.stop()\n} \n\n{$svc.start()} {Write-Host \"Service restarted\"}\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2018-02-28T15:04:03.000Z","url":"https://community.spiceworks.com/t/restart-service-via-powershell-having-issues/637507/2","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"Change this:<\/p>\n
$svc = get-service -name icas -computername 70-01 \nif ($svc.Status -eq \"Stopped\") {$svc.start()} {Write-Host \"Service was stopped but is now starting\"} \nelseif ($svc.status -eq \"Running\") {$svc.stop()} {$svc.start()} {Write-Host \"Service restarted\"} \n<\/code><\/pre>\nto this:<\/p>\n
$svc = get-service -name icas -computername 70-01 \nif ($svc.Status -eq \"Stopped\") {$svc.start(); Write-Host \"Service was stopped but is now starting\"} \nelseif ($svc.status -eq \"Running\") {$svc.stop(); $svc.start(); Write-Host \"Service restarted\"}\n<\/code><\/pre>","upvoteCount":2,"datePublished":"2018-02-28T15:04:31.000Z","url":"https://community.spiceworks.com/t/restart-service-via-powershell-having-issues/637507/3","author":{"@type":"Person","name":"shaneoneill","url":"https://community.spiceworks.com/u/shaneoneill"}},{"@type":"Answer","text":"It should be like this:<\/p>\n
$svc = get-service -name icas -computername '70-01'\n\nif ($svc.Status -eq \"Stopped\") {\n $svc.start()\n Write-Host \"Service was stopped but is now starting\"\n} \nelseif ($svc.status -eq \"Running\") {\n $svc.stop()\n Start-sleep -seconds 5 # idk just giving it more time, not needed I guess\n $svc.start()\n Write-Host \"Service restarted\"\n}\n<\/code><\/pre>","upvoteCount":2,"datePublished":"2018-02-28T15:07:55.000Z","url":"https://community.spiceworks.com/t/restart-service-via-powershell-having-issues/637507/4","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"I see too many curly brackets<\/p>\n
$svc = get-service -name icas -computername 70-01 |select Name,displayname,status\nif($svc.Status -eq \"Stopped\") \n{\nStart-Service $svc.name\n Write-Host \"Service was stopped but is now starting\" -ForegroundColor Green\n}\nElseIf($svc.status -eq \"Running\") {\nRestart-Service $svc.name -Force -Verbose\nWrite-Host \"Service restarted\"\n}\n\n<\/code><\/pre>","upvoteCount":4,"datePublished":"2018-02-28T15:16:10.000Z","url":"https://community.spiceworks.com/t/restart-service-via-powershell-having-issues/637507/5","author":{"@type":"Person","name":"jitensh","url":"https://community.spiceworks.com/u/jitensh"}},{"@type":"Answer","text":"\n\n
<\/div>\n
JitenSh:<\/div>\n
\nI see too many curly brackets<\/p>\n
$svc = get-service -name icas -computername 70-01 |select Name,displayname,status\nif($svc.Status -eq \"Stopped\") \n{\nStart-Service $svc.name\n Write-Host \"Service was stopped but is now starting\" -ForegroundColor Green\n}\nElseIf($svc.status -eq \"Running\") {\nRestart-Service $svc.name -Force -Verbose\nWrite-Host \"Service restarted\"\n}\n\n<\/code><\/pre>\n<\/blockquote>\n<\/aside>\nThanks, but I’m getting this error message;<\/p>\n
Restart-Service : Service 'iTALC Client (icas)' cannot be stopped due to the following error: Cannot open icas service on computer '.'.\nAt C:\\Users\\whettonc\\Desktop\\Untitled1.ps1:8 char:1\n+ Restart-Service $svc.name -Force -Verbose\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n + CategoryInfo : CloseError: (System.ServiceProcess.ServiceController:ServiceController) [Restart-Service], ServiceCommandException\n + FullyQualifiedErrorId : CouldNotStopService,Microsoft.PowerShell.Commands.RestartServiceCommand\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2018-03-05T08:38:42.000Z","url":"https://community.spiceworks.com/t/restart-service-via-powershell-having-issues/637507/6","author":{"@type":"Person","name":"craig582","url":"https://community.spiceworks.com/u/craig582"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Neally:<\/div>\n
\nIt should be like this:<\/p>\n
$svc = get-service -name icas -computername '70-01'\n\nif ($svc.Status -eq \"Stopped\") {\n $svc.start()\n Write-Host \"Service was stopped but is now starting\"\n} \nelseif ($svc.status -eq \"Running\") {\n $svc.stop()\n Start-sleep -seconds 5 # idk just giving it more time, not needed I guess\n $svc.start()\n Write-Host \"Service restarted\"\n}\n<\/code><\/pre>\n<\/blockquote>\n<\/aside>\nResults with the service being stopped but not being restarted.<\/p>\n
Exception calling \"Start\" with \"0\" argument(s): \"Cannot start service icas on computer '70-01'.\"\nAt line:10 char:5\n+ $svc.start()\n+ ~~~~~~~~~~~~\n + CategoryInfo : NotSpecified: (:) [], MethodInvocationException\n + FullyQualifiedErrorId : InvalidOperationException\n<\/code><\/pre>\n\n\n
<\/div>\n
SOZDBA:<\/div>\n
\nChange this:<\/p>\n
$svc = get-service -name icas -computername 70-01 \nif ($svc.Status -eq \"Stopped\") {$svc.start()} {Write-Host \"Service was stopped but is now starting\"} \nelseif ($svc.status -eq \"Running\") {$svc.stop()} {$svc.start()} {Write-Host \"Service restarted\"} \n<\/code><\/pre>\nto this:<\/p>\n
$svc = get-service -name icas -computername 70-01 \nif ($svc.Status -eq \"Stopped\") {$svc.start(); Write-Host \"Service was stopped but is now starting\"} \nelseif ($svc.status -eq \"Running\") {$svc.stop(); $svc.start(); Write-Host \"Service restarted\"}\n<\/code><\/pre>\n<\/blockquote>\n<\/aside>\nGetting a similar error message as above and the service is also being stopped but not started again.<\/p>\n
Exception calling \"Start\" with \"0\" argument(s): \"Cannot start service icas on computer '70-01'.\"\nAt line:3 char:50\n+ elseif ($svc.status -eq \"Running\") {$svc.stop(); $svc.start(); Write- ...\n+ ~~~~~~~~~~~~\n + CategoryInfo : NotSpecified: (:) [], MethodInvocationException\n + FullyQualifiedErrorId : InvalidOperationException\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2018-03-05T08:41:22.000Z","url":"https://community.spiceworks.com/t/restart-service-via-powershell-having-issues/637507/7","author":{"@type":"Person","name":"craig582","url":"https://community.spiceworks.com/u/craig582"}},{"@type":"Answer","text":"Are you running as administrator as administrator if service is not stopping may be it has other dependencies<\/p>\n
try changing this line<\/p>\n
Restart-Service $svc.name -Force -Verbose<\/p>\n
to<\/p>\n
Stop-Service $svc.name -Force -Verbose -PassThru\nStart-Sleep -Seconds 3\nStart-Service $svc.name \n<\/code><\/pre>","upvoteCount":1,"datePublished":"2018-03-05T08:54:16.000Z","url":"https://community.spiceworks.com/t/restart-service-via-powershell-having-issues/637507/8","author":{"@type":"Person","name":"jitensh","url":"https://community.spiceworks.com/u/jitensh"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Neally:<\/div>\n
\nNo, it looks definitely weird.<\/p>\n
write it out , no compact as you did, then you see they are weird, I assume they are supposed to be all in the same loop?<\/p>\n
Also why did you use elseif and not just else if you just have 2 conditions?<\/p>\n<\/blockquote>\n<\/aside>\n
The script I edited was using elseif for just 2 conditions. Not knowing any different, I just left it with how the script originally was.<\/p>","upvoteCount":0,"datePublished":"2018-03-05T08:55:51.000Z","url":"https://community.spiceworks.com/t/restart-service-via-powershell-having-issues/637507/9","author":{"@type":"Person","name":"craig582","url":"https://community.spiceworks.com/u/craig582"}},{"@type":"Answer","text":"\n\n
<\/div>\n
JitenSh:<\/div>\n
\nAre you running as administrator as administrator if service is not stopping may be it has other dependencies<\/p>\n
try changing this line<\/p>\n
Restart-Service $svc.name -Force -Verbose<\/p>\n
to<\/p>\n
Stop-Service $svc.name -Force -Verbose -PassThru\nStart-Sleep -Seconds 3\nStart-Service $svc.name \n<\/code><\/pre>\n<\/blockquote>\n<\/aside>\nStop-Service : Service 'iTALC Client (icas)' cannot be stopped due to the following error: Cannot open icas service on computer '.'.\nAt C:\\Users\\whettonc\\Desktop\\Untitled1.ps1:8 char:1\n+ Stop-Service $svc.name -Force -Verbose -PassThru\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n + CategoryInfo : CloseError: (System.ServiceProcess.ServiceController:ServiceController) [Stop-Service], ServiceCommandException\n + FullyQualifiedErrorId : CouldNotStopService,Microsoft.PowerShell.Commands.StopServiceCommand\n \nStart-Service : Service 'iTALC Client (icas)' cannot be started due to the following error: Cannot open icas service on computer '.'.\nAt C:\\Users\\whettonc\\Desktop\\Untitled1.ps1:10 char:1\n+ Start-Service $svc.name\n+ ~~~~~~~~~~~~~~~~~~~~~~~\n + CategoryInfo : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service], ServiceCommandException\n + FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceCommand\n<\/code><\/pre>\nAbove is the error I’m getting when not running as administrator. When running as administrator I’m getting no error message, but nothing actually happens to the service.<\/p>","upvoteCount":0,"datePublished":"2018-03-05T09:10:59.000Z","url":"https://community.spiceworks.com/t/restart-service-via-powershell-having-issues/637507/10","author":{"@type":"Person","name":"craig582","url":"https://community.spiceworks.com/u/craig582"}},{"@type":"Answer","text":"
That sounds like a permission issue, does your account have rights on the remote computer?<\/p>\n
Maybe try invoke-command<\/p>","upvoteCount":0,"datePublished":"2018-03-05T14:32:25.000Z","url":"https://community.spiceworks.com/t/restart-service-via-powershell-having-issues/637507/11","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Neally:<\/div>\n
\nThat sounds like a permission issue, does your account have rights on the remote computer?<\/p>\n
Maybe try invoke-command<\/p>\n<\/blockquote>\n<\/aside>\n
Do have the permissions on the computer. Will try invoke-command<\/p>","upvoteCount":0,"datePublished":"2018-03-06T08:19:44.000Z","url":"https://community.spiceworks.com/t/restart-service-via-powershell-having-issues/637507/12","author":{"@type":"Person","name":"craig582","url":"https://community.spiceworks.com/u/craig582"}},{"@type":"Answer","text":"
Tried the very basic way of doing this via PowerShell, which is how I was originally doing it, and this does work.<\/p>\n
Get-Service -Name icas -ComputerName 70-01 | Set-Service -Status stopped\nGet-Service -Name icas -ComputerName 70-01 | Set-Service -Status running\n<\/code><\/pre>\nTry to add in write-host screws the whole thing and it doesn’t work.<\/p>\n
Tried Neally’s script again without write-host and it worked the first couple of times that I tested it, and then it went back to just stopping the service (when running again it worked to start the service). When I came back to this a few minutes later, it worked again - perhaps I was trying to do it too often.<\/p>\n
Anyone any clue why write-host screws everything up?<\/p>","upvoteCount":0,"datePublished":"2018-03-06T10:19:22.000Z","url":"https://community.spiceworks.com/t/restart-service-via-powershell-having-issues/637507/13","author":{"@type":"Person","name":"craig582","url":"https://community.spiceworks.com/u/craig582"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Craig582:<\/div>\n
\nAnyone any clue why write-host screws everything up?<\/p>\n<\/blockquote>\n<\/aside>\n
It should not, as it just draws pixels on your local console.<\/p>\n
What if you give it more time between the stop and start, and instead of a restart you stop and start it.<\/p>\n
stop-service $service -verbose\nstart-sleep -seconds 10\nstart-service $service -verbose\n<\/code><\/pre>\nI’ve also seen services that are having issues stopping because they are stuck and you have to use the ‘-force’ switch or even kill the process rather. ¯_(ツ)_/¯<\/p>","upvoteCount":1,"datePublished":"2018-03-06T14:21:24.000Z","url":"https://community.spiceworks.com/t/restart-service-via-powershell-having-issues/637507/14","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
Been really busy these last few days so have not had a chance to reply sooner.<\/p>\n
This is the script I am currently using:<\/p>\n
$host.ui.RawUI.WindowTitle = \"Restarting the iTALC Service on $(Computer:TARGET)\"\n$svc = get-service -name icas -computername '$(Computer:TARGET)'\n\nif ($svc.Status -eq \"Stopped\") {\n $svc.start() \n Write-Host \"Serviced started\"\n} \nelseif ($svc.status -eq \"Running\") {\n $svc.stop()\n Write-Host \"Service Stopped\"\n Start-sleep -seconds 3\n $svc.start()\n Write-Host \"Service Restarted\"\n}\n<\/code><\/pre>\nThe $(Computer:TARGET) is a variable in PDQ Inventory (which is where I’m running the script from).<\/p>\n
The script works most of the time, but when it doesn’t work, I get the error saying it can’t start the service after stopping it.I’ve tried running it as administrator, but it doesn’t seem to change the result.<\/p>\n
I just ran the script on 4 PCs that were having trouble, and it failed to start the service on all of them after stopping. I’d never run the script before on these PCs.<\/p>\n
If the service is stopped before running the script, the script will successfully start the service 100% of the time.<\/p>\n
Really scratching my head over here.<\/p>","upvoteCount":0,"datePublished":"2018-03-09T09:07:47.000Z","url":"https://community.spiceworks.com/t/restart-service-via-powershell-having-issues/637507/15","author":{"@type":"Person","name":"craig582","url":"https://community.spiceworks.com/u/craig582"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Neally:<\/div>\n
\n\n\n
<\/div>\n
Craig582:<\/div>\n
\nThe script works most of the time, but when it doesn’t work, I get the error saying it can’t start the service after stopping<\/p>\n<\/blockquote>\n<\/aside>\n
Did you verify it stopped?<\/p>\n
Some services take a while to stop, and if you try starting it in between, it will not be able to start it if it is still stopping<\/p>\n
Maybe put a loop in that waits for the service to stop for sure?<\/p>\n
$host.ui.RawUI.WindowTitle = \"Restarting the iTALC Service on $(Computer:TARGET)\"\n$svc = get-service -name icas -computername '$(Computer:TARGET)'\n\nif ($svc.Status -eq \"Stopped\") {\n Start-Service -Name icas -Verbose -Comp\n Write-Host \"Serviced started\"\n} \nelseif ($svc.status -eq \"Running\") {\n get-service -name icas -computername '$(Computer:TARGET)' | stop-Service -Force\n Write-Host \"Service Stopped\"\n do {\n $svc = get-service -name icas -computername '$(Computer:TARGET)'\n Start-sleep -seconds 5\n }until($svc.status -eq 'stopped')\n get-service -name icas -computername '$(Computer:TARGET)' | Start-Service -Verbose\n Write-Host \"Service Restarted\"\n}\n<\/code><\/pre>\n<\/blockquote>\n<\/aside>\nI must admit, I never did confirm that the service had actually stopped.<\/p>\n
I will try this and hopefully, all will be good!<\/p>","upvoteCount":0,"datePublished":"2018-03-16T10:08:25.000Z","url":"https://community.spiceworks.com/t/restart-service-via-powershell-having-issues/637507/17","author":{"@type":"Person","name":"craig582","url":"https://community.spiceworks.com/u/craig582"}}]}}
craig582
(Craig582)
February 28, 2018, 2:59pm
1
Quite often, I will find a computer where the service for one of our screen viewing software is having troubles. I have a batch file to fix it, but just trying to speed the whole process up as noticed Powershell accomplishes this faster.
I first tried stopping the service and then restarting it. But I know sometimes the service has just stopped, so all it needs is to be started. After a quick bit of research I found a script online that checks to see if a specific service is running or not, if it isn’t it’ll start it and if it is running already it displays a message on screen saying it was already running.
I need to be able to restart the service if it’s running, but just start it if it’s not running. I altered the script slightly and got this;
$svc = get-service -name icas -computername 70-01
if ($svc.Status -eq "Stopped") {$svc.start()} {Write-Host "Service was stopped but is now starting"}
elseif ($svc.status -eq "Running") {$svc.stop()} {$svc.start()} {Write-Host "Service restarted"}
But the problem is, I’m getting this error;
elseif : The term 'elseif' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:3 char:1
+ elseif ($svc.status -eq "Running") {$svc.stop()} {$svc.start()} {Writ ...
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (elseif:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
After some more research online, most of the time this error is apparently due to an incorrect/missing bracket, but everything looks fine to me.
3 Spice ups
Neally
(Neally)
February 28, 2018, 3:04pm
2
No, it looks definitely weird.
write it out , no compact as you did, then you see they are weird, I assume they are supposed to be all in the same loop?
Also why did you use elseif and not just else if you just have 2 conditions?
$svc = get-service -name icas -computername '70-01'
if ($svc.Status -eq "Stopped") {
$svc.start()
}
{Write-Host "Service was stopped but is now starting"}
elseif ($svc.status -eq "Running") {
$svc.stop()
}
{$svc.start()} {Write-Host "Service restarted"}
1 Spice up
Change this:
$svc = get-service -name icas -computername 70-01
if ($svc.Status -eq "Stopped") {$svc.start()} {Write-Host "Service was stopped but is now starting"}
elseif ($svc.status -eq "Running") {$svc.stop()} {$svc.start()} {Write-Host "Service restarted"}
to this:
$svc = get-service -name icas -computername 70-01
if ($svc.Status -eq "Stopped") {$svc.start(); Write-Host "Service was stopped but is now starting"}
elseif ($svc.status -eq "Running") {$svc.stop(); $svc.start(); Write-Host "Service restarted"}
2 Spice ups
Neally
(Neally)
February 28, 2018, 3:07pm
4
It should be like this:
$svc = get-service -name icas -computername '70-01'
if ($svc.Status -eq "Stopped") {
$svc.start()
Write-Host "Service was stopped but is now starting"
}
elseif ($svc.status -eq "Running") {
$svc.stop()
Start-sleep -seconds 5 # idk just giving it more time, not needed I guess
$svc.start()
Write-Host "Service restarted"
}
2 Spice ups
jitensh
(JitenSh)
February 28, 2018, 3:16pm
5
I see too many curly brackets
$svc = get-service -name icas -computername 70-01 |select Name,displayname,status
if($svc.Status -eq "Stopped")
{
Start-Service $svc.name
Write-Host "Service was stopped but is now starting" -ForegroundColor Green
}
ElseIf($svc.status -eq "Running") {
Restart-Service $svc.name -Force -Verbose
Write-Host "Service restarted"
}
4 Spice ups
craig582
(Craig582)
March 5, 2018, 8:38am
6
JitenSh:
I see too many curly brackets
$svc = get-service -name icas -computername 70-01 |select Name,displayname,status
if($svc.Status -eq "Stopped")
{
Start-Service $svc.name
Write-Host "Service was stopped but is now starting" -ForegroundColor Green
}
ElseIf($svc.status -eq "Running") {
Restart-Service $svc.name -Force -Verbose
Write-Host "Service restarted"
}
Thanks, but I’m getting this error message;
Restart-Service : Service 'iTALC Client (icas)' cannot be stopped due to the following error: Cannot open icas service on computer '.'.
At C:\Users\whettonc\Desktop\Untitled1.ps1:8 char:1
+ Restart-Service $svc.name -Force -Verbose
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (System.ServiceProcess.ServiceController:ServiceController) [Restart-Service], ServiceCommandException
+ FullyQualifiedErrorId : CouldNotStopService,Microsoft.PowerShell.Commands.RestartServiceCommand
craig582
(Craig582)
March 5, 2018, 8:41am
7
Neally:
It should be like this:
$svc = get-service -name icas -computername '70-01'
if ($svc.Status -eq "Stopped") {
$svc.start()
Write-Host "Service was stopped but is now starting"
}
elseif ($svc.status -eq "Running") {
$svc.stop()
Start-sleep -seconds 5 # idk just giving it more time, not needed I guess
$svc.start()
Write-Host "Service restarted"
}
Results with the service being stopped but not being restarted.
Exception calling "Start" with "0" argument(s): "Cannot start service icas on computer '70-01'."
At line:10 char:5
+ $svc.start()
+ ~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : InvalidOperationException
SOZDBA:
Change this:
$svc = get-service -name icas -computername 70-01
if ($svc.Status -eq "Stopped") {$svc.start()} {Write-Host "Service was stopped but is now starting"}
elseif ($svc.status -eq "Running") {$svc.stop()} {$svc.start()} {Write-Host "Service restarted"}
to this:
$svc = get-service -name icas -computername 70-01
if ($svc.Status -eq "Stopped") {$svc.start(); Write-Host "Service was stopped but is now starting"}
elseif ($svc.status -eq "Running") {$svc.stop(); $svc.start(); Write-Host "Service restarted"}
Getting a similar error message as above and the service is also being stopped but not started again.
Exception calling "Start" with "0" argument(s): "Cannot start service icas on computer '70-01'."
At line:3 char:50
+ elseif ($svc.status -eq "Running") {$svc.stop(); $svc.start(); Write- ...
+ ~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : InvalidOperationException
jitensh
(JitenSh)
March 5, 2018, 8:54am
8
Are you running as administrator as administrator if service is not stopping may be it has other dependencies
try changing this line
Restart-Service $svc.name -Force -Verbose
to
Stop-Service $svc.name -Force -Verbose -PassThru
Start-Sleep -Seconds 3
Start-Service $svc.name
1 Spice up
craig582
(Craig582)
March 5, 2018, 8:55am
9
Neally:
No, it looks definitely weird.
write it out , no compact as you did, then you see they are weird, I assume they are supposed to be all in the same loop?
Also why did you use elseif and not just else if you just have 2 conditions?
The script I edited was using elseif for just 2 conditions. Not knowing any different, I just left it with how the script originally was.
craig582
(Craig582)
March 5, 2018, 9:10am
10
JitenSh:
Are you running as administrator as administrator if service is not stopping may be it has other dependencies
try changing this line
Restart-Service $svc.name -Force -Verbose
to
Stop-Service $svc.name -Force -Verbose -PassThru
Start-Sleep -Seconds 3
Start-Service $svc.name
Stop-Service : Service 'iTALC Client (icas)' cannot be stopped due to the following error: Cannot open icas service on computer '.'.
At C:\Users\whettonc\Desktop\Untitled1.ps1:8 char:1
+ Stop-Service $svc.name -Force -Verbose -PassThru
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (System.ServiceProcess.ServiceController:ServiceController) [Stop-Service], ServiceCommandException
+ FullyQualifiedErrorId : CouldNotStopService,Microsoft.PowerShell.Commands.StopServiceCommand
Start-Service : Service 'iTALC Client (icas)' cannot be started due to the following error: Cannot open icas service on computer '.'.
At C:\Users\whettonc\Desktop\Untitled1.ps1:10 char:1
+ Start-Service $svc.name
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service], ServiceCommandException
+ FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceCommand
Above is the error I’m getting when not running as administrator. When running as administrator I’m getting no error message, but nothing actually happens to the service.
Neally
(Neally)
March 5, 2018, 2:32pm
11
That sounds like a permission issue, does your account have rights on the remote computer?
Maybe try invoke-command
craig582
(Craig582)
March 6, 2018, 8:19am
12
Do have the permissions on the computer. Will try invoke-command
craig582
(Craig582)
March 6, 2018, 10:19am
13
Tried the very basic way of doing this via PowerShell, which is how I was originally doing it, and this does work.
Get-Service -Name icas -ComputerName 70-01 | Set-Service -Status stopped
Get-Service -Name icas -ComputerName 70-01 | Set-Service -Status running
Try to add in write-host screws the whole thing and it doesn’t work.
Tried Neally’s script again without write-host and it worked the first couple of times that I tested it, and then it went back to just stopping the service (when running again it worked to start the service). When I came back to this a few minutes later, it worked again - perhaps I was trying to do it too often.
Anyone any clue why write-host screws everything up?
Neally
(Neally)
March 6, 2018, 2:21pm
14
It should not, as it just draws pixels on your local console.
What if you give it more time between the stop and start, and instead of a restart you stop and start it.
stop-service $service -verbose
start-sleep -seconds 10
start-service $service -verbose
I’ve also seen services that are having issues stopping because they are stuck and you have to use the ‘-force’ switch or even kill the process rather. ¯_(ツ)_/¯
1 Spice up
craig582
(Craig582)
March 9, 2018, 9:07am
15
Been really busy these last few days so have not had a chance to reply sooner.
This is the script I am currently using:
$host.ui.RawUI.WindowTitle = "Restarting the iTALC Service on $(Computer:TARGET)"
$svc = get-service -name icas -computername '$(Computer:TARGET)'
if ($svc.Status -eq "Stopped") {
$svc.start()
Write-Host "Serviced started"
}
elseif ($svc.status -eq "Running") {
$svc.stop()
Write-Host "Service Stopped"
Start-sleep -seconds 3
$svc.start()
Write-Host "Service Restarted"
}
The $(Computer:TARGET) is a variable in PDQ Inventory (which is where I’m running the script from).
The script works most of the time, but when it doesn’t work, I get the error saying it can’t start the service after stopping it.I’ve tried running it as administrator, but it doesn’t seem to change the result.
I just ran the script on 4 PCs that were having trouble, and it failed to start the service on all of them after stopping. I’d never run the script before on these PCs.
If the service is stopped before running the script, the script will successfully start the service 100% of the time.
Really scratching my head over here.
Neally
(Neally)
March 15, 2018, 2:26pm
16
Did you verify it stopped?
Some services take a while to stop, and if you try starting it in between, it will not be able to start it if it is still stopping
Maybe put a loop in that waits for the service to stop for sure?
$host.ui.RawUI.WindowTitle = "Restarting the iTALC Service on $(Computer:TARGET)"
$svc = get-service -name icas -computername '$(Computer:TARGET)'
if ($svc.Status -eq "Stopped") {
Start-Service -Name icas -Verbose -Comp
Write-Host "Serviced started"
}
elseif ($svc.status -eq "Running") {
get-service -name icas -computername '$(Computer:TARGET)' | stop-Service -Force
Write-Host "Service Stopped"
do {
$svc = get-service -name icas -computername '$(Computer:TARGET)'
Start-sleep -seconds 5
}until($svc.status -eq 'stopped')
get-service -name icas -computername '$(Computer:TARGET)' | Start-Service -Verbose
Write-Host "Service Restarted"
}
1 Spice up
craig582
(Craig582)
March 16, 2018, 10:08am
17
Neally:
Did you verify it stopped?
Some services take a while to stop, and if you try starting it in between, it will not be able to start it if it is still stopping
Maybe put a loop in that waits for the service to stop for sure?
$host.ui.RawUI.WindowTitle = "Restarting the iTALC Service on $(Computer:TARGET)"
$svc = get-service -name icas -computername '$(Computer:TARGET)'
if ($svc.Status -eq "Stopped") {
Start-Service -Name icas -Verbose -Comp
Write-Host "Serviced started"
}
elseif ($svc.status -eq "Running") {
get-service -name icas -computername '$(Computer:TARGET)' | stop-Service -Force
Write-Host "Service Stopped"
do {
$svc = get-service -name icas -computername '$(Computer:TARGET)'
Start-sleep -seconds 5
}until($svc.status -eq 'stopped')
get-service -name icas -computername '$(Computer:TARGET)' | Start-Service -Verbose
Write-Host "Service Restarted"
}
I must admit, I never did confirm that the service had actually stopped.
I will try this and hopefully, all will be good!