WMI Query

Guys,

I am stuck on what i think is a simple issue \ mistake. I have make this Powershell code that i need to connect to machines in the Computers.txt file and then filter out all applications apart from an app called Mimecast. I need the output to show the computer name and what version of the application is installed. In the Select-object if i remove $_ then the script runs and only returns the version number.

gc S:\computers.txt | ForEach-Object {Get-WmiObject -Class win32_product -Impersonation 3 -computername $_  -Filter "Name LIKE '%Mimecast%'" | Select-Object $_ ,  Version }

Can anyone show me the error of my way?

Thanks in advance

August 29th, 2013 5:42am

Hi,

the $_ at the end of the pipeline within the Select-Object doesn't refer to emitted computername from get-content anymore. Therefore you'll need to caputer the computername within a separate variable in the foreach block and add it via a calculated property:

gc S:\computers.txt | ForEach-Object {
$computerName=$_
Get-WmiObject -Class win32_product -Impersonation 3 -computername $_  -Filter "Name LIKE '%Mimecast%'" | 
Select-Object @{n="ComputerName";e={$computerName}},  Version 
}

Free Windows Admin Tool Kit Click here and download it now
August 29th, 2013 6:02am

Thank you. Thats has worked and think i understand the issue now.

August 29th, 2013 6:15am

Odd this now is. If i Export to CSV then it only add's a single computer to the CSV and not all the machines in the .txt file

gc S:\computers.txt | ForEach-Object {
$computerName=$_
Get-WmiObject -Class win32_product -Impersonation 3 -computername $_  -Filter "Name LIKE '%Mimecast%'" |
Select-Object @{n="ComputerName";e={$computerName}},  Version | Export-Csv S:\Mimecast.csv
}

Free Windows Admin Tool Kit Click here and download it now
August 29th, 2013 6:26am

Hi,

you are now creating a new .csv with every iteration of the loop. You'll need to remove the Export-CSV out of the foreach loop:

gc S:\computers.txt | ForEach-Object {
$computerName=$_
Get-WmiObject -Class win32_product -Impersonation 3 -computername $_  -Filter "Name LIKE '%Mimecast%'" | 
Select-Object @{n="ComputerName";e={$computerName}},  Version 
} |  Export-CSV S:\Mimecast.csv -NoTypeInformation



August 29th, 2013 6:43am

Hi,

the $_ at the end of the pipeline within the Select-Object doesn't refer to the emitted computername from get-content anymore. Therefore you'll need to caputer the computername within a separate variable in the foreach block and add it via a calculated property:

gc S:\computers.txt | ForEach-Object {
$computerName=$_
Get-WmiObject -Class win32_product -Impersonation 3 -computername $_  -Filter "Name LIKE '%Mimecast%'" | 
Select-Object @{n="ComputerName";e={$computerName}},  Version 
}


Free Windows Admin Tool Kit Click here and download it now
August 29th, 2013 12:57pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics