103 lines
5.6 KiB
PowerShell
103 lines
5.6 KiB
PowerShell
#################################################################################################################################
|
|
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)
|
|
$Current_Year = (Get-Date -UFormat %Y)
|
|
############################################################
|
|
$Dte = Get-Date -UFormat %m-%d-%Y
|
|
$Collect_Jobs = @()
|
|
$Patch_Job = @()
|
|
$Failed_Jobs = @()
|
|
$Receive_Jobs = @()
|
|
|
|
$servers = ""
|
|
$WSUSGroup = "xxx" #Read-Host "Enter Target Group Name"
|
|
|
|
If(!(Test-Path E:\WSUS_Scripts\Patching-Day\$Current_Month-$Current_Year\$WSUSGroup-$Current_Month-Patching)) { New-Item -Path "E:\WSUS_Scripts\Patching-Day\$Current_Month-$Current_Year" -ItemType Directory -Name $WSUSGroup-$Current_Month-Patching }
|
|
|
|
#$servers = Get-WsusComputer -ComputerTargetGroups "$WSUSGroup" -IncludeSubgroups|select -ExpandProperty Fulldomainname
|
|
$Group_Folder = "$WSUSGroup-$Current_Month-Patching"
|
|
$Group_Folder_Location = "E:\WSUS_Scripts\Patching-Day\$Current_Month-$Current_Year\$Group_Folder"
|
|
|
|
#$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\$Current_Month-$Current_Year\$WSUSGroup.csv | select -ExpandProperty "Server Name"
|
|
$servers = Import-Csv $Group_Folder_Location\$WSUSGroup.csv | select -ExpandProperty "Computer"
|
|
|
|
####$servers = $servers | ? { $_ -notin $Servers_Reboot_Only.FullDomainName }
|
|
|
|
#$servers | Out-File $Location\Patching-Day\$Current_Month-2019\$Group_Folder\$WSUSGroup-Patching-Today-$Dte.txt
|
|
$servers | Out-File $Group_Folder_Location\$WSUSGroup-Patching-Today-Backup-$Dte.txt
|
|
|
|
xxxForeach ($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 += xxxStart-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
|
|
|
|
|
|
|
|
#$Patch_Job | Export-Csv $Location\Patching-Day\$Current_Month-2019\$Group_Folder\$WSUSGroup-Patching-Jobs-$Dte.csv -NoTypeInformation
|
|
$Patch_Job | Export-Csv $Group_Folder_Location\$WSUSGroup-Patching-Jobs-Backup-$Dte.csv -NoTypeInformation
|
|
|
|
#$Collect_Jobs = Get-Job | Export-Csv $Location\Patching-Day\$Current_Month-2019\$Group_Folder\$WSUSGroup-Collect-Patching-Jobs-$Dte.csv -NoTypeInformation
|
|
$Collect_Jobs = Get-Job | Export-Csv $Group_Folder_Location\$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\$Current_Month-2019\$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 $Group_Folder_Location\$WSUSGroup-Jobs-ChildJobs-Backup-$Dte.csv -NoTypeInformation
|
|
|
|
Get-Job
|
|
<#
|
|
|
|
$Failed_Jobs += $Patch_Job | ? { $_.State -eq "Failed" }
|
|
|
|
$Failed_Jobs | ft -autosize
|
|
|
|
$Failed_Jobs | Export-Csv "E:\WSUS_Scripts\Patching-Day\$Current_Month-$Current_Year\$WSUSGroup-$Current_Month-Patching\failed-jobs.csv" -NoTypeInformation
|
|
|
|
#>
|
|
|
|
|
|
### Running Count
|
|
|
|
# $Patch_Job
|
|
|
|
<#
|
|
|
|
$Patch_Job | ft -AutoSize
|
|
|
|
( $Patch_Job | ? { $_.State -eq "Running" } ).count
|
|
|
|
( $Patch_Job | ? { $_.State -eq "Completed" } ).count
|
|
|
|
( $Patch_Job | ? { $_.State -eq "Failed" } ).count
|
|
|
|
|
|
|
|
$Patch_Job | ? { $_.State -eq "Running" } | export-csv $Group_Folder_Location\jobs-running.csv
|
|
|
|
$T = import-csv $Group_Folder_Location\jobs-running.csv
|
|
$T = $T | % {$_.Name.Split('#')[1] }
|
|
#>
|