###### This Script must be run after the patching is in progress ####################
$File_Date =  Get-Date -UFormat %H-%M-%p
$Current_Month = (Get-Date -UFormat %B)
$Current_Year = (Get-Date -UFormat %Y)

If(!(Test-Path "E:\WSUS_Scripts\Patching-Day\$Current_Month-$Current_Year\$WSUSGroup-$Current_Month-Patching\ReRun")) { New-Item -Path "E:\WSUS_Scripts\Patching-Day\$Current_Month-$Current_Year\$WSUSGroup-$Current_Month-Patching\ReRun" -ItemType Directory }

$Collect_Completed_Servers = @()
$Collect_Completed_Jobs = ""

$WSUSGroup = ""
$WSUSGroup = Read-Host "Enter Target Group Name"

$Collect_Completed_Jobs = $Patch_Job 
$Patch_Job = $Patch_Job | ? { $_.State -ne "Completed" } 
$Failed_Jobs = $Patch_Job | ?  { $_.State -eq "Failed" }

$Collect_Completed_Jobs | % {
                $Obj = New-Object -TypeName PSObject
                $Obj | Add-Member -MemberType NoteProperty -Name Server -Value ($_.Name.Split('#')[1])
                $Obj | Add-Member -MemberType NoteProperty -Name State -Value $_.State
                $Obj
}

$TPJ = $Obj

#If(!(Test-Path "E:\WSUS_Scripts\Patching-Day\$Current_Month-$Current_Year\$WSUSGroup-$Current_Month-Patching\ReRun")) { New-Item -Path "E:\WSUS_Scripts\Patching-Day\$Current_Month-$Current_Year\$WSUSGroup-$Current_Month-Patching\ReRun" -ItemType Directory }

$Patch_Job | Export-Csv "$ReRun_File_Location\$WSUSGroup-Completed-Jobs-$File_Date.csv" -NoTypeInformation

$ReRun_File_Location = "E:\WSUS_Scripts\Patching-Day\$Current_Month-$Current_Year\$WSUSGroup-$Current_Month-Patching\ReRun"

#$TPJ | Export-Csv "$ReRun_File_Location\$WSUSGroup-Completed-Jobs-$File_Date.csv" -NoTypeInformation
$Failed_Jobs |  Export-Csv "$ReRun_File_Location\$WSUSGroup-FAILED-Jobs-$File_Date.csv" -NoTypeInformation

#$Unique_Servers = $Collect_Completed_Servers | select Name -Unique
#$temp = Import-Csv "E:\WSUS_Scripts\Patching-Day\November-2019\NCRA - Development Servers-November-Patching\test--NCRA - Development Servers-Patching-Jobs-11-01-2019 - Copy.csv"
#$Collect_Completed_Servers = $Collect_Completed_Jobs.Name | % {$_.Split('#')[1] }

###################################### Reboot Servers ##########################

Restart-Computer -ComputerName $TPJ.Server -Force -Verbose
################################################################################
<#

$servers | % { $T = Test-Connection -ComputerName $_ -Count 1 -ErrorAction SilentlyContinue 
                                        If($T) { Write-Host  "Server $_ -- Responding" -ForegroundColor Green } Else { Write-Host "Server $_ -- Not Responding" -ForegroundColor Red } 
    }


#>



#  $Servers | % { gwmi win32_operatingsystem -ComputerName $_ | select  @{n="Server";e={$_.PSComputername}},@{n="LastBootup";e={$_.Converttodatetime($_.LastBootUpTime)}}}






