Restart server and poll for started service - powershell

If I was to create a script to restart a server and wait for a started service SERVICE123 - NODE1 to come back up, how would I do this?
I have the following:
Restart-Computer -ComputerName COMPUTER123456 wait -For SERVICE123 - NODE1 -Timeout 400 -Delay 5
If not, is there a script I could use to poll for a started service only? I would like it to sit for 20 minutes or so until this service node is up and running.

To see if a Service is running you can use the invoke Command. And execute the
cmdlet get-service on the machine.
$service = Invoke-Command -ComputerName COMPUTER123456 -ScriptBlock { Get-Service -Name $args[0] } -ArgumentList "Service123"
via $service.Status you can see if the Service is Running or Stopped.
Normaly it should work with your code and the restart-Computer cmdlet,
what is the error, is there any error given. Why does your code not work?
I have an idea, you have to put the hyphen to the parameter 'wait' = -Wait
Restart-Computer -ComputerName COMPUTER123456 -Wait -For "SERVICE123 - NODE1" -Timeout 400 -Delay 5
Have a look at MSTechnet
In example 6 you see exactly what you need.
Greets Eldo.Ob

Related

Unable to restart server after it joins the domain

Morning guys,
I'm running into an issue where I have a script that joins a Server to a Domain and restarts, intalls it's roles/features, etc and then restarts it again. I don't have an issue with the first restart:
Restart-Computer -ComputerName $IP -Credential $AdminCred -Wait -For PowerShell
but when I try to do the second restart at the end of the script it get the following error
Restart-Computer : The computer is skipped. Fail to retrieve its LastBootUpTime via the WMI service with the
following error message: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).
The following is the original code I tried
Restart-Computer -ComputerName $HostName -Wait -For PowerShell
Without credentials, as I expect Kerberos to work as the account from the laptop has proper permissions
but I also ran it with -Credential and same error. Then I tried changing $HostName to $IP and still no luck.
I can get around the error, by enclosing the Restart-Computer command into an invoke-command session but then I can't "wait for powershell" unless I set an arbitrary sleep timer for a couple minutes.
Any Ideas are appreciated!
I figured it out. I had to add the -WsmanAuthentication param and specify Kerberos. Final code
Restart-Computer -ComputerName $HostName -WsmanAuthentication Kerberos -Wait -For PowerShell

Is there a way to warn remote computer of pending restart AND have my script wait for the restart to complete?

I'm looking to combine the "You will be logged off in x minutes" functionality of 'shutdown.exe", and the "-Wait -For PowerShell" functionality of the Restart-Computer cmdlet.
I have a script that requires a remote computer to restart. I'd like to warn the user of the remote computer that their computer will restart in 5 minutes. I would also like my script to wait until the restart completes before continuing forward.
Shutdown.exe has a great warning system and delay built in, but no great way to have the script wait for the reboot to complete. I've tried a loop that waits until Test-Connection is $true, but of course a computer returns a ping before it can accept PowerShell commands like Invoke-Command. I could slap a Start-Sleep on the end, but the time between ping and PowerShell accepting commands varies per computer.
Restart-Computer has a great "wait for PowerShell" feature, but there's no way to warn the user of the remote computer that their computer is about to restart. I've tried running the commands below, but the restart-computer cmdlet will throw an error if there's already a shutdown in progress.
shutdown /r /t 300 /m \\computer $system
restart-computer -computername $system -Force -Wait -For PowerShell -Timeout 300 -Delay 2
You can try just sending the message just before your call to Restart-Computer using the msg command:
msg * /SERVER:$system /TIME:300 "Computer will be restarted in 5 minutes."
restart-computer -computername $system -Force -Wait -For PowerShell -Delay 2
I won't say this is a complete answer to your question, but instead of Test-NetConnection (which only tests ping/TCP as you mentioned), use Test-WSMan which will test the protocol over which PowerShell Remoting operates (note other protocols are possible, but you're likely just using this one in your environment).

PowerShell Stopping and Starting Windows Services on Remote Computer

I have a very basic PowerShell that accepts a list of Windows Server and Services on them and then stops the Services
Get-Service -ComputerName machine1 -Name service1 -ErrorAction Stop | Stop-Service -WarningAction SilentlyContinue
Although I had explicitly mentioned -ErrorAction as Stop, in a case if one of the Server is not reachable, the Powershell doesn't come back. How do I change this behaviour at least in a way to stop processing after a certain amount of time as "n" secs which again can be passed thru parameter?
Incorporating both comments, we end up with following
Enclose the current script in a Test-Connection that allows you to first verify if a computer is actually reachable:
if (Test-Connection -ComputerName machine1 -Quiet)
{
all your current code
}

Set-Service control actions

I am using below command to start the windows service. I need stop the execution of this command if it doesn't start before 5 or 10 and continue with next commands in script. Can you please help me how do it?
Get-Service -ComputerName “$server_name” -Name “XHedge.Execution-NYTRADER” |
Set-Service -Status Running

Powershell get-service status not working as scheduled task

I'm running this code below as a function to iterate through a list of servers to check if services are running. This works when run manually. When run from a scheduled task the code $arrService.Status translates to the service name. I even tried something like Get-Service -Computer $Server -display $ServiceName | Select -expandproperty Status but that returns empty string or no value.
Any idea why the status property would not work when run from a scheduled task?
function CheckService{
param($Server, $ServiceName)
$arrService = Get-Service -Computer $Server -display $ServiceName
if ($arrService.Status -ne "Running"){
Start-Service -InputObject $ServerService
}
}
Thanks to #djs I was able to find a solution, albeit not a fantastic solution. Adding the account to the admin group of each server allows the commands to execute. In our case this isn't a problem so we are going to go with this solution.