Quantcast
Channel: ConfigMgr – Adam, the Automator
Viewing all articles
Browse latest Browse all 19

ConfigMgr Site Maintenance Task Powershell Module

$
0
0

I was getting ready to upgrade our ConfigMgr 2012 SP1 site server to R2 (I know, I’m a little behind the times!) and one of the tasks I needed to do was disable all the site maintenance tasks while the upgrade is being performed. I started to document all the site maintenance tasks that were enabled and then proceeded to disable them and hit my click limit. If I find myself clicking a repetitive pattern more than a couple times I stop and ask myself “Can and should this be automated?” and lucky for you I decided to take some time to create a few small Powershell functions.

These functions will not only work for my upgrade scenario I’m sure I’ll also use them for other tasks down the road.

For anyone doing a ConfigMgr upgrade and want to knock out the disabling/enabling site maintenance tasks here’s a little script to run after you’ve got these functions loaded.

## Export out all the tasks that are enabled
Get-CmSiteMaintenceTask -Status Enabled | Export-Csv -Path 'EnabledConfigMgrSiteMaintTasks.csv' -Append -NoTypeInformation
## Disable all of the enabled tasks
Import-Csv '.\EnabledConfigMgrSiteMaintTasks.csv' | Get-CmSiteMaintenaceTask | Disable-CmSiteMaintenanceTask
## Do the R2 upgrade here
## Enable all the tasks that you disabled prior to the upgrade
Import-Csv '.\EnabledConfigMgrSiteMaintTasks.csv' | Get-CmSiteMaintenaceTask | Enable-CmSiteMaintenanceTask

Here are the 3 functions that make this happen. I’d throw them into a .psm1 file and import them as a module but do with them as you please.

function Get-CmSiteMaintenanceTask {
	<#
	.SYNOPSIS
		This function discovers and records the state of all site maintenance tasks on a ConfigMgr site server.
	.PARAMETER TaskName
		The name of the site maintenance task you'd like to limit the result set by.  This accepts wildcards or
		multiple names
	.PARAMETER Status
		The status (either enabled or disabled) of the site maintenance tasks you'd like to limit the result set by.
	.PARAMETER SiteServer
		The SCCM site server to query
	.PARAMETER SiteCode
		The SCCM site code
	.EXAMPLE
	
	PS> Get-CmSiteMaintenanceTask -TaskName 'Disabled*' -Status Enabled
	
	This example finds all site maintenance tasks starting with 'Disabled' that are enabled.
	#>
	[CmdletBinding()]
	[OutputType([System.Management.ManagementObject])]
	param (
		[Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName)]
		[string[]]$TaskName,
		[Alias('ItemName')]
		[ValidateSet('Enabled', 'Disabled')]
		[string]$Status,
		[string]$SiteServer = 'CONFIGMANAGER',
		[ValidateLength(3, 3)]
		[string]$SiteCode = 'UHP'
	)
	
	process {
		try {
			$WmiParams = @{ 'Computername' = $SiteServer; 'Namespace' = "root\sms\site_$SiteCode"}
			
			Write-Verbose -Message "Building the WMI query..."
			if ($TaskName -or $Status) {
				if ($TaskName) {
					$WmiParams.Query = 'SELECT * FROM SMS_SCI_SQLTask WHERE '
					$NameConditions = @()
					foreach ($n in $TaskName) {
						## Allow asterisks in cmdlet but WQL requires percentage and double backslashes
						$NameValue = $n.Replace('*', '%').Replace('\', '\\')
						$Operator = @{ $true = 'LIKE'; $false = '=' }[$NameValue -match '\%']
						$NameConditions += "(ItemName $Operator '$NameValue')"
					}
					$WmiParams.Query += ($NameConditions -join ' OR ')
				}
				if ($Status) {
					$WmiParams.Class = 'SMS_SCI_SQLTask'
					$Enabled = $Status -eq 'Enabled'
					$WhereBlock = { $_.Enabled -eq $Enabled }
				}
			} else {
				$WmiParams.Class = 'SMS_SCI_SQLTask'
			}
			if ($WhereBlock) {
				Get-WmiObject @WmiParams | where $WhereBlock
			} else {
				Get-WmiObject @WmiParams
			}
		} catch {
			Write-Error $_.Exception.Message
		}
	}
}

function Enable-CmSiteMaintenanceTask {
	<#
	.SYNOPSIS
		This function enables a ConfigMgr site maintenance task.
	.PARAMETER InputObject
		An object of returned from Get-CmSiteMaintenceTask of the task you'd like enabled.
	.EXAMPLE
	
	PS> Get-CmSiteMaintenanceTask -TaskName 'Disabled*' -Status Disabled | Enable-CmsiteMaintenanceTask
	
	This example finds all site maintenance tasks starting with 'Disabled' that are disabled and enables them all.
	#>
	[CmdletBinding()]
	param (
		[Parameter(ValueFromPipeline)]
		[System.Management.ManagementObject]$InputObject
	)
	process {
		try {
			$InputObject | Set-WmiInstance -Arguments @{ 'Enabled' = $true } | Out-Null
		} catch {
			Write-Error $_.Exception.Message
		}
	}
}

function Disable-CmSiteMaintenanceTask {
	<#
	.SYNOPSIS
		This function disables a ConfigMgr site maintenance task.
	.PARAMETER InputObject
		An object of returned from Get-CmSiteMaintenceTask of the task you'd like disabled.
	.EXAMPLE
	
	PS> Get-CmSiteMaintenanceTask -TaskName 'Disabled*' -Status Enabled | Disable-CmsiteMaintenanceTask
	
	This example finds all site maintenance tasks starting with 'Disabled' that are enabled and disables them all.
	#>
	[CmdletBinding()]
	param (
		[Parameter(ValueFromPipeline)]
		[System.Management.ManagementObject]$InputObject
	)
	process {
		try {
			$InputObject | Set-WmiInstance -Arguments @{ 'Enabled' = $false } | Out-Null
		} catch {
			Write-Error $_.Exception.Message
		}
	}
}

Download this script on the Technet Script Repository

The post ConfigMgr Site Maintenance Task Powershell Module appeared first on Adam the Automator.


Viewing all articles
Browse latest Browse all 19

Trending Articles