#################################################################################################################################
#$ErrorActionPreference = "Ignore"

#$servers = "W2012A","W2012B","slfkjsdl"
#$WSUSGroup = "Test GGroupppppp"

#$Location = Split-Path $MyInvocation.MyCommand.Path -Parent
#################################################################################################################################
If(Test-Path E:\WSUS_Scripts\Patching-Day\December-2019\Lock_Script.txt) { 
    Write-Host "The Script is locked for protection. If you are not running it by mistake - Delete the file (E:\WSUS_Scripts\Patching-Day\December-2019\Lock_Script.txt) and run the Script again" -ForegroundColor Yellow
    Break
}
Else { New-Item -Path E:\WSUS_Scripts\Patching-Day\December-2019 -Name Lock_Script.txt -ItemType File }
#################################################################################################################################
 
############################################################
#$WSUSGroup = gc H:\Scripts\Patching\_Group-Config-File\Config-WSUS-Group.txt
$Current_Month = (Get-Date -UFormat %B)
$Current_Year = (Get-Date -UFormat %Y)
############################################################
$Dte = Get-Date -UFormat %m-%d-%Y
$Collect_Jobs = @()
$Patch_Job = @()
$Failed_Jobs = @()
$Receive_Jobs = @()

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

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

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

#$Group_Folder = "$WSUSGroup-$Current_Month-Patching"
$Recent_File = gci $ReRun_File_Location | sort LastWriteTime -Descending | select -First 1 | Select FullName

$servers = Import-Csv $Recent_File.FullName | select -ExpandProperty Server

#$servers | Out-File $Location\Patching-Day\$Current_Month-$Current_Year\$Group_Folder\$WSUSGroup-Patching-Today-$Dte.txt
#$servers | Out-File $ReRun_File_Location\$WSUSGroup-Patching-Today-Backup-$Dte.txt

Foreach ($server in $servers) {     
    
     New-EventLog -LogName Application -Source "Patching-Started" -ErrorAction SilentlyContinue -ComputerName $server -verbose
     New-EventLog -LogName Application -Source "Patching-Finished" -ErrorAction SilentlyContinue -ComputerName $server -verbose
     New-EventLog -LogName System -Source "Reboot-After-Patching" -ErrorAction SilentlyContinue -ComputerName $server -verbose
     Write-EventLog -LogName Application -Source "Patching-Started" -EntryType Information -EventId 7890 -Message "Server Patching Started with Powershell - Remote" -ComputerName $server -ErrorAction SilentlyContinue -verbose

    $Patch_Job_ReRun += Start-Job -ScriptBlock { Param($server) WindowsUpdate\Install-WindowsUpdate -ComputerName $server } -Name "ReRun-$WSUSGroup-#$server#-Patching" -ArgumentList $server
   
    Invoke-Command $server -ErrorAction SilentlyContinue { 
        New-EventLog -LogName Application -Source "Patching-Started" -ErrorAction SilentlyContinue -verbose
        New-EventLog -LogName Application -Source "Patching-Finished" -ErrorAction SilentlyContinue -verbose
        New-EventLog -LogName System -Source "Reboot-After-Patching" -ErrorAction SilentlyContinue -verbose
        Write-EventLog -LogName Application -Source "Patching-Started" -EntryType Information -EventId 7890 -Message "Server Patching Started with Powershell - Invoke" -verbose
    }#endInvoke  
  
}#endForeach


#Get-Job

#$Patch_Job_ReRun | Export-Csv $Location\Patching-Day\$Current_Month-$Current_Year\$Group_Folder\ReRun-$WSUSGroup-Patching-Jobs-$Dte.csv -NoTypeInformation
$Patch_Job_ReRun | Export-Csv $ReRun_File_Location\ReRun-$WSUSGroup-Patching-Jobs-Backup-$Dte.csv -NoTypeInformation

#$Collect_Jobs_ReRun = Get-Job | Export-Csv $Location\Patching-Day\$Current_Month-$Current_Year\$Group_Folder\ReRun-$WSUSGroup-Collect-Patching-Jobs-$Dte.csv -NoTypeInformation
$Collect_Jobs_ReRun = Get-Job | Export-Csv $ReRun_File_Location\ReRun-$WSUSGroup-Collect-Patching-Jobs-Backup-$Dte.csv -NoTypeInformation

#$Patch_Job_ReRun | Select @{n="Server";e={$_.Name.Split('-')[0]}},@{n="MainJobID";e={$_.ID}},@{n="MainJobName";e={$_.Name}},@{n="ChildJobID";e={$_.Childjobs.ID}},@{n="ChildJobState";e={$_.Childjobs.State}},PSBeginTime,PSEndTime,Command | Export-Csv $Location\Patching-Day\$Current_Month-$Current_Year\$Group_Folder\ReRun-$WSUSGroup-Jobs-ChildJobs-$Dte.csv -NoTypeInformation
$Patch_Job_ReRun | Select @{n="Server";e={$_.Name.Split('-')[0]}},@{n="MainJobID";e={$_.ID}},@{n="MainJobName";e={$_.Name}},@{n="ChildJobID";e={$_.Childjobs.ID}},@{n="ChildJobState";e={$_.Childjobs.State}},PSBeginTime,PSEndTime,Command | Export-Csv $ReRun_File_Location\ReRun-$WSUSGroup-Jobs-ChildJobs-Backup-$Dte.csv -NoTypeInformation


#################### Reboot Servers ########################
# Restart-Computer -ComputerName $servers -Verbose
############################################################  

<# 

$Failed_Jobs += $Patch_Job | ? { $_.State -eq "Failed" }

$Failed_Jobs | ft -autosize

$Failed_Jobs | Export-Csv "E:\WSUS_Scripts\Patching-Day\NCRA - Quality Assurance Servers-November-Patching\failed-jobs.csv" -NoTypeInformation

#>
   

### Running Count
##  ( $Patch_Job | ? { $_.State -eq "Running" } ).count

## ( $Patch_Job | ? { $_.State -eq "Completed" } ).count

## ( $Patch_Job | ? { $_.State -eq "Failed" } ).count

#>
 