I've got a couple of exported GPO WMI filters off an AD domain as .MOF
files. I see that I can import them using the Group Policy Management console but would rather like to re-import them via a script to automate the entire process.
I have not been able to find any decent documentation on how this works and the scripts available on TechNet1 are mostly going by reading property values from the source directory and writing them to the destination.
I would like to retain the ability to use GPMC to restore WMI filter backups manually, if needed. So an approach to simply use the .mof
files created by GPMC upon WMI filter export is required.
1 Interestingly, different Microsoft employees have apparently worked on similar problems over the years, all coming to similar solutions published on TechNet:
- https://blogs.technet.microsoft.com/manny/2012/02/04/exporting-and-importing-wmi-filters-with-powershell-part-1-export/
- https://blogs.technet.microsoft.com/ashleymcglone/2014/08/11/gpo-migration-with-powershell-now-including-wmi-filters/
- https://blogs.technet.microsoft.com/askds/2008/05/16/bulk-exporting-and-importing-wmi-filters-for-group-policy/
Best Answer
A
.mof
is an input file to themofcomp
utility which is present on every Windows system. You just need to provide the file name and the corresponding namespace as parameters to make it work.The file name is obviously the easy part - this is something you specify when exporting the WMI filter. But
mofcomp
would not run the.mof
export without the correct namespace as it does not know where to find the MSFT_SomFilter class which is being instantiated:Just searching the net for the class name
MSFT_SomFilter
will get you the link of the class documentation on MSDN which conveniently states:So just call
mofcomp
with the appropriate namespace and see it do the work. You likely will need to run this on your Domain Controller with administrative rights (i.e. elevated).Note that if a WMI Filter with the same GUID as the one defined by the
ID = <GUID>
line the.mof
already exists, it will be overwritten. If the GUID is not yet in use, a new WMI filter will be created.The
#PRAGMA AUTORECOVER
warning has no real substance here - no new classes are being defined by the.mof
, so nothing is lost in the case of a WMI repository rebuild.