Getting a no result on foreach with Get-WMIObject

Welcome Forums General PowerShell Q&A Getting a no result on foreach with Get-WMIObject

This topic contains 3 replies, has 3 voices, and was last updated by

 
Participant
2 years, 7 months ago.

  • Author
    Posts
  • #37341

    Participant
    Points: 0
    Rank: Member

    I have a list of sharenames in a text file not the unc paths.
    for example,

    DoeJane$
    TomCooks$

    I am trying to get a listing of shares thru WMI (as the server i am working on has powershell v2 old I know but its all i have at the moment) so i can match the corresponding path:

    ie
    name path:
    ______
    Tomcooks$ e:\users\tomcooks

    I am looking for the path directory.

    I did a test on a single user

    Get-wmiobject win32_share -filter "name = 'tomcook'"

    I get the correct result for one user.

    So now i want to pass it through a text file so I write out like below:

    cat "C:\Users\tanw-admin\Desktop\disabledusers2.txt"| foreach-object {get-wmiobject win32_share -filter "name = '$_'"}
    
    

    But in Powershell it comes back with no result.

    i tried this:

    
    $filenames = cat "C:\Users\tanw-admin\Desktop\disabledusers2.txt"
    
     foreach ($share in $filenames) {
     
     get-wmiobject win32_share -filter 'name = $share'
     }
    
    

    Same thing, what gives? Thanks for any help

  • #37342

    Participant
    Points: 0
    Rank: Member

    Hi Wei-Yen,
    I have just tested your first example, and it works fine for me with multiple entries in the text file.
    The second example also works with a bit of tweaking, just some syntactical things on the filter – which you had correct in the first example.
    (Obviously I changed the file location on my pc)

    $filenames = cat "C:\shares.txt"
     foreach ($share in $filenames) {
     get-wmiobject win32_share -filter "name = '$share'"
     }
    

    Also just to clarify, you say in the text file you have the share name as Tomcooks$.
    In your working test you just have 'tomcooks'. That might be a typo in the question, but I thought I'd mention it in case the sharenames in your list don't match the actual share names.

    Cheers,
    Liam

  • #37345

    Participant
    Points: 0
    Rank: Member

    Hey Wei-Yen

    In the beginning, I got the same result as yours, nothing. Then I use trace-command to trace the details and found that there are some space after the sharename.

    trace-command -PSHost -name ParameterBinding -Expression{

    gc C:\temp\aaa.txt |
    ForEach-Object{
    $temp=$_.trim()
    Get-WmiObject -Class win32_share -Filter "name ='$temp'"

    }

    }

    after I trim the space, it'll be good.

    Cheers
    Yuan

  • #37490

    Participant
    Points: 0
    Rank: Member

    Thanks Yuan Li, that was the problem

The topic ‘Getting a no result on foreach with Get-WMIObject’ is closed to new replies.