Hi there,
For my next maintenance Weekend i try to write a script to reboot all Servers and check after the reboot all automatic Service if there are running or not.
Now the Script works fine for me so far, but it is not perfect. I would like to ask if anybody could help me to optimize it.
############################################################################################################################################################################ # First Part: Reboot Servers ############################################################################################################################################################################# $date = Get-Date -Format dd-MM-yyyy # Get list of Servers $Servers = Get-Content "D:\Scripts\Reboot\servers.txt" # Reboot each server ForEach ($Server in $Servers) { "Computer $Server initiated reboot at $(Get-Date)" | Add-Content -Path D:\Logs\Reboot\Rebootlogs_$date.txt Restart-Computer $Server -Force -Wait } # Check each Server forEach ($Server in $Servers) { if (Test-Connection $Server -quiet) { "Computer $Server verified to be responding to ping at $(Get-Date)" | Add-Content -Path D:\Logs\Reboot\Rebootlogs_$date.txt } else { "Computer $Server unresponsive to ping at $(Get-Date)" | Add-Content -Path D:\Logs\Reboot\Rebootlogs_$date.txt } } ############################################################################################################################################################################ # Seconde Part: Check Services ############################################################################################################################################################################# # Get list of Servers $Servers = Get-Content "D:\Scripts\Reboot\servers.txt" # Check Auto Services on each Server ForEach ($Server in $Servers) { ForEach-Object { Write-Output $Server | Out-File -FilePath "D:\Logs\Reboot\services_$date.txt" -Append # get Auto that not Running: Get-WmiObject Win32_Service | Where-Object { $_.StartMode -eq 'Auto' -and $_.State -ne 'Running' } | # process them; in this example we just show them: Format-Table -AutoSize @( 'Name' 'DisplayName' @{ Expression = 'State'; Width = 9 } @{ Expression = 'StartMode'; Width = 9 } 'StartName' ) | Out-File -FilePath "D:\Logs\Reboot\services_$date.txt" -Append } }
As you can see i do it in two parts, the perfect way might be in one, where i check one server and write also just one log with all information.
At the Moment the log files Looks like that:
Reboot:
Computer server1 initiated reboot at 04/28/2015 15:14:51
Computer server2 initiated reboot at 04/28/2015 15:16:40
Computer server1 verified to be responding to ping at 04/28/2015 15:17:41
Computer server2 verified to be responding to ping at 04/28/2015 15:17:44
Service:
Server1
Name DisplayName State StartMode StartName
---- ----------- ----- --------- ---------
RemoteRegistry Remote Registry Stopped Auto NT AUTHORITY\LocalService
sppsvc Software Protection Stopped Auto NT AUTHORITY\NetworkSer...
Server2
Name DisplayName State StartMode StartName
---- ----------- ----- --------- ---------
RemoteRegistry Remote Registry Stopped Auto NT AUTHORITY\LocalService
sppsvc Software Protection Stopped Auto NT AUTHORITY\NetworkSer...
Now my Question is how to Change maybe my Loop or code to get one Log like that:
Computer server1 initiated reboot at 04/28/2015 15:14:51
Computer server1 verified to be responding to ping at 04/28/2015 15:17:41
Name DisplayName State StartMode StartName
---- ----------- ----- --------- ---------
RemoteRegistry Remote Registry Stopped Auto NT AUTHORITY\LocalService
sppsvc Software Protection Stopped Auto NT AUTHORITY\NetworkSer...
Computer server2 initiated reboot at 04/28/2015 15:16:40
Computer server2 verified to be responding to ping at 04/28/2015 15:17:44
Name DisplayName State StartMode StartName
---- ----------- ----- --------- ---------
RemoteRegistry Remote Registry Stopped Auto NT AUTHORITY\LocalService
sppsvc Software Protection Stopped Auto NT AUTHORITY\NetworkSer...
Thanks for helping.