-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListPendingWindowsUpdates.ps1
99 lines (81 loc) · 2.68 KB
/
ListPendingWindowsUpdates.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
function GetPendingUpdatesRemote
{
try
{
#Create Session COM object
$updatesession = [activator]::CreateInstance([type]::GetTypeFromProgID("Microsoft.Update.Session",'localhost'))
}
catch
{
# Catch error and return it
return $_.Exception
}
# Store result
$retval = @()
# Configure Session COM Object
$updatesearcher = $updatesession.CreateUpdateSearcher()
# Configure Searcher object to look for Updates awaiting installation
$searchresult = $updatesearcher.Search("IsInstalled=0")
# Verify if Updates need installed
if ($searchresult.Updates.Count -gt 0)
{
# Updates are waiting to be installed. Cache the count to make the For loop run faster
$count = $searchresult.Updates.Count
# Loop through updates available for installation
for ($i=0; $i -lt $count; $i++)
{
# Create object holding update
$update = $searchresult.Updates.Item($i)
# Create temp object
$temp = "" | Select Title,KB,Priority,RebootBehavior,IsDownloaded
$temp.Title = $update.Title
$temp.KB = ('KB' + $update.KBArticleIDs)
# Get priority
$temp.Priority = switch ($update.DownloadPriority)
{
1 {'Low'}
2 {'Normal'}
3 {'High'}
}
# Get reboot behaivor
$temp.RebootBehavior = switch ($update.InstallationBehavior.RebootBehavior)
{
0 {'NeverReboots'}
1 {'AlwaysRequiresReboot'}
2 {'CanRequestReboot'}
}
# Verify that update has been downloaded
if ($update.IsDownLoaded -eq "True")
{
$temp.IsDownloaded = $true
}
else
{
$temp.IsDownloaded = $false
}
$retval += $temp
}
}
return $retval
}
# Get dependencies
import-module ActiveDirectory
# Get all the servers and sort them
Get-ADComputer -Filter "*" | Sort-Object Name | Set-Variable servers
$result = @()
# Loop the servers
foreach($server in $servers)
{
try
{
# Invoke remote function
Invoke-Command -ScriptBlock ${function:GetPendingUpdatesRemote} -ComputerName $server -ErrorAction Stop | Set-Variable remoteresult
$result += $remoteresult
}
catch
{
# Catch error and store it
$_.Exception
}
}
$result | Select PSComputerName,Title,KB,Priority,RebootBehavior,IsDownloaded | Out-GridView