MSEndpointMgr

Migrate Status Message Queries in ConfigMgr 2012 with PowerShell

I’ve been doing ConfigMgr migration projects for a while now and first up until recently one of my customers asked me if we could migrate the Status Message Queries they’ve put many hours into creating. I told the customer that sure, it’s possible but not out of the box with the built in migration features in ConfigMgr 2012. In order to migrate these Status Message Queries we need to turn to our good friends PowerShell and the SMS Provider.

Using PowerShell to query for a Status Message Query

As for almost everything else that you can do in ConfigMgr, there’s a WMI class that can be used to query for information. In this particular case I explored the SMS_Query WMI Server Class, more information about it can be found here:
https://msdn.microsoft.com/en-us/library/hh949058.aspx
In this class we’ll find instances that represents any kind of query that you can create in the ConfigMgr console. When we create a Status Message Query from within the console, the FROM statement in that query will be SMS_StatusMessage and that will be represented in a property called TargetClassName on every instance in the SMS_Query class. Since we don’t want any other queries than just the Status Message Queries, this is an excellent property to filter on when querying for all instances.
Here’s a simple code snippet for querying for Status Message Queries only from the SMS_Query WMI Server Class.

Get-WmiObject -Namespace "root\SMS\site_PS1" -Class SMS_Query -Filter "TargetClassName like 'SMS_StatusMessage'"

This command line above will return all instances that are available in the SMS_Query WMI Server Class where the property TargetClassName has the value of SMS_StatusMessage. In general, this is simplified explanation of what the script that I’ve created to migrate the Status Message Queries performs.

Download information

As for all of my scripts, you will find these at the TechNet Gallery as well.

Script documentation

I try to write my scripts so they would resemble a regular PowerShell cmdlet, and to accomplish a successful migration of the Status Message Queries, I had to create two scripts for this. There’s an Export-CMStatusMessageQuery.ps1 and an Import-CMStatusMessageQuery.ps1 script. The scripts are built so that you can leverage PowerShell’s built in help functionality and common parameters like Verbose, Confirm and WhatIf. In addition to these advanced functions, I’ve included a Force parameter so that you can overwrite the file specified in the Path parameter, if you should need to. What’s important to note here is that the export script will only query for a custom Status Message Query. This means that the script will not deal with the built-in queries that comes with ConfigMgr out of the box.
Because I’ve named the scripts in accordance with the best practice for PowerShell naming standards, I don’t think that I need to explain in detail what the differences between the scripts are.
Export-CMStatusMessageQuery.ps1
When using this script, you have two ways to go about it. You could either choose to only export specific Status Message Queries by specifying the -Name parameter, or you could use the -Recurse parameter to let the script know that you want to export all of the custom queries. For the Name parameter, you’re able to use an asterix as a wildcard character to perform a more detailed query. You may have wondered up until now what I’m talking about when I’m saying export a Status Message Query. For either a single query or all of them, the script will collect the following properties:

  • Name
  • Expression
  • TargetClassName
  • LimitedToCollectionID (this property however is not used for Status Message Queries)

and export them to a new XML file that you specify the location of by using the -Path parameter.
Import-CMStatusMessageQuery.ps1
This script requires you to specify the location of the exported XML file created by the Export-CMStatusMessageQuery.ps1 script to the -Path parameter. Once the script has successfully validated that the specified XML file is one that contains exported Status Message Queries, it will create a new WMI instance in the SMS_Query WMI Server Class for every Query element in the XML.

Examples

Here I’ll demonstrate a couple of examples where you could use this script. You should run the export script on the Site server you wish to migrate the queries from, and the import script on the Site server you wish to migrate the queries to:
Example 1
Migrate all custom Status Message Queries:

.\Export-CMStatusMessageQuery.ps1 -SiteServer CAS01 -Recurse -Path "C:\Temp\SMQ.xml" -Force -Verbose
.\Import-CMStatusMessageQuery.ps1 -SiteServer CM01 -Path "C:\Temp\SMQuery.xml" -Verbose

Example 2
Migrate all custom Status Message Queries that contains Windows 8.1 in the name:

.\Export-CMStatusMessageQuery.ps1 -SiteServer CAS01 -Name "*Windows 8.1*" -Path "C:\Temp\SMQ.xml" -Force -Verbose
.\Import-CMStatusMessageQuery.ps1 -SiteServer CM01 -Path "C:\Temp\SMQuery.xml" -Verbose

Example 3
Migrate a specific Status Message Query called OSD – Windows 8.1 Enterprise 64-bit:

.\Export-CMStatusMessageQuery.ps1 -SiteServer CAS01 -Name "OSD - Windows 8.1 Enterprise 64-bit" -Path "C:\Temp\SMQ.xml" -Force -Verbose
.\Import-CMStatusMessageQuery.ps1 -SiteServer CM01 -Path "C:\Temp\SMQuery.xml" -Verbose

(848)

Nickolaj Andersen

Chief Technical Architect and Enterprise Mobility MVP since 2016. Nickolaj has been in the IT industry for the past 10 years specializing in Enterprise Mobility and Security, Windows devices and deployments including automation. Awarded as PowerShell Hero in 2015 by the community for his script and tools contributions. Creator of ConfigMgr Prerequisites Tool, ConfigMgr OSD FrontEnd, ConfigMgr WebService to name a few. Frequent speaker at conferences such as Microsoft Ignite, NIC Conference and IT/Dev Connections including nordic user groups.

Add comment

Sponsors