Getting a no result on foreach with Get-WMIObject

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Wei-Yen Tan Wei-Yen Tan 5 months, 3 weeks ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #37341
    Profile photo of Wei-Yen Tan
    Wei-Yen Tan
    Participant

    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
    Profile photo of Liam Kemp
    Liam Kemp
    Participant

    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
    Profile photo of Yuan Li
    Yuan Li
    Participant

    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
    Profile photo of Wei-Yen Tan
    Wei-Yen Tan
    Participant

    Thanks Yuan Li, that was the problem

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.