-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGet-UpdateHistory.ps1
145 lines (89 loc) · 3.71 KB
/
Get-UpdateHistory.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
Function Get-UpdateHistory {
<#
.SYNOPSIS
This cmdlet is used to return information on the history of Windows Updates
.DESCRIPTION
Return a list of installed updates on local or remote devices
.PARAMETER ComputerName
Define computer(s) to remotely return the Windows Update history of
.EXAMPLE
"DC01.domain.com","DHCP.domain.com" | Get-WUHistory
# Return information on Windows Update history for remote devices
.EXAMPLE
Get-WUHistory
# Return information on Windows Updates that were installed and how
.NOTES
Author: Robert Osborne
Contact: rosborne@advisor360.com, rosborne@vinebrooktech.com
.LINK
https://vinebrooktech.com
.INPUTS
System.String[]
.OUTPUTS
PSCustonObject
#>
[OutputType('PSWindowsUpdate.WUHistory')]
[CmdletBinding(
SupportsShouldProcess=$True,
ConfirmImpact="Low")]
param(
[Parameter(
Position=0,
Mandatory=$False,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True)] # End Parameter
[ValidateScript({Test-Connection -CompuerName $env:COMPUTERNAME -Count 2 -BufferSize 32 -Quiet})]
[String[]]$ComputerName = $env:COMPUTERNAME
) # End param
BEGIN {
$UpdateCollection = @()
} PROCESS {
ForEach ($Computer in $ComputerName) {
Write-Verbose "Building Windows Update history list"
If ($PSCmdlet.ShouldProcess($Computer,"Get updates history")) {
Write-Verbose -Message "Getting updates history for $Computer"
If ($Computer -like $env:COMPUTERNAME) {
Write-Verbose -Message "Creating Microsoft.Update.Session object for local device $Computer"
$Session = New-Object -ComObject Microsoft.Update.Session
} Else {
Write-Verbose -Message "Creating update session for remote device $Computer"
$Session = [Activator]::CreateInstance([Type]::GetTypeFromProgID("Microsoft.Update.Session",$Computer))
} # End If Else
Write-Verbose -Message "Creating update searcher for $Computer"
$Searcher = $Session.CreateUpdateSearcher()
$TotalHistoryCount = $Searcher.GetTotalHistoryCount()
If($TotalHistoryCount -gt 0) {
$History = $Searcher.QueryHistory(0, $TotalHistoryCount)
$NumberOfUpdate = 1
Foreach($H in $History) {
Write-Verbose -Message "Searching $($NumberOfUpdate)/$($TotalHistoryCount) $($H.Title) `nUPDATE: $($H.Title)"
$Matches = $Null
$H.Title -match "KB(\d+)" | Out-Null
If($Matches -eq $Null) {
Add-Member -InputObject $H -MemberType NoteProperty -Name KB -Value ""
} Else {
Add-Member -InputObject $H -MemberType NoteProperty -Name KB -Value ($matches[0])
} # End If Else
Add-Member -InputObject $H -MemberType NoteProperty -Name ComputerName -Value $Computer
Switch ($H.ResultCode) {
'1' { $Result = "In Progress" }
'2' { $Result = "Succeeded" }
'3' { $Result = "Succeeded with Errors" }
'4' { $Result = "Failed" }
'5' { $Result = "Aborted" }
} # End Switch
Add-Member -InputObject $H -MemberType NoteProperty -Name Result -Value $Result
$H.PSTypeNames.Clear()
$H.PSTypeNames.Add('PSWindowsUpdate.WUHistory')
$UpdateCollection += $H
$NumberOfUpdate++
} # End Foreach
} Else {
Write-Warning "Update history was likely cleared. No results could be returned"
} # End If Else
} # End If
} # End Foreach
} END {
Return $UpdateCollection
} # End BPE
} # End Function Get-UpdateHistory