Freigeben über


Exporting and Importing WMI Filters with PowerShell: Part 1, Export

Since this is my first blog post, I'd like to take quick second to introduce myself. My name is Manny Murguia, and I'm a Senior Consultant at Microsoft. It's been a little over 4 years since I started, and it's been a wild ride so far. I'm looking forward to blogging and hope you get out much out of it as I put in to it! You can find more about me at the About link on my homepage. With introductions out of the way, here we go!

Since I'll be writing about WMI filters, it's only appropriate for me to give a shout out to Mike Stephens. Mike, without knowing it, helped push me to write this blog based on a post of his back in 2008 on the Directory Services Team Blog: bulk-exporting-and-importing-wmi-filters-for-group-policy. I decided to put PowerShell to the test and come up with another method of exporting and importing WMI filters in Active Directory. Before you keep reading this post, take a second to visit this site if you're just looking for a way to create a ton of boxed WMI filters for your environment. My post is directed at those administrators who want to duplicate an existing environment. I will, however, admit that a large piece of the import script you see in "Exporting and Importing WMI Filters with PowerShell: Part 2, Import" came directly from that site (that does it for my anti-plagiarism plug).

Anyone who has worked with Group Policy has undoubtedly used Group Policy Management Console (GPMC). With GPMC, creating, linking, and deleting Group Policy Objects (GPO's) and WMI filters is a snap. The biggest challenge with GPMC, as Mike mentioned back in 2008, is still how to export and import WMI filters. The painstaking process of individually exporting and importing the filters can take good chunk out of your workday. In my experience, this process itself ends up being repeated multiple times since there is always someone asking for a "dump" of production GPO's and WMI filters for their lab (or even for some other production system they happen to be building without you - nothing personal).

Welcome, PowerShell. Since the introduction of Active Directory modules for PowerShell, administrators have had a new tool in their arsenal. Rejoice, and take advantage of it! When it comes to managing Active Directory objects, PowerShell is quite "PowerFull." WMI filters, of course, fall into that category. Since WMI filters are all from an objectClass called "msWMI-Som," we can collect all WMI filters in a domain using the following command:

$WMIFilters = Get-ADObject -Filter 'objectClass -eq "msWMI-Som"' -Properties "msWMI-Name","msWMI-Parm1","msWMI-Parm2"

What doe all that mean? Here's a quick breakdown:

$WMIFilters: This value is just an object we are creating to store our WMI Filters

Get-ADObject: This cmdlet allows us to populate our object with a collection object AD objects matching the -Filter parameter

-Filter: This parameter allows us to filter on what objects will be returned from the query. Since we know WMI filters are of objectClass "msWMI-Som" the filter is pretty straightforward.

-Properties: This parameters allows us to specify which AD attributes should be collected for the objects returned by the query. With WMI filters, we really only care about three attributes for WMI filters:

  • msWMI-Name: The friendly name of the WMI filter
  • msWMI-Parm1: The description of the WMI filter
  • msWMI-Parm2: The query and other related data of the WMI filter

The following script is also attached to this post as Backup_All_WMI_Filters.ps1.

param([String]$BackupFile)

if ([String]::IsNullOrEmpty($BackupFile)) {   $BackupFile = "C:\WMI_Full_Backup.csv"  set-content $BackupFile $NULL } else {  set-content $BackupFile $NULL }

import-module ActiveDirectory

$WMIFilters = Get-ADObject -Filter 'objectClass -eq "msWMI-Som"' -Properties "msWMI-Name","msWMI-Parm1","msWMI-Parm2"

foreach ($WMIFilter in $WMIFilters) {  $NewContent = $WMIFilter."msWMI-Name" + "`t" + $WMIFilter."msWMI-Parm1" + "`t" + $WMIFilter."msWMI-Parm2"  add-content $NewContent -path $BackupFile }

clear

write-host "A full backup of WMI Filters has been stored at $BackupFile`n"

So what does this script do? Let's check it out.

It starts by allow the person running the script to provide just one parameter called $BackupLocation. If that parameter is not specified, it defaults to C:\WMI_Full_Backup.csv. Either way, the file content is set to $NULL.

param([String]$BackupFile)

if ([String]::IsNullOrEmpty($BackupFile)) {  $BackupFile = "C:\WMI_Full_Backup.csv"  set-content $BackupFile $NULL } else {  set-content $BackupFile $NULL }

Next, we import the Active Directory Modules for PowerShell and "dump" a full collection of WMI filters from the domain along with the Name, Description, and Query for each filter described earlier.

import-module ActiveDirectory

$WMIFilters = Get-ADObject -Filter 'objectClass -eq "msWMI-Som"' -Properties "msWMI-Name","msWMI-Parm1","msWMI-Parm2"

Now comes the actually export process. We'll run through a loop for each WMI filter in our $WMIFilters collection and do two things: Structure a tab-delimited string that will be added to our $BackupFile and append that string to our $BackupFile.

foreach ($WMIFilter in $WMIFilters) { $NewContent = $WMIFilter."msWMI-Name" + "`t" + $WMIFilter."msWMI-Parm1" + "`t" + $WMIFilter."msWMI-Parm2" add-content $NewContent -path $BackupFile }

The last two commands in the script just present a clean output the console to notify the administrator the script is complete. Notice there was not a ton of error-checking in this script. Who has time for that?

clear

write-host "A full backup of WMI Filters has been stored at $BackupFile`n"

By now, you have noticed we don't have actual WMI filters at all. In fact, we only have exactly what we need. The Name, Description, and Query will work fine for the import process. The backup file can be imported using the script provided in Exporting and Importing WMI Filters with PowerShell: Part 2, Import.

Disclaimer: The information on this site is provided "AS IS" with no warranties, confers no rights, and is not supported by the authors or Microsoft Corporation. Use of included script samples are subject to the terms specified in the Terms of Use .

Backup_All_WMI_Filters.ps1

Comments

  • Anonymous
    January 01, 2003
    Good stuff, Pat! Always looking for ways to improve efficiency. If you have any ideas on the importing WMI blog, please post those too! That's where the real magic happens :)
  • Anonymous
    May 26, 2014
    Hi Manny

    Thank you for sharing.
    It could also be achieved using the following one-liner (Ok, broken in three lines for simpler reading):

    Get-ADObject -Filter 'objectClass -eq "msWMI-Som"' -Properties "msWMI-Name","msWMI-Parm1","msWMI-Parm2" |
    Select DistinguishedName, msWMI-Name, msWMI-Parm1, msWMI-Parm2, Name |
    Export-CSV -NoTypeInformation -Encoding UTF8 C:TEMPWMIFilter.csv

    Of course for a complete script you would need to add all the params and error checking and alike.