From 7bcb4277018a3e6831cd9af144ea76d9696d8b16 Mon Sep 17 00:00:00 2001 From: codaamok Date: Fri, 23 Sep 2022 23:43:53 +0100 Subject: [PATCH] New parameter UpdateNameFilter +semver: minor --- CHANGELOG.md | 2 ++ README.md | 1 - docs/Invoke-CMSnowflakePatching.md | 29 +++++++++++++++++---- src/Public/Invoke-CMSnowflakePatching.ps1 | 31 +++++++++++++++++++++-- 4 files changed, 55 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 74aae60..a24e30a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added +- New parameter `-UpdateNameFilter` for `Invoke-CMSnowflakePatching` ## [0.3.0] - 2022-09-23 ### Added diff --git a/README.md b/README.md index 6d80c45..35d4b3f 100644 --- a/README.md +++ b/README.md @@ -72,5 +72,4 @@ By default it doesn't reboot or make any retry attempts, but there parameters fo ## To do - Pass alternate credentials for connecting to remote hosts -- Pass a filter of update name, article IDs, or update IDs, to `Invoke-CMSnowflakePatching` to specify which updates to install - Consider using PendingReboot or Test-PendingReboot from the gallery to make the `IsPendingReboot` reflect more than just the newly installed updates exit code \ No newline at end of file diff --git a/docs/Invoke-CMSnowflakePatching.md b/docs/Invoke-CMSnowflakePatching.md index c857e24..d0b15ce 100644 --- a/docs/Invoke-CMSnowflakePatching.md +++ b/docs/Invoke-CMSnowflakePatching.md @@ -14,15 +14,16 @@ Invoke software update installation for a ConfigMgr client, an array of clients, ### ByChoosingConfigMgrCollection (Default) ``` -Invoke-CMSnowflakePatching [-ChooseCollection] [-AllowReboot] [-Attempts ] [-RebootTimeoutMins ] - [-InstallUpdatesTimeoutMins ] [-SoftwareUpdateScanCycleTimeoutMins ] - [-InvokeSoftwareUpdateInstallTimeoutMins ] [] +Invoke-CMSnowflakePatching [-ChooseCollection] [-AllowReboot] [-Attempts ] + [-UpdateNameFilter ] [-RebootTimeoutMins ] [-InstallUpdatesTimeoutMins ] + [-SoftwareUpdateScanCycleTimeoutMins ] [-InvokeSoftwareUpdateInstallTimeoutMins ] + [] ``` ### ByComputerName ``` Invoke-CMSnowflakePatching -ComputerName [-AllowReboot] [-Attempts ] - [-RebootTimeoutMins ] [-InstallUpdatesTimeoutMins ] + [-UpdateNameFilter ] [-RebootTimeoutMins ] [-InstallUpdatesTimeoutMins ] [-SoftwareUpdateScanCycleTimeoutMins ] [-InvokeSoftwareUpdateInstallTimeoutMins ] [] ``` @@ -30,7 +31,7 @@ Invoke-CMSnowflakePatching -ComputerName [-AllowReboot] [-Attempts [-AllowReboot] [-Attempts ] - [-RebootTimeoutMins ] [-InstallUpdatesTimeoutMins ] + [-UpdateNameFilter ] [-RebootTimeoutMins ] [-InstallUpdatesTimeoutMins ] [-SoftwareUpdateScanCycleTimeoutMins ] [-InvokeSoftwareUpdateInstallTimeoutMins ] [] ``` @@ -192,6 +193,24 @@ Accept pipeline input: False Accept wildcard characters: False ``` +### -UpdateNameFilter +One or more strings used to filter the updates you want to invoke the installation of based on name. +It is advised to either provide full or partial strings of the match you need. +The function wraps wildcards around the filter. +For example, if you provide '7-Zip', the function will search for available updates with '%7-Zip%'. + +```yaml +Type: String[] +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + ### -RebootTimeoutMins How long to wait for a host to become responsive again after reboot. This parameter is hidden from tab completion. diff --git a/src/Public/Invoke-CMSnowflakePatching.ps1 b/src/Public/Invoke-CMSnowflakePatching.ps1 index b0d18a1..954c1b8 100644 --- a/src/Public/Invoke-CMSnowflakePatching.ps1 +++ b/src/Public/Invoke-CMSnowflakePatching.ps1 @@ -52,6 +52,10 @@ function Invoke-CMSnowflakePatching { Specify the number of retries you would like to script to make when a software update install failure is detected. In other words, if software updates fail to install, and you specify 2 for the Attempts parameter, the script will attempts installation twice. The default value is 1. + .PARAMETER UpdateNameFilter + One or more strings used to filter the updates you want to invoke the installation of based on name. + It is advised to either provide full or partial strings of the match you need. The function wraps wildcards around the filter. + For example, if you provide '7-Zip', the function will search for available updates with '%7-Zip%'. .PARAMETER RebootTimeoutMins How long to wait for a host to become responsive again after reboot. This parameter is hidden from tab completion. @@ -115,6 +119,17 @@ function Invoke-CMSnowflakePatching { })] [Int]$Attempts = 1, + [Parameter()] + [ValidateScript({ + if ($_ -match '%') { + throw 'String can not contain character %' + } + else { + $true + } + })] + [String[]]$UpdateNameFilter, + [Parameter(DontShow)] [Int]$RebootTimeoutMins = 120, @@ -250,7 +265,8 @@ function Invoke-CMSnowflakePatching { ArgumentList = @( $Member.Name, $AllowReboot.IsPresent, - $Attempts, + $Attempts, + (,$UpdateNameFilter), $InvokeSoftwareUpdateInstallTimeoutMins, $InstallUpdatesTimeoutMins, $RebootTimeoutMins @@ -261,6 +277,7 @@ function Invoke-CMSnowflakePatching { [String]$ComputerName, [Bool]$AllowReboot, [Int]$Attempts, + [String[]]$UpdateNameFilter, [Int]$InvokeSoftwareUpdateInstallTimeoutMins, [Int]$InstallUpdatesTimeoutMins, [Int]$RebootTimeoutMins @@ -268,9 +285,19 @@ function Invoke-CMSnowflakePatching { $Module = Get-Module 'PSCMSnowflakePatching' + + + if (-not [String]::IsNullOrWhiteSpace($UpdateNameFilter)) { + $Filter = 'ComplianceState = 0 AND (EvaluationState = 0 OR EvaluationState = 1 OR EvaluationState = 13) AND (Name LIKE "%{0}%")' -f + [String]::Join('%" OR Name LIKE "%', $UpdateNameFilter) + } + else { + $Filter = 'ComplianceState = 0 AND (EvaluationState = 0 OR EvaluationState = 1 OR EvaluationState = 13)' + } + $GetCMSoftwareUpdatesSplat = @{ ComputerName = $ComputerName - Filter = 'ComplianceState = 0 AND (EvaluationState = 0 OR EvaluationState = 1 OR EvaluationState = 13)' + Filter = $Filter ErrorAction = 'Stop' } [CimInstance[]]$UpdatesToInstall = Get-CMSoftwareUpdates @GetCMSoftwareUpdatesSplat