I have seen a few articles on using Powershell scripts to reboot servers. I also have seen that getting a script to run from Schedule Task can be tricky. However I have been diligent to do so.
Today I think I have my script and list in a working solution. Its currently working in a lab with test VMs.
We have an SaaS environment that requires nightly reboots. Don't ask, I didn't write the app but it does. Nightmarish I know. Anyway I cant reboot the whole environment at once. I need to split it up in two segments. What I did is to have two scripts with a server list each.
Here is the code for the Powershell Script. As usual will breakdown the code down in the post:
$list = Get-Content "E:\testserverlist.txt"
foreach ($s in $list)
{
(gwmi win32_operatingsystem -ComputerName $s).Win32Shutdown(6)
}
While it appears to be fairly simple, it took some time to get. I was having a few issues locating the proper WMI commands to reboot a machine. The up side, I can use IP Addresses with "-ComputerName" so I am not being held back by any issues with DNS in our AD environment.
Break down:
Variable "$list" is loading the IPs from the TestServerList.txt
Each IP in the list is $s
For every "$S" IP Address, send the command to FORCE REBOOT.
Once figured out, its pretty direct and simple. The only issue I am currently running into is the Communication Time Out when Powershell tries to send a command to a server that is already down. With the old SHUTDOWN command in DOS,the command is sent not waiting for a response. Powershell however is waiting for a response and I have to wait for a time out. Little more research is needed for that. I would want to set a time out of five seconds then move onto the next IP. Also will look at how to email on the timeout when it occurs. I have read other scripts on how to do that.
-Boston Tech Guy Out.