$Location = Split-Path $MyInvocation.MyCommand.Path -Parent
#################################################################################################################################
$Dte = Get-Date -UFormat %m-%d-%Y
$Current_Month = (Get-Date -UFormat %B)
$WSUSGroup = "NCRA - Development Servers"
$Group_Folder = "$WSUSGroup-$Current_Month-Patching"
$Group = "NCRA - Development Servers"
#################################################################################################################################
#$WSUSGroup = Read-Host "Enter Target Group Name"
#$servers = Get-WsusComputer -ComputerTargetGroups "$WSUSGroup" -IncludeSubgroups|select Fulldomainname -ExpandProperty Fulldomainname
$servers = Import-Csv E:\WSUS_Scripts\Files\*$Group*.csv
#$servers = "w2012a","w2012b","testtest"

$servers | Out-File $Location\Patching-Day\$Group_Folder\$WSUSGroup-Servers-Rebooting-$Dte.txt -ErrorAction SilentlyContinue
$servers | Out-File E:\WSUS_Scripts\Patching-Day\$Group_Folder\$WSUSGroup-Servers-Rebooting-Backup-$Dte.txt -ErrorAction SilentlyContinue

$Patch_Job | Export-Csv $Location\Patching-Day\$Group_Folder\$WSUSGroup-Patching-Jobs-$Dte-Before-Reboot.csv -NoTypeInformation -ErrorAction SilentlyContinue
$Patch_Job | Export-Csv E:\WSUS_Scripts\Patching-Day\$Group_Folder\$WSUSGroup-Patching-Jobs-$Dte-Before-Reboot-Backup.csv -NoTypeInformation -ErrorAction SilentlyContinue

Get-Job | Export-Csv $Location\Patching-Day\$Group_Folder\$WSUSGroup-Patching-Get-Jobs-$Dte-Before-Reboot.csv -NoTypeInformation -ErrorAction SilentlyContinue
Get-Job | Export-Csv E:\WSUS_Scripts\Patching-Day\$Group_Folder\$WSUSGroup-Patching-Get-Jobs-$Dte-Before-Reboot-Backup.csv -NoTypeInformation -ErrorAction SilentlyContinue

$Collect_Jobs = Get-Job | Export-Csv $Location\Patching-Day\$Group_Folder\$WSUSGroup-Patching-Collect-Patching-Jobs-$Dte-Before-Reboot.csv -NoTypeInformation
$Collect_Jobs = Get-Job | Export-Csv E:\WSUS_Scripts\Patching-Day\$Group_Folder\$WSUSGroup-Patching-Collect-Patching-Jobs-$Dte-Before-Reboot-Backup.csv -NoTypeInformation

Foreach ($server in $servers) {
    
    Write-EventLog -LogName Application -Source "Patching-Finished" -EntryType Information -EventId 7891 -Message "Powershell-WSUS-Patching-Finished - Remote" -ComputerName $server.Computer -ErrorAction SilentlyContinue
    Write-EventLog -LogName System -Source "Reboot-After-Patching" -EntryType Information -EventId 7892 -Message "Server Rebooting After Patching - Remote" -ComputerName $server.Computer -ErrorAction SilentlyContinue
    
    Invoke-Command $server.Computer -ErrorAction SilentlyContinue {
        #New-EventLog -LogName Application -Source "Powershell-WSUS-Patching-Finished" -ErrorAction SilentlyContinue
        #New-EventLog -LogName System -Source "Powershell-Reboot-After-Patching" -ErrorAction SilentlyContinue 
        Write-EventLog -LogName Application -Source "Patching-Finished" -EntryType Information -EventId 7891 -Message "Powershell-WSUS-Patching-Finished - Invoke" -ErrorAction SilentlyContinue
        Write-EventLog -LogName System -Source "Reboot-After-Patching" -EntryType Information -EventId 7892 -Message "Server Rebooting After Patching - Invoke" -ErrorAction SilentlyContinue
    }#endInvoke
    Restart-Computer -ComputerName $server.Computer -Force -Verbose

}#endForeach 

<#

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


#>