获取远程设备的 Chrome 版本 [英] Get Chrome version for remote devices
问题描述
我正在使用 PS V3.0 检查 Google chrome 版本:
get-content -Path C:\Support\assets.txt |ForEach-Object ({get-wmiobject win32_product -ComputerName $_ | where-Object {$_.name -eq "google chrome"} |FT 版本}){写主机$_"}
txt 文件中包含远程设备的 IP 地址.
该命令运行良好,并为我提供了 Chrome 版本,但我无法在循环中包含 IP 地址.
它只给我 chrome 版本,没有它来自哪个远程设备的信息.我想得到这样的东西:
<前>IP 地址 - Chrome 版本IP 地址 - Chrome 版本据我所知应该是 Foreach (action){do something}
?
还有机会从输入中删除单词version"吗?
好吧,你犯了一些非常无辜的初学者类型错误,但这很容易纠正.
让我们从 ft
开始.ft
是 Format-Table
的缩写.一般来说,当您尝试输出某些内容时,您只使用 Format-
命令.你试图使用它的结果,而不是输出它,所以我们需要删除 ft
.而是使用 Select-Object
cmdlet(或更常用的是较短的 select
).
get-content -Path C:\Support\assets.txt |ForEach-Object ({get-wmiobject win32_product -ComputerName $_ | where-Object {$_.name -eq "google chrome"} |选择版本})
好的,这会为您提供一个只有 Version
属性的对象数组.不是很有用,尤其是当您想知道每个都与哪台计算机相关联时!所以,总的来说,这是一个很好的教训,但在这里不是很实用.让我们继续让事情变得更好!
通过将事情传送到这样的 ForEach
循环,您使事情变得比需要的更难.您正在针对每个 IP 地址进行单独的 Get-WMIObject
调用.如果我们查看 get-help get-wmiobject -parameter computername
,我们可以看到它接受一个字符串数组.所以我们可以对多个目标进行 1 次调用,这应该有助于加快速度.
$IPList = get-content -Path C:\Support\assets.txtGet-WMIObject win32_product -ComputerName $IPList |哪里{$_.Name -eq '谷歌浏览器'}
这应该会稍微加快你的结果,但是让事情变得更快的是使用 Get-WMIObject
的 -Filter
参数而不是 哪里
.原因是提供程序在过滤自己的对象并返回您想要的内容方面比 PowerShell 更有效地过滤事物.此外,这减少了从远程机器发回的数据,因此您只能从它们那里获取您想要的数据,而不是每台机器可能有数百个结果,然后解析为您想要的数据.从本质上讲,您可以让所有计算机的处理器来解决您的问题,而不仅仅是您的问题.所以让我们使用 -Filter
参数:
$IPList = get-content -Path C:\Support\assets.txtGet-WMIObject win32_product -ComputerName $IPList -Filter "Name='Google Chrome'"
好的,现在应该恢复得更快了.因此,直到最后一项,您都需要每个版本所在的计算机名称.好消息,你已经拥有了!好吧,我们有计算机的实际名称,而不是您指定的 IP 地址.默认情况下不显示它,但每个结果都有一个您可以参考的 PSComputerName
属性.我们可以简单地通过管道传递到 Select
并指定我们想要的属性:
$IPList = get-content -Path C:\Support\assets.txtGet-WMIObject win32_product -ComputerName $IPList -Filter "Name='Google Chrome'" |选择 PSComputerName,Version
这可能会给您带来满意的结果.如果没有,您可以像以前一样通过 ForEach
循环运行它,并按照您指定的方式对其进行格式化:
$IPList = get-content -Path C:\Support\assets.txtForEach($IPList 中的 $IP){$Version = Get-WMIObject win32_product -ComputerName $IP -Filter "Name='Google Chrome'" |选择 - 扩展版本$IP - $Version"}
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 "$_"}
Inside the txt file are the IP addresses of remote devices.
The command is working fine and gives me Chrome version, but I cannot include IP address inside the loop.
It gives me only chrome version without information to which remote device it's from. I would like to get something like this :
IP address - Chrome version IP address - Chrome version
As far as I understand this it should be Foreach (action){do something}
?
Also is there any chance to remove word "version" from the input?
Ok, you've made some very innocent beginner type mistakes, but that's fairly easily remedied.
Let's start with ft
. ft
is short for Format-Table
. Generally speaking, you only use a Format-
command when you are trying to output something. You are trying to use the results of that, not output it, so we need to drop the ft
. Instead use the Select-Object
cmdlet (or more commonly used is the shorter select
).
get-content -Path C:\Support\assets.txt | ForEach-Object ({get-wmiobject win32_product -ComputerName $_ | where-Object {$_.name -eq "google chrome"} |Select version})
Ok, that gets you an array of objects that only have the Version
property. Not super useful, especially when you wanted to know what computer each is associated with! So, that's a good lesson in general, but not very practical here. Let's move on with actually making things better!
You are making things harder than need be by piping things to a ForEach
loop like that. You are making separate Get-WMIObject
calls against each IP address. If we look at get-help get-wmiobject -parameter computername
we can see that it accepts an array of strings. So we can make 1 call against multiple targets, which should help speed things up a bit.
$IPList = get-content -Path C:\Support\assets.txt
Get-WMIObject win32_product -ComputerName $IPList | Where{$_.Name -eq 'Google Chrome'}
That should speed up your results a bit, but what will make things a whole lot faster is to use Get-WMIObject
's -Filter
parameter instead of Where
. The reason is that the provider is more efficient at filtering its own objects, and returning just what you want, than PowerShell is as filtering things. Also, this reduces the data sent back from the remote machines, so you are only getting the data you want from them rather than potentially hundreds of results per machine, and then parsing down to just the ones you want. Essentially you have all of the computers' processors working on your problem, instead of just yours. So let's use the -Filter
parameter:
$IPList = get-content -Path C:\Support\assets.txt
Get-WMIObject win32_product -ComputerName $IPList -Filter "Name='Google Chrome'"
Ok, things should come back a whole lot faster now. So down to the last item, you want the computer name for what each version was found on. Good news, you already have it! Well, we have the actual name of the computer, not the IP address that you specified. It is not displayed by default, but each one of those results has a PSComputerName
property that you can refer to. We can simply pipe to Select
and specify the properties that we want:
$IPList = get-content -Path C:\Support\assets.txt
Get-WMIObject win32_product -ComputerName $IPList -Filter "Name='Google Chrome'" | Select PSComputerName,Version
That's probably going to get you results that you're happy with. If not, you can run it through a ForEach
loop similarly to how you were, and format it like you specified:
$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"
}
这篇关于获取远程设备的 Chrome 版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!