Kaido Jarvemets - Logo

Fetching Maintenance Windows for Device Collections in Configuration Manager

The Challenge

Configuration Manager (CM) provides a robust infrastructure for managing devices in large organizations. A critical component of this is the ‘maintenance window‘ which ensures that deployments occur during designated times, minimizing disruptions. But how do you obtain a comprehensive view of these windows across multiple device collections?

The Problem

For administrators managing an extensive environment, navigating through the CM console to fetch details about maintenance windows can be tedious. Manual approaches are time-consuming, error-prone, and not scalable. Moreover, crafting extensive SQL queries might not be feasible for everyone.

The Solution

Enter our streamlined script tailored for this specific challenge. Here’s a snapshot of its functionality:

  • Initialization: It kicks off by importing the necessary Configuration Manager module and setting the required site location.
  • Targeted Query: The script then dives deep, querying all device collections inside a designated Software Updates folder.
  • Fetching Details: For each identified collection, it meticulously fetches the associated maintenance window details.
  • User-Friendly Presentation: The data, once collated, is presented in a structured grid view, making it easy for users to interpret and make decisions.

This script is not just about simplifying a process; it’s about empowering CM administrators to have the right data at their fingertips. Whether it’s for compliance checks, deployment planning, or auditing, this tool is designed to be both efficient and adaptable.

A word of caution: As with any script that interacts with your environment, always test in a sandbox or development setup first. This ensures safety and maintains the integrity of your production systems.

The Script

				
					<#
    =================================================================================
    DISCLAIMER:
    This script is provided "as-is" with no warranties. Usage of this script is at
    your own risk. The author is not liable for any damages or losses arising from
    using this script. Please review the full legal disclaimer at:
    https://kaidojarvemets.com/legal-disclaimer/
    =================================================================================
#>
#Import Configuration Manager Module
Import-Module $env:SMS_ADMIN_UI_PATH.Replace("\bin\i386","\bin\configurationmanager.psd1")
$SiteCode = Get-PSDrive -PSProvider CMSITE
Set-Location "$($SiteCode.Name):\"

$SWUpdatesFolderID = 16777224 #Software Updates Device Collection Folder. Use the Get-CMFolder cmdlet to identify the ContainerNodeID
$SiteCode = $SiteCode.Name
$MaintenanceWindows = @()

#Query All Collections from Software Updates Folder
$CollectionsInSpecficFolder = Get-CimInstance -Namespace "ROOT\SMS\Site_$SiteCode" `
-Query "select * from SMS_Collection where CollectionID is in(select InstanceKey from SMS_ObjectContainerItem where ObjectType='5000' and ContainerNodeID='$SWUpdatesFolderID') and CollectionType='2'"

Write-Output -InputObject "Device Collections in $SWUpdatesFolderID folder: $($CollectionsInSpecficFolder.Count)"

foreach ($Collection in $CollectionsInSpecficFolder) {
    $MWQuery = Get-CMMaintenanceWindow -CollectionId $Collection.CollectionID
    
    if (($MWQuery | Measure-Object).Count -gt 1) {
        foreach ($MW in $MWQuery) {
            $MaintenanceWindowProps = @{
                Collection = $Collection.Name
                CollectionID = $Collection.CollectionID
                MWName = $MW.Name
                IsEnabled = $MW.IsEnabled
                IsGMT = $MW.IsGMT
                Duration = $MW.Duration
                Description = $MW.Description
                ServiceWindowType = switch ($MW.ServiceWindowType) {1 {'All Deployments'} 4 {'Software Updates'} 5 {'Task Sequences'}}
                StartTime = $MW.StartTime
            }
            $MaintenanceWindows += New-Object PSObject -Property $MaintenanceWindowProps
        }
    } else {
        $MaintenanceWindowProps = @{
            Collection = $Collection.Name
            CollectionID = $Collection.CollectionID
            MWName = $MWQuery.Name
            IsEnabled = $MWQuery.IsEnabled
            IsGMT = $MWQuery.IsGMT
            Duration = $MWQuery.Duration
            Description = $MWQuery.Description
            ServiceWindowType = switch($MWQuery.ServiceWindowType){1{'All Deployments'} 4{'Software Updates'} 5{'Task Sequences'}}
            StartTime = $MWQuery.StartTime
        }
        $MaintenanceWindows += New-Object PSObject -Property $MaintenanceWindowProps
    }
}
$MaintenanceWindows | Out-GridView -Title "Maintenance Windows in $SWUpdatesFolderID folder: $(Get-Date)"
				
			

Conclusion

In conclusion, while Configuration Manager offers a plethora of features, sometimes a little scripting can be the key to unlocking its full potential. Happy scripting and efficient Configuration Manager management!

Leave a Reply

Contact me

If you’re interested in learning about Fetching Maintenance Windows for Device Collections in Configuration Manager. I can help you understand how this solution can benefit your organization and provide a customized solution tailored to your specific needs.

Table of Contents