<# $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" #$servers = "W2012A","W2012B","slfkjsdl" #$WSUSGroup = "Test GGroupppppp" ################################################################################################################################# If(Test-Path E:\WSUS_Scripts\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\Lock_Script.txt) and run the Script again" -ForegroundColor Yellow Break } Else { New-Item -Path E:\WSUS_Scripts -Name Lock_Script.txt -ItemType File } ################################################################################################################################# Get-Job | Remove-Job $Location = Split-Path $MyInvocation.MyCommand.Path -Parent ############################################################ #$WSUSGroup = gc H:\Scripts\Patching\_Group-Config-File\Config-WSUS-Group.txt $Current_Month = (Get-Date -UFormat %B) ############################################################ $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" #$servers = Get-WsusComputer -ComputerTargetGroups "$WSUSGroup" -IncludeSubgroups|select -ExpandProperty Fulldomainname $Group_Folder = "$WSUSGroup-$Current_Month-Patching" #$Servers_Reboot_Only = Import-Csv E:\WSUS_Scripts\Patching-Day\$Group_Folder\CCXQ1-November-Prep-Files\Manual-$WSUSGroup-OperatingSystems-$Current_Month.csv $servers = Import-Csv E:\WSUS_Scripts\Patching-Day\$Group_Folder\$WSUSGroup-Target-Group-Computers-$Current_Month.csv | select -ExpandProperty FullDomainName $servers = $servers | ? { $_ -notin $Servers_Reboot_Only.FullDomainName } #$Failed_Ping_Test = gc $Location\Files\$WSUSGroup-Ping-Failed-$Current_Month.txt #$servers = $servers | ? { $_ -notin $Failed_Ping_Test } $servers | Out-File $Location\Patching-Day\$Group_Folder\$WSUSGroup-Patching-Today-$Dte.txt $servers | Out-File E:\WSUS_Scripts\Patching-Day\$Group_Folder\$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 += Start-Job -ScriptBlock { Param($server) WindowsUpdate\Install-WindowsUpdate -ComputerName $server } -Name "$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 | Export-Csv $Location\Patching-Day\$Group_Folder\$WSUSGroup-Patching-Jobs-$Dte.csv -NoTypeInformation $Patch_Job | Export-Csv E:\WSUS_Scripts\Patching-Day\$Group_Folder\$WSUSGroup-Patching-Jobs-Backup-$Dte.csv -NoTypeInformation $Collect_Jobs = Get-Job | Export-Csv $Location\Patching-Day\$Group_Folder\$WSUSGroup-Collect-Patching-Jobs-$Dte.csv -NoTypeInformation $Collect_Jobs = Get-Job | Export-Csv E:\WSUS_Scripts\Patching-Day\$Group_Folder\$WSUSGroup-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\$Group_Folder\$WSUSGroup-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\$Group_Folder\$WSUSGroup-Jobs-ChildJobs-Backup-$Dte.csv -NoTypeInformation <# $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