Public/Test-CCMIsWindowAvailableNow.ps1
function Test-CCMIsWindowAvailableNow { <# .SYNOPSIS Determine if a window is available now for the provided runtime and MWType .DESCRIPTION This function uses the IsWindowAvailableNow method of the CCM_ServiceWindowManager CIM class. It will allow you to determine if a deployment will run based on your input parameters. It also will determine your client settings for software updates to appropriately fall back to an 'All Deployment Service Window' according to both your settings, and whether a 'Software Update Service Window' is available .PARAMETER MWType Specifies the types of MW you want information for. Defaults to 'Software Update Service Window'. Valid options are below 'All Deployment Service Window', 'Program Service Window', 'Reboot Required Service Window', 'Software Update Service Window', 'Task Sequences Service Window', 'Corresponds to non-working hours' .PARAMETER MaxRunTime The max run time (in seconds) that will be passed to the IsWindowAvailableNow method. This is defined for the applications, programs, and updates you deploy. For software updates, you would want the cumulative max run time of all updates in a SUG. .PARAMETER CimSession Provides CimSession to gather Maintenance Window information info from .PARAMETER ComputerName Provides computer names to gather Maintenance Window information info from .PARAMETER PSSession Provides PSSession to gather Maintenance Window information info from .PARAMETER ConnectionPreference Determines if the 'Get-CCMConnection' function should check for a PSSession, or a CIMSession first when a ComputerName is passed to the function. This is ultimately going to result in the function running faster. The typical use case is when you are using the pipeline. In the pipeline scenario, the 'ComputerName' parameter is what is passed along the pipeline. The 'Get-CCMConnection' function is used to find the available connections, falling back from the preference specified in this parameter, to the the alternative (eg. you specify, PSSession, it falls back to CIMSession), and then falling back to ComputerName. Keep in mind that the 'ConnectionPreference' also determines what type of connection / command the ComputerName parameter is passed to. .EXAMPLE C:\PS> Test-CCMIsWindowAvailableNow Return information about the default MWType of 'Software Update Service Window' with a runtime of 0, and fallback based on client settings and 'Software Update Service Window' availability. .EXAMPLE C:\PS> Test-CCMIsWindowAvailableNow -ComputerName 'Workstation1234','Workstation4321' -MWType 'Task Sequences Service Window' -MaxRunTime 3600 Return information on whether a task sequence with a run time of 3600 seconds can currently run on 'Workstation1234','Workstation4321' .NOTES FileName: Test-CCMIsWindowAvailableNow.ps1 Author: Cody Mathis Contact: @CodyMathis123 Created: 2020-01-29 Updated: 2020-02-27 #> [CmdletBinding(DefaultParameterSetName = 'ComputerName')] param ( [parameter(Mandatory = $false)] [ValidateSet('All Deployment Service Window', 'Program Service Window', 'Reboot Required Service Window', 'Software Update Service Window', 'Task Sequences Service Window', 'Corresponds to non-working hours')] [string[]]$MWType = 'Software Update Service Window', [Parameter(Mandatory = $false)] [int]$MaxRuntime, [Parameter(Mandatory = $false)] [bool]$FallbackToAllProgramsWindow, [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, ParameterSetName = 'CimSession')] [Microsoft.Management.Infrastructure.CimSession[]]$CimSession, [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, ParameterSetName = 'ComputerName')] [Alias('Connection', 'PSComputerName', 'PSConnectionName', 'IPAddress', 'ServerName', 'HostName', 'DNSHostName')] [string[]]$ComputerName = $env:ComputerName, [Parameter(Mandatory = $false, ParameterSetName = 'PSSession')] [Alias('Session')] [System.Management.Automation.Runspaces.PSSession[]]$PSSession, [Parameter(Mandatory = $false, ParameterSetName = 'ComputerName')] [ValidateSet('CimSession', 'PSSession')] [string]$ConnectionPreference ) begin { #region Create hashtable for mapping MW types $MW_Type = @{ 'All Deployment Service Window' = 1 'Program Service Window' = 2 'Reboot Required Service Window' = 3 'Software Update Service Window' = 4 'Task Sequences Service Window' = 5 'Corresponds to non-working hours' = 6 } #endregion Create hashtable for mapping MW types $testInMWSplat = @{ Namespace = 'root\CCM\ClientSDK' ClassName = 'CCM_ServiceWindowManager' MethodName = 'IsWindowAvailableNow' Arguments = @{ MaxRuntime = [uint32]$MaxRuntime } } $getCurrentWindowTimeLeft = @{ Namespace = 'root\CCM\ClientSDK' ClassName = 'CCM_ServiceWindowManager' MethodName = 'GetCurrentWindowAvailableTime' Arguments = @{ } } $getUpdateMWExistenceSplat = @{ Namespace = 'root\CCM\ClientSDK' Query = 'SELECT Duration FROM CCM_ServiceWindow WHERE Type = 4' } $getSoftwareUpdateFallbackSettingsSplat = @{ Namespace = 'root\CCM\Policy\Machine\ActualConfig' Query = 'SELECT ServiceWindowManagement FROM CCM_SoftwareUpdatesClientConfig' } $invokeCommandSplat = @{ FunctionsToLoad = 'Test-CCMIsWindowAvailableNow', 'Get-CCMConnection' } $StringArgs = @(switch ($PSBoundParameters.Keys) { 'MaxRuntime' { [string]::Format('-MaxRuntime {0}', $MaxRuntime) } 'FallbackToAllProgramsWindow' { [string]::Format('-FallbackToAllProgramsWindow ${0}', $FallbackToAllProgramsWindow) } }) } process { foreach ($Connection in (Get-Variable -Name $PSCmdlet.ParameterSetName -ValueOnly)) { $getConnectionInfoSplat = @{ $PSCmdlet.ParameterSetName = $Connection } switch ($PSBoundParameters.ContainsKey('ConnectionPreference')) { $true { $getConnectionInfoSplat['Prefer'] = $ConnectionPreference } } $ConnectionInfo = Get-CCMConnection @getConnectionInfoSplat $Computer = $ConnectionInfo.ComputerName $connectionSplat = $ConnectionInfo.connectionSplat $Result = [ordered]@{ } $Result['ComputerName'] = $Computer try { switch ($Computer -eq $env:ComputerName) { $true { $HasUpdateMW = $null -ne (Get-CimInstance @getUpdateMWExistenceSplat @connectionSplat).Duration $FallbackSetting = (Get-CimInstance @getSoftwareUpdateFallbackSettingsSplat @connectionSplat).ServiceWindowManagement foreach ($MW in $MWType) { $MWFallback = switch ($FallbackToAllProgramsWindow) { $true { switch ($MWType) { 'Software Update Service Window' { switch ($FallbackSetting -ne $FallbackToAllProgramsWindow) { $true { Write-Warning 'Requested fallback setting does not match the computers fallback setting for software updates' } } switch ($HasUpdateMW) { $true { $FallbackSetting -and $HasUpdateMW } $false { $true } } } default { $FallbackToAllProgramsWindow } } } $false { switch ($MWType) { 'Software Update Service Window' { switch ($HasUpdateMW) { $true { $FallbackSetting -and $HasUpdateMW } $false { $true } } } default { $false } } } } $testInMWSplat['Arguments']['FallbackToAllProgramsWindow'] = [bool]$MWFallback $testInMWSplat['Arguments']['ServiceWindowType'] = [uint32]$MW_Type[$MW] $CanProgramRunNow = Invoke-CimMethod @testInMWSplat @connectionSplat if ($CanProgramRunNow -is [Object]) { $getCurrentWindowTimeLeft['Arguments']['FallbackToAllProgramsWindow'] = [bool]$MWFallback $getCurrentWindowTimeLeft['Arguments']['ServiceWindowType'] = [uint32]$MW_Type[$MW] $TimeLeft = Invoke-CimMethod @getCurrentWindowTimeLeft @connectionSplat $TimeLeftTimeSpan = New-TimeSpan -Seconds $TimeLeft.WindowAvailableTime $Result['MaintenanceWindowType'] = $MW $Result['CanProgramRunNow'] = $CanProgramRunNow.CanProgramRunNow $Result['FallbackToAllProgramsWindow'] = $MWFallback $Result['MaxRunTime'] = $MaxRuntime $Result['WindowAvailableTime'] = [string]::Format('{0} day(s) {1} hour(s) {2} minute(s) {3} second(s)', $TimeLeftTimeSpan.Days, $TimeLeftTimeSpan.Hours, $TimeLeftTimeSpan.Minutes, $TimeLeftTimeSpan.Seconds) [pscustomobject]$Result } } } $false { $ScriptBlock = [string]::Format('Test-CCMIsWindowAvailableNow {0} {1}', [string]::Join(' ', $StringArgs), [string]::Format("-MWType '{0}'", [string]::Join("', '", $MWType))) $invokeCommandSplat['ScriptBlock'] = [scriptblock]::Create($ScriptBlock) Invoke-CCMCommand @invokeCommandSplat @connectionSplat } } } catch { $ErrorMessage = $_.Exception.Message Write-Error $ErrorMessage } } } } |