-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathGet-InstalledUpdates.ps1
96 lines (90 loc) · 4.88 KB
/
Get-InstalledUpdates.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
function Get-InstalledUpdates {
<#
.SYNOPSIS
Gets the NTFS rights set on a folder.
Lists the installed hotfixes and/or updates on target systems.
.DESCRIPTION
Lists the installed hotfixes and/or updates on target systems by using PowerShell remoting.
.PARAMETER ComputerName
An array which takes the names of the target computers as input.
.PARAMETER All
This parameter and parameterset is used by default and lists both installed updates and hotfixes.
.PARAMETER HotFixes
This parameter lists all installed hotfixes.
.PARAMETER Updates
This parameter lists all installed updates.
.EXAMPLE
PS [JeffWouters.nl]\> Get-InstalledUpdates -ComputerName 'server1','server2','server3'
KB Type ComputerName
-- ---- ------------
KB2862330 Update server1
KB2862330 Update server2
KB2862330 Update server3
KB2861698 Update server1
KB2861698 Update server3
KB2489256 Update server1
KB2489256 Update server3
KB2506014 Update server2
KB2509553 Update server1
KB2509553 Update server2
.EXAMPLE
PS [JeffWouters.nl]\> Get-InstalledUpdates -ComputerName 'server1','server2','server3' -HotFixes
KB Type ComputerName
-- ---- ------------
KB2489256 Update server1
KB2489256 Update server3
KB2506014 Update server2
KB2509553 Update server1
KB2509553 Update server2
.EXAMPLE
PS [JeffWouters.nl]\> Get-InstalledUpdates -ComputerName 'server1','server2','server3' -Updates
KB Type ComputerName
-- ---- ------------
KB2862330 Update server1
KB2862330 Update server2
KB2862330 Update server3
KB2861698 Update server1
KB2861698 Update server3
#>
[cmdletbinding(DefaultParameterSetName="All")]
param(
[parameter(mandatory=$true,parametersetname='All')]
[parameter(mandatory=$true,parametersetname='HotFixes')]
[parameter(mandatory=$true,parametersetname='Updates')]
[array]$ComputerName,
[parameter(mandatory=$false,parametersetname='All')][switch]$All,
[parameter(mandatory=$false,parametersetname='HotFixes')][switch]$HotFixes,
[parameter(mandatory=$false,parametersetname='Updates')][switch]$Updates
)
$Session = New-PSSession -ComputerName $ComputerName
Invoke-Command -Session $Session -ScriptBlock { $Session = New-Object -ComObject Microsoft.Update.Session }
Invoke-Command -Session $Session -ScriptBlock { $Searcher = $Session.CreateUpdateSearcher() }
Invoke-Command -Session $Session -ScriptBlock { $HistoryCount = $Searcher.GetTotalHistoryCount() }
if (($PSCmdlet.ParameterSetName -eq 'All') -or ($PSCmdlet.ParameterSetName -eq 'Updates')) {
$Output = Invoke-Command -Session $Session -ScriptBlock {
$Updates = $Searcher.QueryHistory(0,$HistoryCount)
foreach ($Update in $Updates) {
[regex]::match($Update.Title,'(KB[0-9]{6,7})').value | Where-Object {$_ -ne ""} | foreach {
$Object = New-Object -TypeName PSObject
$Object | Add-Member -MemberType NoteProperty -Name KB -Value $_
$Object | Add-Member -MemberType NoteProperty -Name 'Type' -Value 'Update'
$Object
}
}
}
$Output | Select-Object KB,Type,@{Name="ComputerName";Expression={$_.PSComputerName}}
}
if (($PSCmdlet.ParameterSetName -eq 'All') -or ($PSCmdlet.ParameterSetName -eq 'HotFixes')) {
$Output = Invoke-Command -Session $Session -ScriptBlock {
$HotFixes = Get-HotFix | Select-Object -ExpandProperty HotFixID
foreach ($HotFix in $HotFixes) {
$Object = New-Object -TypeName PSObject
$Object | Add-Member -MemberType NoteProperty -Name KB -Value $HotFix
$Object | Add-Member -MemberType NoteProperty -Name 'Type' -Value 'HotFix'
$Object
}
}
$Output | Select-Object KB,Type,@{Name="ComputerName";Expression={$_.PSComputerName}}
}
Remove-PSSession $Session
}