WMI Query for certain computer names but not others

14,314

I haven't tested the parenthesis from the previous answer, if it works that should solve it.

In my opinion you're over complicating it, this can be simplified. First, if your server names always start with MS, drop the initial percent sign. Next, think of it as match all you want, then exclude those you don't.

You want all servers that start with MS

SELECT * FROM Win32_ComputerSystem WHERE Name LIKE 'MS%'

You don't want any servers that start with MSDS (or MSDSY, but in your example you could stop at the second S)

SELECT * FROM Win32_ComputerSystem WHERE not Name LIKE 'MSDS%' 

Now combine them

SELECT * FROM Win32_ComputerSystem WHERE Name LIKE 'MS%' AND NOT Name LIKE 'MSDS%'

This will get all servers that start with MS except those that start with MSDS.

I also encourage you to use the WBEMTEST utility, it can help you fine tune these queries (and/or make sure your answer is accurate on stack exchange!)

Share:
14,314
mcswainy
Author by

mcswainy

Updated on September 18, 2022

Comments

  • mcswainy
    mcswainy almost 2 years

    I'm trying to write a GPO WMI query that will include computers with certain names and yet exclude some machines that fall within those parameters. This is what I have (and doesn't work):

    SELECT * FROM Win32_ComputerSystem WHERE Name LIKE '%MSD%' OR Name LIKE '%MSL%' AND NOT Name LIKE "%MSDSY%"
    

    This option has not worked. I've also tried to seperate it out into two different queries like below:

    SELECT * FROM Win32_ComputerSystem WHERE Name LIKE '%MSD%' OR Name LIKE '%MSL%'
    

    And then:

    SELECT * from Win32_ComputerSystem WHERE NOT Name LIKE "%MSDSY01% OR %MSDSY02% OR %MSDSY05%"
    

    That didn't work either. Can someone please tell me what I did wrong? Thanks.

    • Srinivasan MK
      Srinivasan MK about 6 years
      What are some sample names? Give those sample names, which ones would you expect TO be returned by the query?
    • mcswainy
      mcswainy about 6 years
      @EBGreen Thank you for the response, basically, I'm wanting to apply the GPO to all machines named MSD% or MSL% but not MSDSY%. It's like the group policy only looks at the first set but ignores the exclusion statement.
    • mcswainy
      mcswainy about 6 years
      @EBGreen the sample names would be MSDCR01, MSDCR02, MSDSY01, MSDSY02. Out of those, I'd like to not see the MSDSY ones.