Using variables with WMI queries

by smcreamer at 2012-08-22 14:54:21

File.csv example:

This works. List all captions for the imported shares.
$captions = import-csv file.csv | select caption
foreach ($caption in $captions) {$caption}

This is does not work. It just hangs.
$captions = import-csv file.csv | select caption
foreach ($caption in $captions) {gwmi win32_share -ComputerName plcfsvs1 -filter "caption like ‘$caption’" | select servername,caption,name,path}

My best guess is having $caption inside the double quotes is causing the issue.

I think the variable inside the string needs to be expanded. How is this done?

How do I see what value is in the $caption variable as the code runs?

Thanks in advance.
by willsteele at 2012-08-22 15:21:37
Can you give an example of a caption?
by poshoholic at 2012-08-22 21:31:41
Are you sure it hangs? It may just be taking a long time. Depending on how many shares you import, you’re enumerating all of the shares on server plcfsvs1 once for each imported share you have (i.e. once for each caption you enumerated). I wouldn’t recommend that approach.

I think you’re better off doing it like this:

[script=powershell]$filter = "caption = '$($captions -join ''' OR caption = ''')'"
gwmi win32_share -ComputerName plcfsvs1 -filter $filter | select @{Name='servername';Expression={$_.__SERVER}},caption,name,path[/script]

Note that I corrected your select statement by creating a servername property (which does not exist by default).

Another word of caution: the caption property is just the display name. It is not guaranteed to be unique, and it can be changed. The name of the share however should remain constant as long as that share exists. Therefore you may want to consider using name instead of caption to retrieve the shares that you imported.

To answer your question about seeing the value of $caption as the code runs, you can use a debugger (PowerShell ISE supports debugging). Change the format of your foreach statement so that gwmi is on a line by itself and put a breakpoint on that line. Then press F5 to run to the breakpoint. Once there, you can use the embedded console to invoke commands, like $caption, which will show you the value of $caption. Another alternative is to use one of the Write-* commands inside your script to show values as it runs. Write-Host always shows output, but you need to remove it later. Write-Debug is something you can use during development and then leave behind because it only shows if $debugPreference is set to ‘Continue’.
by smcreamer at 2012-08-23 13:37:43
PSComputerName = Server1 -Name of the server the shares are located on.
Caption = Fred$ - The name of the share.
Name = \Share\Fred$ - The share path.
Path - J:\homes\Fred - The file path.

Instead of using caption, would it be better to pull the share name by getting a substring of the Name property?
by poshoholic at 2012-08-23 15:11:21
I think so. You can perform wildcard searches with WMI by using the LIKE operator and the % character (in WQL, % is the equivalent of the well known * wildcard character). Something like this:

PS C:> gwmi win32_share -filter ‘name like "%ADMIN$%"’

Name Path Description
---- ---- -----------
ADMIN$ C:\Windows Remote Admin