Optimize Powershell script (reboot remote Server and Check Services)

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.



April 28th, 2015 1:31pm

You could probably have something nice using Function and CmdletBinding so you can get something like:

function reboot-myservers { blablabla }

function check-myservices { blablabla }

$Servers = Get-Content "D:\Scripts\Reboot\servers.txt"

$servers | reboot-myservers | check-myservices





Free Windows Admin Tool Kit Click here and download it now
April 28th, 2015 1:44pm

There are workflows that can do this very efficiently.  Serach for them.

Here is one of many: http://blogs.technet.com/b/heyscriptingguy/archive/2013/01/23/powershell-workflows-restarting-the-computer.aspx

April 28th, 2015 2:12pm

That worked perfect thanks for that.

cheers

Free Windows Admin Tool Kit Click here and download it now
May 4th, 2015 3:20am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics