Kelly Garcia
asked on
PowerShell Restart Servers Script
Hi Guys,
I have have this server restart script:
$header = "server"
$servers = import-csv c:\path\to\ServersToReboot .csv -header $header
foreach($i in $servers) {
Write-Host "Attempting to reboot:" $i.server "..."
$serverObj = gwmi Win32_operatingsystem -computer $i.server
$status = $serverObj.reboot()
if ($status.ReturnValue = "0") {
Write-Host "Reboot successful."
} else {
Write-Host "Reboot failed. Check permissions."
}
}
However I need to reboot servers one at time, when one server comes back up then to reboot the next one, how can I do this?
Please help!
Thank you in advance.
I have have this server restart script:
$header = "server"
$servers = import-csv c:\path\to\ServersToReboot
foreach($i in $servers) {
Write-Host "Attempting to reboot:" $i.server "..."
$serverObj = gwmi Win32_operatingsystem -computer $i.server
$status = $serverObj.reboot()
if ($status.ReturnValue = "0") {
Write-Host "Reboot successful."
} else {
Write-Host "Reboot failed. Check permissions."
}
}
However I need to reboot servers one at time, when one server comes back up then to reboot the next one, how can I do this?
Please help!
Thank you in advance.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks Dan, Problem is that I need confirmation that my server is back online sometimes on my network the VM doesn't pick up an ip address so it requires another reboot.
If there was a way to ping the server to see that its back online, if it is then to continue the loop of restarting the other servers, hope this makes sense.
what does the -quite switch mean?
If there was a way to ping the server to see that its back online, if it is then to continue the loop of restarting the other servers, hope this makes sense.
what does the -quite switch mean?
ASKER
Also is there a way to check if there are any users logged in?
Test-Connection pings the server :)
See here: http://technet.microsoft.com/en-us/library/hh849808.aspx
-Quiet will disable output and simply return True if the ping succeeded and False if not.
So this:
while (!(Test-Connection -ComputerName $i -Quiet))
will be true as long as the $i server does not anwer the ping. ! negates.
See here: http://technet.microsoft.com/en-us/library/hh849808.aspx
-Quiet will disable output and simply return True if the ping succeeded and False if not.
So this:
while (!(Test-Connection -ComputerName $i -Quiet))
will be true as long as the $i server does not anwer the ping. ! negates.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks dan much appreciated, what I am thinking is to send an email for servers that fail the ping :)
So is the while (!(Test-Connection -ComputerName $i -Quiet)) only once inside the for loop, ow will it continue sending me emails until its back up?
So is the while (!(Test-Connection -ComputerName $i -Quiet)) only once inside the for loop, ow will it continue sending me emails until its back up?
ASKER
Hi Dan,
that's my script so far will it work?
The idea is to reboot a server, then wait 5 minutes and then do a ping If that fails it will send us an email saying it failed and then continue rebooting the other servers.
Thank you soo much dan.
that's my script so far will it work?
$header = "server"
$servers = import-csv c:\servers.csv -header $header
foreach($i in $servers) {
Write-Host "Attempting to reboot:" $i.server "..."
$serverObj = gwmi Win32_operatingsystem -computer $i.server
$status = $serverObj.reboot()
if ($status.ReturnValue = "0") {
Write-Host "Reboot successful."
Start-Sleep -s 300 # this will wait for 5 minutes
} else {
Write-Host "Reboot failed. Check permissions."
}
do {
Send-MailMessage -To "test.test@test.ac.uk" -From "test@test.ac.uk" -SmtpServer 192.168.100.31 -Subject "$i ping failed" -Body "$i ping failed"
}
while (!(Test-Connection -ComputerName $i -Quiet))
}
The idea is to reboot a server, then wait 5 minutes and then do a ping If that fails it will send us an email saying it failed and then continue rebooting the other servers.
Thank you soo much dan.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi it don't seem to read the if (!(Test-Connection -ComputerName $i -Quiet))
instead it sends me the email. Please Help!
instead it sends me the email. Please Help!
ASKER
I've tried
However for some reason it sends the email from the else statement?? even if the condition is true??
if (Test-Connection -ComputerName $i -Quiet)
{
Send-MailMessage -To "test@test.ac.uk" -From "test@test.ac.uk" -SmtpServer smtp.test.ac.uk -Subject "$i ping successful" -Body "$i ping successful"
}
Else
{
Send-MailMessage -To "test@test.ac.uk" -From "test@test.ac.uk" -SmtpServer smtp.test.ac.uk -Subject "$i ping Failed" -Body "$i ping Failed"
}
However for some reason it sends the email from the else statement?? even if the condition is true??
Nope.
If it always sends the message from the Else statement, that means the condition is false.
Test the condition in PS and see if it works. Remove the -Quiet to see the output and errors.
I'm suspecting that $i does not resolve properly. DNS trouble.
If it always sends the message from the Else statement, that means the condition is false.
Test the condition in PS and see if it works. Remove the -Quiet to see the output and errors.
I'm suspecting that $i does not resolve properly. DNS trouble.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
good question footech, I never knew about the restart-computer cmdlet! please let me know how I can use it for my purpose, its not just about the script its also about learning powershell.
Many thanks guys, I will give it the best grade I can when I close it.
Many thanks guys, I will give it the best grade I can when I close it.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thank you all much appreciated!
ASKER
Hi Guys,
I am trying to link the script that restarts the servers to another script to test the connection and restart the VM's using power cli, so far this is what I have written:
and it needs to be run on another server with powercli installed, problem is that I wont be sitting on the computer when this command runs and the get-credential will prompt for credential and also how can I link this with my other script, restart server script?
I am trying to link the script that restarts the servers to another script to test the connection and restart the VM's using power cli, so far this is what I have written:
Add-PSSnapin VMware.VimAutomation.Core
$cred=get-credential
Connect-VIServer 10.2.32.4 –Credential $cred
Get-Content C:\Reboot_Servers.csv| % { if (!(Test-Connection -ComputerName $_ -Quiet))
$vm=get-vm "*" + $_ + "*"
Restart-VM -VM $VM
Start-Sleep -s 300 # this will wait for 5 minutes
}
and it needs to be run on another server with powercli installed, problem is that I wont be sitting on the computer when this command runs and the get-credential will prompt for credential and also how can I link this with my other script, restart server script?
Live is much easier if you install PowerCLI on the machine you want to run the script on.
And I have no idea how you would want to link the scripts - they have no connection to each other. However, you can always just call another script within a script by providing the path:
And I have no idea how you would want to link the scripts - they have no connection to each other. However, you can always just call another script within a script by providing the path:
<# code here #>
C:\Scripts\otherScript.ps1
ASKER
thanks Qlemo, yea its a lot better I can install powercli on the server, but problem is it has to go through a lengthy change request at my organisation.
ASKER
How can I get around the get-credential issue, where I can store this data so it wont prompt?
You want to store the credentials, or to get asked locally instead of remote?
ASKER
store credentials, so when I use windows task scheduler I wont get prompted for it, hope it makes sense.
Is having the password in plain text in the script ok, or do you need better security?
Plain text:
$cred = New-Object Management.Automation.PsCredential("Username",
(ConvertTo-SecureString "Password" -AsPlainText -force))
Password hashed in a file:$cred = New-Object Management.Automation.PsCredential("Username",
(Get-Content C:\Passwordfile.txt | ConvertTo-SecureString))
and the file has been generated once withRead-Host -AsSecureString | ConvertFrom-SecureString | Out-File C:\Passwordfile.txt
(to get a prompt) orConvertTo-SecureString "Password" -AsPlainText -Force | ConvertFrom-SecureString | Out-File C:\Passwordfile.txt
ASKER
how would I reimport the password file in the get-credential? or anywhere else where it requires user names and password?
Open in new window