1

I am trying to collect a list of the viewers installed on a set of servers. I am trying to loop through that list and run a wmi query and store the results and export a table with with the wmi result and server name next to it.

I am running this on server 2012

$computers = Get-Content C:\computers.txt

$WMIQuery = foreach ($computer in $computers){Get-WmiObject -Class 
Win32_Product | where-object {$_.name -match "Microsoft Viewer*"}}

$WMIQuery

$WMIQuery | Out-File c:\Viewers.txt

Desired Results

Server Name Object1 Object2

Server1 Microsoft Excel Viewer Microsoft Visio Viewer

I output the file and get a blank txt file.

2 Answers 2

1
foreach ($computer in (Get-Content -Path "C:\computers.txt")) {
    Get-WMIObject -ComputerName $computer -Class Win32_Product |
        Where-Object {$_.name -match "Microsoft Viewer" } |
            Out-File -Append -Path "C:\viewers.txt"
}

Your original code wasn't identifying the computer to perform Get-WMIObject against, so it was looking at only the computer that you were running the script on.

If there are many products on the remote computer, you may want to consider filtering on the remote computer instead of locally, so as to avoid transferring large amounts of data over what may be a slower-than-ideal network:

foreach ($computer in (Get-Content -Path "C:\computers.txt")) {
    Get-WMIObject -ComputerName $computer -Class Win32_Product -Filter "Name LIKE '*Microsoft Viewer*'"|
        Out-File -Append -Path "C:\viewers.txt"
}

(I think I have the filter syntax correct; I seem to have to hack at it every time I write a new filter...)

Sign up to request clarification or add additional context in comments.

4 Comments

When using the filter it produced the following error " Invalid query "select * from Win32_Product where Name LIKE 'Microsoft Office" "
Using the first block of code did give me the results, however it did not give me any information like computer name that i ran it against so i have some formatting to do. I would like it in excel format if possible.
If you want it in Excel format then use Export-CSV -NoTypeInformation -Append -Path "C:\viewers.csv" instead of the Out-File. As far as the filter goes, you must explicitly include the wildcards as I did, and I remind you that I did say that I have to hack at the filter to get it "right" every single time I write a new filter...
I got it into excel, i was wondering if there was a quick way of listing it as row server name, foundobject1, foundobject2 etc. Right now it is listing it as Server1 Object1 Server1 Object2 Server1 Object3 Server2 Object1
1

I don't have enough rep to add a comment, but Jeff is correct. However, there are still issues with the original poster's query. The following piece of code will yield no results based on the examples provided by the poster:

{$_.name -match "Microsoft Viewer*"}

That needs to either be changed to

{$_.name -like "*Microsoft*Viewer*"}

or

{$_.name -match "Microsoft.*?Viewer"}

1 Comment

What's the actual product name? It's not something that we seem to have installed on our systems here, so I had no real way of testing my filter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.