<#

$ErrorActionPreference = "Ignore"
$WSUSGroup = Read-Host "Enter Target Group Name"
$servers = Get-WsusComputer -ComputerTargetGroups "$WSUSGroup" -IncludeSubgroups|select Fulldomainname -ExpandProperty Fulldomainname
Foreach ($server in $servers) {Install-WindowsUpdate -ComputerName $server -AsJob}

#Function Restart {# Force Reboot of Group (when auto failed) un-rem and run line below.
#Foreach ($server in $servers) {Restart-Computer -ComputerName $server -Force}}

#Write-Host "Type Restart and press enter to reboot the $WSUSGroup WSUS Computer Group"
Read-Host -Prompt "Press Enter to exit" 
#>

#################################################################################################################################
#$ErrorActionPreference = "Ignore"
$WSUSGroup = Read-Host "Enter Target Group Name"

$servers = Get-WsusComputer -ComputerTargetGroups "$WSUSGroup" -IncludeSubgroups|select Fulldomainname -ExpandProperty Fulldomainname

#$servers = "W2012A","W2012B","slfkjsdl"
#$WSUSGroup = "Test GGroupp"
$Location = Split-Path $MyInvocation.MyCommand.Path -Parent
$Dte = Get-Date -UFormat %m-%d-%Y
$Collect_Jobs = @()
$Patch_Job = @()

$servers | Out-File $Location\Patching-Day\"$WSUSGroup"-Server-Patching-Today-$Dte.txt
$servers | Out-File E:\WSUS_Scripts\Patching-Day\"$WSUSGroup"-Server-Patching-Today-Backup-$Dte.txt

Foreach ($server in $servers) {     

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



    #Install-WindowsUpdate -ComputerName $server -AsJob

    
     

}#endForeach

Get-Job

$Patch_Job | Export-Csv $Location\Patching-Day\Patching-Jobs-$Dte.csv -NoTypeInformation
$Patch_Job | Export-Csv E:\WSUS_Scripts\Patching-Day\Patching-Jobs-Backup-$Dte.csv -NoTypeInformation

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

$Patch_Job | 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\Jobs-ChildJobs-$Dte.csv -NoTypeInformation
$Patch_Job | 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 E:\WSUS_Scripts\Patching-Day\Jobs-ChildJobs-Backup-$Dte.csv -NoTypeInformation
    
#Function Restart {# Force Reboot of Group (when auto failed) un-rem and run line below.
#Foreach ($server in $servers) {Restart-Computer -ComputerName $server -Force}}

#Write-Host "Type Restart and press enter to reboot the $WSUSGroup WSUS Computer Group"









 