PS H:\Powershell> New-Object -ComObject Microsoft.Update.Session |gm
TypeName: System.__ComObject#{918efd1e-b5d8-4c90-8540-aeb9bdc56f9d}
Name MemberType Definition
---- ---------- ----------
CreateUpdateDownloader Method IUpdateDownloader CreateUpdateDownloader ()
CreateUpdateInstaller Method IUpdateInstaller CreateUpdateInstaller ()
CreateUpdateSearcher Method IUpdateSearcher CreateUpdateSearcher ()
CreateUpdateServiceManager Method IUpdateServiceManager2 CreateUpdateServiceManager ()
QueryHistory Method IUpdateHistoryEntryCollection QueryHistory (string, int, int)
ClientApplicationID Property string ClientApplicationID () {get} {set}
ReadOnly Property bool ReadOnly () {get}
UserLocale Property uint UserLocale () {get} {set}
WebProxy Property IWebProxy WebProxy () {get} {set}
PS H:\Powershell>
CreateUpdateSearcher Method IUpdateSearcher CreateUpdateSearcher ()
QueryHistory Method IUpdateHistoryEntryCollection QueryHistory (string, int, int)
$obj= New-Object -ComObject Microsoft.Update.Session
We also want to store the CreateUpdateSearcher method inside a variable.
$obj= New-Object -ComObject Microsoft.Update.Session
$Search= $obj.CreateUpdateSearcher()
Now, let's see what is stored in the $Search variable and check its properties and methods,
PS H:\Powershell> $Search
CanAutomaticallyUpgradeService : False
ClientApplicationID :
IncludePotentiallySupersededUpdates : False
ServerSelection : 0
Online : True
ServiceID : 00000000-0000-0000-0000-000000000000
IgnoreDownloadPriority : False
SearchScope : 0
PS H:\Powershell>
PS H:\Powershell> $Search|gm
TypeName: System.__ComObject#{04c6895d-eaf2-4034-97f3-311de9be413a}
Name MemberType Definition
---- ---------- ----------
BeginSearch Method ISearchJob BeginSearch (string, IUnknown, Variant)
EndSearch Method ISearchResult EndSearch (ISearchJob)
EscapeString Method string EscapeString (string)
GetTotalHistoryCount Method int GetTotalHistoryCount ()
QueryHistory Method IUpdateHistoryEntryCollection QueryHistory (int, int)
Search Method ISearchResult Search (string)
CanAutomaticallyUpgradeService Property bool CanAutomaticallyUpgradeService () {get} {set}
ClientApplicationID Property string ClientApplicationID () {get} {set}
IgnoreDownloadPriority Property bool IgnoreDownloadPriority () {get} {set}
IncludePotentiallySupersededUpdates Property bool IncludePotentiallySupersededUpdates () {get} {set}
Online Property bool Online () {get} {set}
SearchScope Property SearchScope SearchScope () {get} {set}
ServerSelection Property ServerSelection ServerSelection () {get} {set}
ServiceID Property string ServiceID () {get} {set}
PS H:\Powershell>
$obj= New-Object -ComObject Microsoft.Update.Session
$Search= $obj.CreateUpdateSearcher()
$HistoryCount = $Search.GetTotalHistoryCount()
PS H:\Powershell> $HistoryCount
149
$obj= New-Object -ComObject Microsoft.Update.Session
$Search= $obj.CreateUpdateSearcher()
$HistoryCount = $Search.GetTotalHistoryCount()
$Search.QueryHistory(0, $HistoryCount)
$obj= New-Object -ComObject Microsoft.Update.Session
$Search= $obj.CreateUpdateSearcher()
$HistoryCount = $Search.GetTotalHistoryCount()
$Search.QueryHistory(0, 5)
function Get-MsUpdates{
param($nupdates,[Parameter(Mandatory=$true)][string]$HostName)
$obj= New-Object -ComObject Microsoft.Update.Session
$Search= $obj.CreateUpdateSearcher()
$HistoryCount = $Search.GetTotalHistoryCount()
if($nupdates -eq $null){
$Search.QueryHistory(0, $HistoryCount)
}else
{
$Search.QueryHistory(0, $nupdates)
}
}
PS H:\Powershell> Get-MsUpdates -HostName localhost
This will show you all the updates installed or, if you want to filter, you just have to use -nupdates option, something like this,
PS H:\Powershell> Get-MsUpdates -nupdates 5 -HostName localhost
And it will show you the last five updates.
function Get-MsUpdates{
param($nupdates,[Parameter(Mandatory=$true)][string]$HostName)
$obj= New-Object -ComObject Microsoft.Update.Session
$Search= $obj.CreateUpdateSearcher()
$HistoryCount = $Search.GetTotalHistoryCount()
if($nupdates -eq $null){
$Search.QueryHistory(0, $HistoryCount)
}else
{
$Search.QueryHistory(0, $nupdates)
}
}
#What this will do, will get the machine names from a file, check the updates on Windows Server 2003 and above and create a log with teh checked servers
#And Errors that may occur
#In my case I'm getting credentials from a file and pass this credentials to the commandas below
$pass= Get-Content '.\cred.txt' |ConvertTo-SecureString
$user= 'dparisfe'
$credentials= New-Object -TypeName System.Management.Automation.pscredential -ArgumentList $user,$pass
write-host "Insert the number of updates to Search or Press [Enter] to all updates?" -Foregroundcolor Green
[int]$Num=read-host
if ($Num -eq 0 ){
Write-Host "You choosed 0 or pressed the [Enter] Key, this will show you all updates." -Foregroundcolor Green
$Num= 10000
}
$File=Get-Content -Path '.\My_hosts.txt' #Change this to pointing to your file Path
$date=get-date -format d
foreach ($Hosts in $file)
{
$Computername=$Hosts
$Object = New-Object -TypeName PSObject
Invoke-Command -ComputerName $Computername -Credential $credentials -scriptblock ${function:get-MsUpdates} -ArgumentList $Num, $Computername |Select-Object Date,@{expression={$COMPUTERNAME};Label="Host"},@{name="Operation"; expression={switch($_.operation){1 {"Installation"}; 2 {"Uninstallation"}; 3 {"Other"}}}},@{name="Status"; expression={switch($_.resultcode){1 {"In Progress"}; 2 {"Succeeded"}; 3 {"Succeeded With Errors"};4 {"Failed"}; 5 {"Aborted"}}}},@{name="Title";expression={[regex]::Match($_.Title,'(KB[0-9]{6,7})').Value}}|FT
}
}
PS H:\Powershell> .\MSUpdate.ps1
Insert the number of updates to Search or Press [Enter] to all updates?
5
Date Host Operation Status Title
---- ---- --------- ------ -----
08/01/2015 22:44:11 BCN-C77MM12 Installation Succeeded KB2819745
08/12/2014 15:29:43 BCN-C77MM12 Installation Succeeded KB2506143
04/12/2014 8:20:20 BCN-C77MM12 Installation Succeeded KB2837579
04/12/2014 8:20:10 BCN-C77MM12 Installation Succeeded KB2878284
04/12/2014 8:19:59 BCN-C77MM12 Installation Succeeded KB2687567
PS H:\Powershell>
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (3)
Commented:
Commented:
PG
Commented: