Getting error in my script

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Getting error in my script



I keep getting an error message stating


"Exception calling "Start" with "0" argument(s): "Cannot start service RTCRGS on computer 'CORPxxxxxxx.xxxx.net'."
At line:25 char:18
+ $_.Start <<<< ()
+ CategoryInfo : NotSpecified: (:) , MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException"



Any suggestion on why this happens and how to fix it?


$services = "RTCRGS"
$SvrName = 'CORPxxxx.xxxx.xxxx'
Get-Service -ComputerName $SvrName -Name $services | %
Write-host "$($_.Name) on $SvrName is $($_.Status)"
if ($_.Status -eq 'stopped')
Write-Host "Starting $($_.Name) in 5 sec..."
Start-Sleep 5
Write-Host "$($_.Name) is Starting, please wait..."
$_.Start()
Write-Host "Checking status of service, please wait for 25 sec..."
Start-Sleep 20
Write-Host "$($_.Name) on $SvrName is $($_.Status)"
elseif ($_.Status -eq 'running')
Write-Host "Restaring $($_.Name) in 5 sec..."
Start-Sleep 5
$_.Stop()
Start-Sleep 5
Write-Host "$($_.Name) is Starting, please wait..."
$_.Start()
Write-Host "Checking status of service, please wait for 25 sec..."
Start-Sleep 25
Write-Host "$($_.Name) on $SvrName is $($_.Status)"






Is there an issue with the service on the target server? i.e. can you manually stop/start it? (i.e. it may be disabled). Some services may need to be manually started a different way.
– HAL9256
Aug 10 at 20:20





I concur. Have you tried using Start-Service with the -ComputerName parameter?
– TheIncorrigible1
Aug 10 at 20:23


Start-Service


-ComputerName





Which $_.Start() line is it failing on? if it is the second one, you need to add a check and wait loop before trying to restart it. i.e. the service may still be in a "Stopping" state and would error out when trying to restart. (i.e. 5 seconds likely is not enough time for the service to fully stop)
– HAL9256
Aug 10 at 20:25


$_.Start()





@HAL9256 Thank you for the help, amazing suggestion on the time and loop!!! :)
– Sammy90
Aug 13 at 13:52




1 Answer
1



If the service is NOT disabled (and you have checked you can restart it manually) you might be better of with something like this


$services = "RTCRGS"
$SvrName = 'CORPxxxx.xxxx.xxxx'
Get-Service -ComputerName $SvrName -Name $services | ForEach-Object
Write-host "$($_.Name) on $SvrName is $($_.Status)"

if (@("Stopped","StopPending","Paused","PausePending") -contains $_.Status)
Write-Host "Starting service $($_.Name), please wait..."
$_.Start()

else
Write-Host "Stopping service $($_.Name)..."
$_.Stop()
# as HAL9256 suggested, perform a loop to see if service has really stopped
do
# recheck the ACTUAL status, not the status you captured before
$newStatus = (Get-Service -ComputerName $SvrName -Name $_.Name).Status
while ($newStatus -ne "Stopped")
Write-Host "$($_.Name) is Restarting, please wait..."
$_.Start()


Write-Host "Checking status of service, please wait for 25 sec..."
Start-Sleep 25

# here again you should recheck the ACTUAL status, not the status you captured before
$newStatus = (Get-Service -ComputerName $SvrName -Name $_.Name).Status
Write-Host "$($_.Name) on $SvrName is $newStatus"



You can read the possible values for 'Status' here





Ah.. and i see you have asked about the status before
– Theo
Aug 11 at 12:36





You sir are a life saver, thank you so much !!! :)
– Sammy90
Aug 13 at 13:51






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard