Get Chrome version for remote devices

I am using PS V3.0 to check google chrome version:

get-content -Path C:\Support\assets.txt | ForEach-Object ({get-wmiobject win32_product -ComputerName $_  | where-Object {$_.name -eq "google chrome"}  |FT version})

{write-host "$_"}

      

The txt file contains the IP addresses of the remote devices.

The command works fine and gives me the Chrome version, but I cannot include the IP address inside the loop.

This only gives me the chrome version without the information that the remote device is with. I would like to get something like this:

IP address - Chrome version
IP address - Chrome version

As I understand it, it should be Foreach (action){do something}

?

Also is it possible to remove the word "version" from the input?

+3


source to share


1 answer


Okay, you've made some very innocent beginner-type mistakes, but that's pretty easy to fix.

Let's start with ft

. ft

shortened for Format-Table

. Generally speaking, you use a command Format-

when you are trying to output something. You are trying to use the results of this, not output it, so we need to abandon ft

. Use a cmdlet instead Select-Object

(or more often a shorter one select

).

get-content -Path C:\Support\assets.txt | ForEach-Object ({get-wmiobject win32_product -ComputerName $_  | where-Object {$_.name -eq "google chrome"}  |Select version})

      

Ok, this gives you an array of objects that have a property Version

. Not very useful, especially when you wanted to know which computer you are connected to! So, a good lesson overall, but not very practical. Let's move on making things better!

You are making things harder than necessary by tying things to such a loop ForEach

. You make separate calls Get-WMIObject

against each IP address. If we look at get-help get-wmiobject -parameter computername

, we can see that it takes an array of strings. Thus, we can make 1 challenge against multiple targets, which should speed up the process.

$IPList = get-content -Path C:\Support\assets.txt
Get-WMIObject win32_product -ComputerName $IPList | Where{$_.Name -eq 'Google Chrome'}

      



This should speed up your results a bit, but what will make things a lot faster is using a parameter Get-WMIObject

-Filter

instead Where

. The reason is that the provider filters its own objects more efficiently and returns exactly what you want than PowerShell as filtering. Plus, it reduces the data sent from remote computers, so you only get the data you want, rather than potentially hundreds of results per machine, then parse only the ones you want. Basically, you have all the computer processors working on your problem, not just yours. So use the parameter -Filter

:

$IPList = get-content -Path C:\Support\assets.txt
Get-WMIObject win32_product -ComputerName $IPList -Filter "Name='Google Chrome'"

      

Okay, now it should be a lot faster. So, down to the last item, you need the computer name for each version. Good news, you already have it! Well, we have the actual computer name, not the IP address you provided. It is not displayed by default, but each of these results has a property PSComputerName

that you can refer to. We can just connect to select

and specify the properties we want:

$IPList = get-content -Path C:\Support\assets.txt
Get-WMIObject win32_product -ComputerName $IPList -Filter "Name='Google Chrome'" | Select PSComputerName,Version

      

You will probably get results that you are happy with. If not, you can run it through a loop ForEach

similar to the way you were and format it as you pointed out:

$IPList = get-content -Path C:\Support\assets.txt
ForEach($IP in $IPList){
    $Version = Get-WMIObject win32_product -ComputerName $IP -Filter "Name='Google Chrome'" | Select -Expand Version
    "$IP - $Version"
}

      

+2


source







All Articles