Wednesday, July 24, 2024

Script to find all maintenance windows in an SCCM environment:

 PowerShell script to find all maintenance windows in an SCCM environment:

Param
(
    #Location of file in which to record this information
    [Parameter(Mandatory=$false,
                ValueFromPipelineByPropertyName=$true,
                Position=0)]
    $OutputFile = "C:\Users\LVSAlter\Documents\SCCM discovery\MaintenanceWindows.csv",

    #Site code for the SCCM instance
    [Parameter(Mandatory=$false,
                ValueFromPipelineByPropertyName=$true,
                Position=1)]
    $SiteCode = "WJP"
)

CLS
#region Connect to CM site drive
    $ProviderMachineName = 'SYCPSCCMCB01.westjet.priv'
    #region Load CM Module for PowerShell
        if((Get-Module ConfigurationManager) -eq $null) { Import-Module "$($ENV:SMS_ADMIN_UI_PATH)\..\ConfigurationManager.psd1" } # Import the ConfigurationManager.psd1 module
        if((Get-PSDrive -Name $SiteCode -PSProvider CMSite -ErrorAction SilentlyContinue) -eq $null) { # Connect to the site's drive if it is not already present
            New-PSDrive -Name $SiteCode -PSProvider CMSite -Root $ProviderMachineName
        }
    #endregion Load CM Module for PowerShell
    Set-Location "$($SiteCode):\" # Set the current location to be the site code.
#endregion Connect to CM site drive

$OutputString = "Collection Type,Collection Name,Maintenance Window Name,Maintenance Window,Description,Duration,Enabled,GMT"
Out-File -FilePath $OutputFile -InputObject $OutputString


$Collections = (Get-CMDeviceCollection).name
foreach ($Collection in $Collections) { 
    $CollectionWindows = Get-CMMaintenanceWindow -CollectionName $Collection | Select-Object -Property Name,Description,Duration,IsEnabled,IsGMT
    foreach ($Window in $CollectionWindows) {
        $OutputString = $null
        $OutputString = '"Device Collection"' + ',"' + $Collection + '","' + $CollectionWindows.Name + '","' + $Window.Description + '","' + $Window.Duration + '","' + $Window.IsEnabled + '","' + $Window.IsGMT + '"'
        Write-Output $OutputString
        Out-File -FilePath $OutputFile -InputObject $OutputString -Append
    } 
}

$Collections = (Get-CMUserCollection).name
foreach ($Collection in $Collections) { 
    $CollectionWindows = Get-CMMaintenanceWindow -CollectionName $Collection | Select-Object -Property Name,Description,Duration,IsEnabled,IsGMT
    foreach ($Window in $CollectionWindows) {
        $OutputString = $null
        $OutputString = '"User Collection"' + ',"' + $Collection + '","' + $CollectionWindows.Name + '","' + $Window.Description + '","' + $Window.Duration + '","' + $Window.IsEnabled + '","' + $Window.IsGMT + '"'
        Write-Output $OutputString
        Out-File -FilePath $OutputFile -InputObject $OutputString -Append
    } 
}



No comments:

Post a Comment