I am attempting to create a Powershell script to reboot a pre-defined list of servers, from a named text file, excluding any server listed in a second, named input file that is also listed in the first, named input file.
I have a text file with a list of servers listed, one per line, within the source file (see 1000.txt). I have a second text file with a list of servers, one per line, see (exclusions.txt).
1000.txt exclusions.txt
Server1 Server1
Server2 Server4
Server3
Server4
My goal is to use the Powershell Compare-Object cmdlet to compare the contents of the two files and add the results of the compare to an array.
Next, using a ForEach loop, loop through the newly created array and reboot server that was added after the compare was done.
Here is the code I have so far:
# USAGE: .\Reboot-Servers -File <Filename> -Exclusions .\exclusions.txtParam(
[Parameter(Position=0, Mandatory=$true)]
[string]
[ValidateNotNullOrEmpty()]
[alias("f")]
$File,
[Parameter(Position=1, Mandatory=$false)]
[string]
[ValidateNotNullOrEmpty()]
[alias("e")]
$Exclusions
)#End Param
#$ErrorActionPreference = "SilentlyContinue"
[Array]$Servers = Compare-Object -ReferenceObject $(Get-Content $File) -DifferenceObject $(Get-Content $Exclusions) | Where-Object {$_.SideIndicator -eq "<="} | Select-Object -ExpandProperty InputObject | Sort-Object
ForEach ($Server in $Servers) {
$cmd = Restart-Computer -Wait -ThrottleLimit 2 -Force
Invoke-Command -ComputerName $Server -ScriptBlock {$cmd}
IF ($cmd.returnvalue -match "0")
{
Write-Host "$Server - Reboot command is complete" -ForegroundColor Green
} ELSE {
Write-Host "$Server - Unable to send reboot command" -ForegroundColor Red
}
}
$Servers = @()
The Compare-Object cmdlet is working as expected and is creating a string object with the resulting list of servers in the following format
Server2 Server 3
The ForEach loop is correctly reading the first object in the array called $Servers. However, any subsequent object is ignored. The IF, ELSE statement is being completely ignored.
Could someone please shed some light on what I am doing wrong? Or perhaps suggest an alternate method of achieving the same result?
H. Miller