none
PowerShell WMI Query Headache RRS feed

  • Question

  • Hi All,

    PowerShell Issue doing my head in, I have an array of systems that I need to query and everything worked until I changed the array. Now I am getting RPC Errors as if the Get-WmiObject Query is written wrong.

    First, The Array.......

    $array = Get-ADComputer -SearchBase 'OU=Standard,OU=Desktops,dc=xxxxx,dc=co,dc=nz' -Filter 'ObjectClass -eq "Computer"'

    Now the WMI Query,

    foreach($computer in $array) {
        $compSys = $computer | Select -ExpandProperty Name
    
    
    $activeUser = Get-WmiObject -Class WIN32_ComputerSystem -ComputerName $compSys | Select -ExpandProperty UserName
    
    $lookFor = "Drive='C:' AND Extension='pst'","Drive='C:' AND Extension='docx'"
    
    foreach($lookQuery in $lookFor) {
        $searchData = Get-WmiObject -Class CIM_DataFile -Filter $lookQuery -ComputerName $compSys | Select -ExpandProperty Name

    Now Here is the kicker, this works just how I want it to.......

    But, the second I point this at a different OU I get RPC Errors.

    I am loosing my mind so any thoughts / input will help alot


    Jarad, MCITP, Brisbane Australia

    Tuesday, May 27, 2014 4:09 AM

Answers

  • There are nothing wrong with my WMI Queries, you are barking up the wrong tree.

    If you understood english then you would know that the issue is between OU's, from the work go I advised this works. You are just making yourself look stupid becase you failed to read the question.

    Now, this issue is resolved. There was a Firewall in place stopping this from running. Allowed port 135 through the firewall and problem solved.


    Jarad, MCITP, Brisbane Australia

    • Marked as answer by GrumpyBum Tuesday, May 27, 2014 6:20 AM
    Tuesday, May 27, 2014 6:20 AM

All replies

  • First this is unecesary and may actually cause errors.

    $array = Get-ADComputer -SearchBase 'OU=Standard,OU=Desktops,dc=xxxxx,dc=co,dc=nz' -Filter 'ObjectClass -eq "Computer"

    It only needs to be:

    $array=Get-ADComputer -SearchBase 'OU=Standard,OU=Desktops,dc=xxxxx,dc=co,dc=nz'

    Get-ADComputer filters for only computers.


    ¯\_(ツ)_/¯

    Tuesday, May 27, 2014 4:28 AM
  • Now look at what happens when we fix this part of the way.  Run it. Look at what happens.  Do you know how to handle this kind of scripting situation?

    $lookFor="Drive='C:' AND (Extension='pst OR Extension='docx')"
    $computers=Get-ADComputer -SearchBase 'OU=Standard,OU=Desktops,dc=xxxxx,dc=co,dc=nz' Write-Host "You found $($computers.count) computers" -fore green foreach($computer in $computers){ (Get-WmiObject Win32_ComputerSystem -ComputerName $computer.Name).UserName Get-WmiObject CIM_DataFile -Filter $lookQuery -ComputerName $computer.Name }


    ¯\_(ツ)_/¯




    • Edited by jrv Tuesday, May 27, 2014 4:42 AM
    Tuesday, May 27, 2014 4:34 AM
  • Thanks for responding quickly,

    I have that there or I get the following:

    cmdlet Get-ADComputer at command pipeline position 1
    Supply values for the following parameters:
    (Type !? for Help.)
    Filter: 


    Jarad, MCITP, Brisbane Australia

    Tuesday, May 27, 2014 4:39 AM
  • Note that searching the whole drive like that will take a long time.

    ¯\_(ツ)_/¯


    • Edited by jrv Tuesday, May 27, 2014 4:51 AM
    Tuesday, May 27, 2014 4:40 AM
  • Your filter is wrong:

    $lookFor="Drive='C:' AND (Extension='pst OR Extension='docx')"

    You want OR not AND and don't make it an array.


    ¯\_(ツ)_/¯

    Tuesday, May 27, 2014 4:43 AM
  • Note that searching the who drive like that will take a long time.

    ¯\_(ツ)_/¯

    Time is not the issue, it is still quicker than doing it manually.

    $array=Get-ADComputer -SearchBase 'OU=Standard,OU=Desktops,dc=xxxxx,dc=co,dc=nz'
    
    
    
    cmdlet Get-ADComputer at command pipeline position 1
    Supply values for the following parameters:
    (Type !? for Help.)
    Filter: 

    Really just need this working and I can leave this running over night


    Jarad, MCITP, Brisbane Australia

    Tuesday, May 27, 2014 4:44 AM
  • Thanks for responding quickly,

    I have that there or I get the following:

    cmdlet Get-ADComputer at command pipeline position 1
    Supply values for the following parameters:
    (Type !? for Help.)
    Filter: 


    Jarad, MCITP, Brisbane Australia

    Sorry use "-filter *"


    ¯\_(ツ)_/¯

    Tuesday, May 27, 2014 4:51 AM
  • Thanks for responding quickly,

    I have that there or I get the following:

    cmdlet Get-ADComputer at command pipeline position 1
    Supply values for the following parameters:
    (Type !? for Help.)
    Filter: 


    Jarad, MCITP, Brisbane Australia

    Sorry use "-filter *"


    ¯\_(ツ)_/¯

    Quickly, Given this works on 1 OU and NOT the other, how is changing the filter going to help.

    The ARRAY IS NOT the Issue, the WMI Query is


    Jarad, MCITP, Brisbane Australia

    Tuesday, May 27, 2014 5:02 AM
  • The WMI filter is wrong. Fix it . It is logically a non-filter,

    You cannot have a file that is an ext of PST AND DOCX.  It is one or the other.  WMU filters are NOT arrays.  Your filter declaration is an array.

    Here is your filter:

    $lookFor = "Drive='C:' AND Extension='pst'","Drive='C:' AND Extension='docx'"

    Now do this:

    $lookFor.GetType()

    Notice that it is an array.  Look up WMI Filter - It is a string.  You cannot use an array.


    ¯\_(ツ)_/¯

    Tuesday, May 27, 2014 5:13 AM
  • Your filter is wrong:

    $lookFor="Drive='C:' AND (Extension='pst OR Extension='docx')"

    You want OR not AND and don't make it an array.


    ¯\_(ツ)_/¯

    2 Things.....

    1, This works and it is not wrong. An OR Statement will return the Entire C drive and all DOCX files on D Drive as well.

    2, If I was not going to use an Array then I would be doing this manually and not using PowerShell

    Sorry to say, but you seem to be jumping / speaking without thinking. This entire thread reflects that.

    The question is simple, What could cause this to work on one OU and not the Other?


    Jarad, MCITP, Brisbane Australia


    • Edited by GrumpyBum Tuesday, May 27, 2014 5:16 AM Spelling
    Tuesday, May 27, 2014 5:14 AM
  • Well it is clear that you know more about mathematics andlogic thanthe guys who built the computer. 

    If I take a OR b I get one OR the other and NOY both.

    You filter MUST be the following:

    filter="Drive='C:' AND (Extension='pst' OR Extension='docx')"

    Note the parens.  Note that it is NOT and array.  Nore that it uses OR to select one of two required extensions.

    Now if that logic is not the math you learned in junior high school then you will need to teach me the math that is used by my friends in New Zealand.  At Wellington they seem to like my version of logic.  Yours only works south of Christchurch.


    ¯\_(ツ)_/¯

    Tuesday, May 27, 2014 5:21 AM
  • The question is simple, What could cause this to work on one OU and not the Other? (Primary School English)


    Jarad, MCITP, Brisbane Australia

    Tuesday, May 27, 2014 5:31 AM
  • What returns from your OUs?  I cannot see your system.  Your query will return only computer objects.   Not all OUs have computer objects.

    You are the only one who can inspect the result of the AD query.


    ¯\_(ツ)_/¯

    Tuesday, May 27, 2014 5:37 AM
  • As I pointed out before - getting rid of this will help if AD is set in some odd fashion.

    -Filter 'ObjectClass -eq "Computer"'

    This is always safer:

    -Filter *

    Why post all of the WMI if all youwant to knowis why you are not getting data back from the WMI query?


    ¯\_(ツ)_/¯


    • Edited by jrv Tuesday, May 27, 2014 5:40 AM
    Tuesday, May 27, 2014 5:40 AM
  • What returns from your OUs?  I cannot see your system.  Your query will return only computer objects.   Not all OUs have computer objects.

    You are the only one who can inspect the result of the AD query.


    ¯\_(ツ)_/¯

    I have said this already, the Array is fine I have checked this.

    All computers from the OU are in the Array and nicely formatted as Strings


    Jarad, MCITP, Brisbane Australia

    Tuesday, May 27, 2014 5:51 AM
  • What is the problem then. You have all of the computers. What is it that is not working?

    You wrote. "WmiObject Query is written wrong"  I m telling you your query filter is wrong.

    RPC error also may mean you have remote WMI issues ot you have remote firewall issues.  Only you can figure that out since you won't post the exact error.

    The filter is definitely wrong.  Look at the definitions of a WMI filter and look at you use. 


    ¯\_(ツ)_/¯

    Tuesday, May 27, 2014 6:06 AM
  • Proof: (notice only three files are returned.)

    PS C:\scripts> $lookFor = "Drive='C:' AND (Extension='pst' OR Extension='docx')"
    PS C:\scripts> gwmi cim_datafile -filter $lookfor


    Compressed : False
    Encrypted  : False
    Size       :
    Hidden     : False
    Name       : c:\scripts\backup.pst
    Readable   : True
    System     : False
    Version    :
    Writeable  : True

    Compressed : False
    Encrypted  : False
    Size       :
    Hidden     : False
    Name       : c:\scripts\test.docx
    Readable   : True
    System     : False
    Version    :
    Writeable  : True

    Compressed : False
    Encrypted  : False
    Size       :
    Hidden     : False
    Name       : c:\scripts\test.pst
    Readable   : True
    System     : False
    Version    :
    Writeable  : True


    ¯\_(ツ)_/¯

    Tuesday, May 27, 2014 6:10 AM
  • I have been building WMI queries since 1995.  I have used Cim_Datafile  thousands of times.  You are not using it correctly and you are not posting the complete error.  I guess you know all of the answers and so I will leave you to your own devices.


    ¯\_(ツ)_/¯

    Tuesday, May 27, 2014 6:12 AM
  • There are nothing wrong with my WMI Queries, you are barking up the wrong tree.

    If you understood english then you would know that the issue is between OU's, from the work go I advised this works. You are just making yourself look stupid becase you failed to read the question.

    Now, this issue is resolved. There was a Firewall in place stopping this from running. Allowed port 135 through the firewall and problem solved.


    Jarad, MCITP, Brisbane Australia

    • Marked as answer by GrumpyBum Tuesday, May 27, 2014 6:20 AM
    Tuesday, May 27, 2014 6:20 AM
  • Um yeah and no the firewall was not commented about (Wait for the post edits above), and I know I was mistaken for not asking the network guys about physical devices. But guess what, my queries are running perfectly and they always were so thanks for nothing.

    Have a nice day :)


    Jarad, MCITP, Brisbane Australia

    Tuesday, May 27, 2014 6:31 AM
  • Your query shows a real lack of knowledge about WMI and PowerShell.  You also managed to completely buffalo the whole process by keeping the real question half a secret  Next time post the complete error message.

    ¯\_(ツ)_/¯

    Tuesday, May 27, 2014 6:51 AM