search
Categories
Sponsors
VirtualMetric Hyper-V Monitoring, Hyper-V Reporting
Archive
Blogroll

Badges
MCSE
Community

Cozumpark Bilisim Portali
Posted in Windows Powershell, Windows Server | 1 Comment | 2,743 views | 20/06/2014 22:51

You may want to check available Microsoft Updates on remote servers. This script will give you that info.

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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
Function Get-MicrosoftUpdate {
 
<#
    .SYNOPSIS
 
        Function to get available Microsoft Updates on Windows Servers.
 
    .DESCRIPTION
 
        If you have large number of Windows Servers, Microsoft Updates may not be an easy job.
		This script could get available Microsoft Updates from remote servers by using PowerShell.
 
    .PARAMETER  WhatIf
 
        Display what would happen if you would run the function with given parameters.
 
    .PARAMETER  Confirm
 
        Prompts for confirmation for each operation. Allow user to specify Yes/No to all option to stop prompting.
 
    .EXAMPLE
 
        Get-MicrosoftUpdate
 
    .EXAMPLE
 
        Get-MicrosoftUpdate -ComputerName Server01
 
    .EXAMPLE
 
        Get-MicrosoftUpdate -ComputerName Server01 -SuppressMode		
 
    .INPUTS
 
        None
 
    .OUTPUTS
 
        None
 
    .NOTES
 
        Author: Yusuf Ozturk
        Website: http://www.yusufozturk.info
        Email: ysfozy@gmail.com
        Date created: 19-June-2014
        Last modified: 22-June-2014
        Version: 1.2
 
    .LINK
 
        http://www.yusufozturk.info
        http://twitter.com/yusufozturk
 
#>
 
	[CmdletBinding(SupportsShouldProcess = $true)]
	param (
 
		# ComputerName
		[Parameter(
			Mandatory = $false,
			HelpMessage = 'Computer Name')]
		$ComputerName = "localhost",
 
		# Suppress Mode
		[Parameter(
			Mandatory = $false,
			HelpMessage = 'Suppress Mode')]
		[switch]$SuppressMode = $false,
 
		# Debug Mode
		[Parameter(
			Mandatory = $false,
			HelpMessage = 'Debug Mode')]
		[switch]$DebugMode = $false
	)
 
	# Enable Debug Mode
	if ($DebugMode)
	{
		# Set Error Action Preference
		$ErrorActionPreference = "Stop"
 
		# Set Debug Preference
		$DebugPreference = "Continue"
	}
	else
	{
		# Set Error Action Preference
		$ErrorActionPreference = "silentlycontinue"
	}
 
	if ($ComputerName -ne "localhost")
	{
		if (!$SuppressMode)
		{
			# Informational Output
			Write-Host "Getting Microsoft Update information from $ComputerName.." -ForegroundColor Cyan
			Write-Host "Please wait.." -ForegroundColor Gray
			Write-Host " "
		}
 
		# Invoke Script on Remote Computer
		$InvokeCommand = Invoke-Command -ComputerName $ComputerName -ArgumentList $SuppressMode, $DebugMode -ScriptBlock {
 
			# Define Parameters
			param ($SuppressMode, $DebugMode)
 
			# Create Microsoft Update Session
			$UpdateSession = New-Object -ComObject "Microsoft.Update.Session"
 
			# Search Microsoft Updates
			$UpdateSearcher = $UpdateSession.CreateUpdateSearcher()
			$SearchResult = $UpdateSearcher.Search("IsInstalled=0 and IsHidden=0")
 
			# Create Microsoft Update Install Queue
			$InstallQueue = New-Object -ComObject "Microsoft.Update.UpdateColl"
 
			for ($i = 0; $i -lt $SearchResult.Updates.Count; $i++)
			{
				# Get Current Microsoft Update
				$Update = $SearchResult.Updates.Item($i)
 
				# Get Microsoft Update Description
				$UpdateDescription = $Update.Title
 
				# Microsoft Update KB Pattern
				$KBPattern = "KB\d{1,20}"
 
				# Search for Microsoft Update KB
				$RegexSearch = $UpdateDescription -match $KBPattern
 
				# Get Microsoft Update KB
				$UpdateKB = $Null;
				$UpdateKB = $Matches[0]
 
				if (!$SuppressMode)
				{
					# Informational Output
					Write-Host "-------------------------------------------------"
					Write-Host " "
					Write-Host "$UpdateKB is available to install.." -ForegroundColor Cyan
					Write-Host "Description: $UpdateDescription" -ForegroundColor Gray
					Write-Host " "
				}
 
				# Add to Install Queue
				$InstallQueue.Add($Update) | Out-Null
 
				# Reboot Behaviour
				if ($Update.InstallationBehavior.RebootBehavior -gt 0) 
				{ 
					# Set Reboot Status
					$RebootRequired = $True
 
					if (!$SuppressMode)
					{
						# Informational Output
						Write-Host "Warning: $UpdateKB requires reboot." -ForegroundColor Yellow
						Write-Host " "
						Write-Host " "
					}
				}
				else
				{
					if (!$SuppressMode)
					{
						# Informational Output
						Write-Host "$UpdateKB doesn't require reboot." -ForegroundColor Green
						Write-Host " "
						Write-Host " "
					}
				}
			}
 
			if ($InstallQueue.Count -eq 0)
			{
				if (!$SuppressMode)
				{
					# Informational Output
					Write-Host "Warning: There are no Microsoft Updates available" -ForegroundColor Yellow
				}
 
				# Update Status
				$UpdateStatus = "False"
			}
			else
			{
				if (!$SuppressMode)
				{
					# Informational Output
					Write-Host "Available Microsoft Updates: $i" -ForegroundColor Green
				}
 
				# Update Status
				$UpdateStatus = "True"
			}
 
			# Output Update Status
			$UpdateStatus
		}
 
		if ($SuppressMode)
		{
			# Output Invoke Command Results
			$InvokeCommand
		}
	}
	else
	{
		# Create Microsoft Update Session
		$UpdateSession = New-Object -ComObject "Microsoft.Update.Session"
 
		# Search Microsoft Updates
		$UpdateSearcher = $UpdateSession.CreateUpdateSearcher()
		$SearchResult = $UpdateSearcher.Search("IsInstalled=0 and IsHidden=0")
 
		# Create Microsoft Update Install Queue
		$InstallQueue = New-Object -ComObject "Microsoft.Update.UpdateColl"
 
		for ($i = 0; $i -lt $SearchResult.Updates.Count; $i++)
		{
			# Get Current Microsoft Update
			$Update = $SearchResult.Updates.Item($i)
 
			# Get Microsoft Update Description
			$UpdateDescription = $Update.Title
 
			# Microsoft Update KB Pattern
			$KBPattern = "KB\d{1,20}"
 
			# Search for Microsoft Update KB
			$RegexSearch = $UpdateDescription -match $KBPattern
 
			# Get Microsoft Update KB
			$UpdateKB = $Null;
			$UpdateKB = $Matches[0]
 
			if (!$SuppressMode)
			{
				# Informational Output
				Write-Host "-------------------------------------------------"
				Write-Host " "
				Write-Host "$UpdateKB is available to install.." -ForegroundColor Cyan
				Write-Host "Description: $UpdateDescription" -ForegroundColor Gray
				Write-Host " "
			}
 
			# Add to Install Queue
			$InstallQueue.Add($Update) | Out-Null
 
			# Reboot Behaviour
			if ($Update.InstallationBehavior.RebootBehavior -gt 0) 
			{ 
				# Set Reboot Status
				$RebootRequired = $True
 
				if (!$SuppressMode)
				{
					# Informational Output
					Write-Host "Warning: $UpdateKB requires reboot." -ForegroundColor Yellow
					Write-Host " "
					Write-Host " "
				}
			}
			else
			{
				if (!$SuppressMode)
				{
					# Informational Output
					Write-Host "$UpdateKB doesn't require reboot." -ForegroundColor Green
					Write-Host " "
					Write-Host " "
				}
			}
		}
 
		if ($InstallQueue.Count -eq 0) 
		{
			if (!$SuppressMode)
			{
				# Informational Output
				Write-Host "Warning: There are no Microsoft Updates available" -ForegroundColor Yellow
			}
 
			# Update Status
			$UpdateStatus = "False"
		}
		else
		{
			if (!$SuppressMode)
			{
				# Informational Output
				Write-Host "Available Microsoft Updates: $i" -ForegroundColor Green
			}
 
			# Update Status
			$UpdateStatus = "True"
		}
 
		if ($SuppressMode)
		{
			# Output Update Status
			$UpdateStatus
		}
	}
}

So if you just type:

Get-MicrosoftUpdate

that will get Microsoft Updates on your local server.
If you want to get Microsoft Updates from remote server, then type like:

Get-MicrosoftUpdate -ComputerName "MyHyperVHost01"

That will give you Reboot requirements as well.