Use PowerShell to Link a WQL Query to a WMI filter

Hey Guys,

I have very basic knowledge of scripting but I am trying to learn and I find it difficult but interesting at the same time.  I'm a junior admin and sometimes get assigned tasks where I don't even know where to start so I appreciate your patience.

Anyways I have a WMI Query that works when I plug it into a GPO WMI Filter through the GUI (shown here):
 TestExclude2.PNG
What this Test Exclude filter does is exclude a specific AD group from having the TestAutoLogOff2 GPO from being applied to them in the User's Test OU.

Creating this WMI filter was easy enough through GUI but my dilemma is how can I use PowerShell to create this WMI filter.  My goal is to create a PS1 script that I will be able to use globally throughout all our test, dev, and prod environments.  

I would greatly appreciate any insight.  Thanks.
emag50Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
Why do you say it was easy to create through the GUI?
I don't think that WMI filter does what you say it does - it looks like it will always return true.

Using WMI queries through PowerShell is pretty much the same, though there is some difference in quoting rules.  For example, to run the query you showed above, you would run
Get-WmiObject -Query "Select * from Win32_GroupUser Where GroupComponent='Win32_Group.Domain=""toys.test"",Name=""Test Exclude GPO Group""'"
# or
Get-WmiObject -Class Win32_GroupUser -Filter "GroupComponent='Win32_Group.Domain=""toys.test"",Name=""Test Exclude GPO Group""'"

Open in new window


Run the above query from any domain machine and you should get the same thing.

I'm really not clear on what your goal is.  You said
My goal is to create a PS1 script that I will be able to use globally throughout all our test, dev, and prod environments.
Create a .PS1 script to do what?
emag50Author Commented:
Sorry I wasn't clear on what I want the PS1 file to accomplish.

In a nutshell this is what I need to do:
1. Create a "Session Time Out" GPO that kicks users off remote sessions to the servers after being disconnected after a certain period of time.
2. Create a group membership based WMI filter that excludes the group from having the GPO applied to them.
3. Link the WMI filter to this GPO

I'm not even sure if this is possible now.  I just recently found an article that says the GroupPolicy PowerShell module that Microsoft shipped in Win7/Server2008-R2 did not include any suppor for managing WMI filters:

https://sdmsoftware.com/group-policy-blog/gpmc/digging-into-group-policy-wmi-filters-and-managing-them-through-powershell/
emag50Author Commented:
I meant that doing this through GUI is easier to do as in all tasks that are "non-scripted."  You just open up GPMC, right click WMI filters, then create one with whatever WMI query you need using the correct WQL syntax.

Accomplishing this using PowerShell is another story.
footechCommented:
These are the GPO cmdlets available for 2008 R2/Win7 (you're probably already aware).
https://technet.microsoft.com/en-us/library/ee461027.aspx

There are some definite limitations for working with Group Policies via PowerShell.  If I were you I would create the GPOs, and WMI filters via GPMC, then export and import as needed. PS can help with the last bit.
http://blogs.technet.com/b/ashleymcglone/archive/2014/08/11/gpo-migration-with-powershell-now-including-wmi-filters.aspx
http://blogs.technet.com/b/manny/archive/2012/02/04/perform-a-full-export-and-import-of-wmi-filters-with-powershell.aspx

If you really want an example of creating a WMI filter via PS, see here:
https://github.com/darkoperator/powershell_scripts/blob/master/install-wmifilters.ps1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.