Author Posts

April 6, 2016 at 5:59 pm

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

April 6, 2016 at 6:31 pm

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

April 6, 2016 at 9:55 pm

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

April 10, 2016 at 1:55 pm

Thanks Yuan Li, that was the problem