Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function Write-Banner
- {
- [CmdletBinding()]
- param (
- [Parameter(ValueFromPipeline=$true)]
- [String]$Message
- )
- '#' * ($Message).Length
- Write-Host $Message
- '#' * ($Message).Length
- Write-Host ''
- }
- $updateSession = new-object -com 'Microsoft.Update.Session'
- $UpdateDownloader = $UpdateSession.CreateUpdateDownloader()
- $UpdateInstaller = $UpdateSession.CreateUpdateInstaller()
- $UpdateSearcher = $UpdateSession.CreateUpdateSearcher()
- # Find updates AND INCLUDE Drivers
- $criteria = "IsInstalled=0 AND IsHidden=0 AND DeploymentAction=*"
- $search = $UpdateSearcher.Search(($criteria))
- $h1 = 'Category'
- $h2 = 'Title'
- $h3 = 'Description'
- $Catalog=@()
- $UpdatesExist = $false
- $AvailableUpdates = $Search.Updates
- If ($AvailableUpdates.count -gt 0)
- {
- $UpdatesExist = $true
- Foreach ($Update in $AvailableUpdates)
- {
- $Table = '' | Select-Object $h1,$h2,$h3
- $Index = $Update.Categories.Item.count - 1
- $Item = $Update.Categories.Item($Index)
- $Category = $Item.Name
- $Title = $Update.Title
- $Table.$h1 = $Category
- $Table.$h2 = $Update.Title
- $Table.$h3 = $Update.Description
- $Catalog += $Table
- }
- $Group = $Catalog | Group-Object -Property 'Category'
- Foreach ($Member in $Group)
- {
- $Title = $Member.Name
- Write-Host "[${Title}]" -ForegroundColor Yellow
- $Member.Group | Foreach-Object {
- Write-Host ' - ' $_.Title -ForegroundColor Cyan
- Write-Host ' ' $_.Description
- Write-Host ''
- }
- Write-Host ''
- }
- $Time = (Get-Date).ToString('HH:mm:ss')
- Write-Host "[${Time}] Downloading Updates ..."
- $UpdateDownloader.Updates = $AvailableUpdates
- # Commented for TESTING. Uncomment to download the updates.
- #$UpdateDownloader.Download()
- $Time = (Get-Date).ToString('HH:mm:ss')
- Write-Host "[${Time}] Installing Updates ..."
- $UpdateInstaller.Updates = $UpdateDownloader.Updates
- # Commented for TESTING. Uncomment to install the updates.
- #$UpdateInstaller.Install()
- }
- if ( -not ($UpdatesExist) )
- {
- Write-Banner 'No new updates available'
- }
- if ($UpdateInstaller.RebootRequiredBeforeInstallation)
- {
- Write-Banner 'Reboot Required Before Installation'
- }
- $SystemInfo = New-Object -com 'Microsoft.Update.SystemInfo'
- if ($SystemInfo.RebootRequired)
- {
- Write-Banner 'Reboot Required'
- # Optionally include a command to restart with a delay of X number of seconds
- # &cmd /C "shutdown -r -f -t X"
- }
- <#
- AVAILABLE OPTIONS FOR SEARCH CRITERIA
- Note: The default search criteria is, "IsInstalled=0 and IsHidden=0"
- Criterion: Type
- Type: String
- Acceptable operators: =,!= (equals, not equals)
- Finds updates of a specific type, such as "'Driver'" and "'Software'".
- For example, you may exclude drivers with "Type='Software' AND Type!='Driver'"
- You may also search for drivers only by specifying "Type='Driver'" and not including 'Software'
- Criterion: DeploymentAction
- Type: String
- Acceptable Operators: =
- Finds updates that are deployed for a specific action, such as an installation or uninstallation that the administrator of a server specifies.
- "DeploymentAction='Installation'" finds updates that are deployed for installation on a destination computer.
- "DeploymentAction='Uninstallation'" finds updates that are deployed for uninstallation on a destination computer.
- "DeploymentAction=*" finds updates that are deployed for installation and uninstallation on a destination computer.
- If this criterion is not explicitly specified, each group of criteria that is joined to an AND operator implies "DeploymentAction='Installation'".
- "DeploymentAction='Uninstallation'" depends on the other query criteria.
- Criterion: IsAssigned
- Type: int(bool)
- Acceptable Operators: =
- Finds updates that are intended for deployment by Automatic Updates.
- "IsAssigned=1" finds updates that are intended for deployment by Automatic Updates, which depends on the other query criteria. At most, one assigned Windows-based driver update is returned for each local device on a destination computer.
- "IsAssigned=0" finds updates that are not intended to be deployed by Automatic Updates.
- Criterion: BrowseOnly
- Type: int(bool)
- Acceptable Operators: =
- "BrowseOnly=1" finds updates that are considered optional.
- "BrowseOnly=0" finds updates that are not considered optional.
- Criterion: AutoSelectOnWebSites
- Type: int(bool)
- Acceptable Operators: =
- Finds updates where the AutoSelectOnWebSites property has the specified value.
- "AutoSelectOnWebSites=1" finds updates that are flagged to be automatically selected by Windows Update.
- "AutoSelectOnWebSites=0" finds updates that are not flagged for Automatic Updates.
- Criterion: UpdateID
- Type: string(UUID)
- Acceptable Operators: =
- Acceptable operators: =,!= (equals, not equals)
- Finds updates for which the value of the UpdateIdentity.UpdateID property matches the specified value. Can be used with the != operator to find all the updates that do not have an UpdateIdentity.UpdateID of the specified value.
- For example, "UpdateID='12345678-9abc-def0-1234-56789abcdef0'" finds updates for UpdateIdentity.UpdateID that equal 12345678-9abc-def0-1234-56789abcdef0.
- For example, "UpdateID!='12345678-9abc-def0-1234-56789abcdef0'" finds updates for UpdateIdentity.UpdateID that are not equal to 12345678-9abc-def0-1234-56789abcdef0.
- Note A RevisionNumber clause can be combined with an UpdateID clause that contains an = (equal) operator. However, the RevisionNumber clause cannot be combined with an UpdateID clause that contains the != (not-equal) operator.
- For example, "UpdateID='12345678-9abc-def0-1234-56789abcdef0' and RevisionNumber=100" can be used to find the update for UpdateIdentity.UpdateID that equals 12345678-9abc-def0-1234-56789abcdef0 and whose UpdateIdentity.RevisionNumber equals 100.
- Criterion: RevisionNumber
- Type: int
- Acceptable Operators: =
- Finds updates for which the value of the UpdateIdentity.RevisionNumber property matches the specified value.
- For example, "RevisionNumber=2" finds updates where UpdateIdentity.RevisionNumber equals 2.
- This criterion must be combined with the UpdateID property.
- Criterion: CategoryIDs
- Type: string(UUID)
- Acceptable Operators: CONTAINS
- Finds updates that belong to a specified category.
- Application 5C9376AB-8CE6-464A-B136-22113DD69801
- Connectors 434DE588-ED14-48F5-8EED-A15E09A991F6
- CriticalUpdates E6CF1350-C01B-414D-A61F-263D14D133B4
- DefinitionUpdates E0789628-CE08-4437-BE74-2495B842F43B
- DeveloperKits E140075D-8433-45C3-AD87-E72345B36078
- Drivers EBFC1FC5-71A4-4F7B-9ACA-3B9A503104A0
- FeaturePacks B54E7D24-7ADD-428F-8B75-90A396FA584F
- Guidance 9511D615-35B2-47BB-927F-F73D8E9260BB
- HotFix 5EAEF3E6-ABB0-4192-9B26-0FD955381FA9
- SecurityUpdates 0FA1201D-4330-4FA8-8AE9-B877473B6441
- ServicePacks 68C5B0A3-D1A6-4553-AE49-01D3A7827828
- ThirdParty 871A0782-BE12-A5C4-C57F-1BD6D9F7144E
- Tools B4832BD8-E735-4761-8DAF-37F882276DAB
- UpdateRollups 28BC880E-0592-4CBF-8F95-C79B17911D5F
- Updates CD5FFD1E-E932-4E3A-BF74-18BF0B1BBD83
- Upgrades 3689BDC8-B205-4AF4-8D4A-A63924C5E9D5
- For example, "CategoryIDs contains 'B54E7D24-7ADD-428F-8B75-90A396FA584F'" finds Feature Packs
- Criterion: IsInstalled
- Type: int(bool)
- Acceptable Operators: =
- Finds updates that are installed on the destination computer.
- "IsInstalled=1" finds updates that are installed on the destination computer.
- "IsInstalled=0" finds updates that are not installed on the destination computer.
- Criterion: IsHidden
- Type: int(bool)
- Acceptable Operators: =
- Finds updates that are marked as hidden on the destination computer.
- "IsHidden=1" finds updates that are marked as hidden on a destination computer. When you use this clause, you can set the UpdateSearcher.IncludePotentiallySupersededUpdates property to VARIANT_TRUE so that a search returns the hidden updates. The hidden updates might be superseded by other updates in the same results.
- "IsHidden=0" finds updates that are not marked as hidden. If the UpdateSearcher.IncludePotentiallySupersededUpdates property is set to VARIANT_FALSE, it is better to include that clause in the search filter string so that the updates that are superseded by hidden updates are included in the search results. VARIANT_FALSE is the default value.
- Criterion: IsPresent
- Type: int(bool)
- Acceptable Operators: =
- When set to 1, finds updates that are present on a computer.
- "IsPresent=1" finds updates that are present on a destination computer. If the update is valid for one or more products, the update is considered present if it is installed for one or more of the products.
- "IsPresent=0" finds updates that are not installed for any product on a destination computer.
- Criterion: RebootRequired
- Type: int(bool)
- Acceptable Operators: =
- Finds updates that require a computer to be restarted to complete an installation or uninstallation.
- "RebootRequired=1" finds updates that require a computer to be restarted to complete an installation or uninstallation.
- "RebootRequired=0" finds updates that do not require a computer to be restarted to complete an installation or uninstallation.
- Display all applicable updates, even those superseded by newer updates
- $UpdateSearcher.IncludePotentiallySupersededUpdates = $true
- Display ALL hidden updates you must include superseded and specify the IsHidden Criteria
- $UpdateSearcher.IncludePotentiallySupersededUpdates = $true
- ( IsHidden = 1 )
- Example criteria: Find updates that are NOT installed and NOT marked as hidden
- $criteria = "IsInstalled=0 and IsHidden=0"
- Find updates and EXCLUDE Drivers
- $criteria = "IsInstalled=0 AND IsHidden=0 AND Type='Software' AND DeploymentAction=*"
- Find updates and limit search to FeaturePacks only
- $criteria = "IsInstalled=0 AND IsHidden=0 AND DeploymentAction=* AND CategoryIDs contains 'B54E7D24-7ADD-428F-8B75-90A396FA584F'"
- You can also include 'or' as an operator to create multiple query types
- $criteria = "IsInstalled=0 and DeploymentAction='Installation' or IsPresent=1 and DeploymentAction='Uninstallation' or IsInstalled=1 and DeploymentAction='Installation' and RebootRequired=1 or IsInstalled=0 and DeploymentAction='Uninstallation' and RebootRequired=1"
- #>
Advertisement
Add Comment
Please, Sign In to add comment