简体   繁体   English

获取远程计算机上已安装软件的版本

[英]Get version of Installed Software on remote machine

I am trying a write an application to fetch the version of the application installed on remote machines.我正在尝试编写一个应用程序来获取安装在远程机器上的应用程序版本。 There is a need to query many remote servers and get the version of the application and show it on the dashboard.需要查询许多远程服务器并获取应用程序的版本并将其显示在仪表板上。 Powershell WMI takes too long to get the information. Powershell WMI 需要很长时间才能获取信息。 I am looking for something lot faster.我正在寻找更快的东西。

The app will read remote server information like IP, Username, and password from a config file and fetch the data.该应用程序将从配置文件中读取远程服务器信息,例如 IP、用户名和密码,并获取数据。

Any help is really appreciated.非常感谢任何帮助。

It sounds like you want to take a closer look at Powershell Sessions .听起来您想仔细看看Powershell Sessions

There are at least two ways to approach in from there, one is using Invoke-Command in combination with the -ComputerName attribute, possibly along with -Authentication or -Credential .至少有两种方法可以从那里进入,一种是将Invoke-Command-ComputerName属性结合使用,可能与-Authentication-Credential一起使用。 -ScriptBlock contains the code you want to run. -ScriptBlock包含您要运行的代码。

 Invoke-Command -ComputerName "computername.domain.local" -ScriptBlock {... }

I assume from "the application" that your concern is one application, and not every application.我从“应用程序”中假设您关注的是一个应用程序,而不是每个应用程序。 Then you should be able to tell the version by running Get-Item on the executable, then look at either VersionInfo.ProductVersion or VersionInfo.FileVersion , whichever is more relevant to your case.然后您应该能够通过在可执行文件上运行Get-Item来判断版本,然后查看与您的情况更相关的VersionInfo.ProductVersionVersionInfo.FileVersion

To access one of them, you could use something like:要访问其中一个,您可以使用以下内容:

 $version = (Get-Item "path-to-executable\executable.exe').VersionInfo.ProductVersion

To find out which attributes are relevant to your executable, you can run要找出哪些属性与您的可执行文件相关,您可以运行

Get-Item "executable.exe" | Select -ExpandProperty VersionInfo | Format-List *

Combining these techniques, you could try something like this.结合这些技术,你可以尝试这样的事情。

 # this is a dummy array for example purposes $computers = @(@{'ip' = '127.0.0.1'; 'username' = 'admin'; 'password' = 'password'}) foreach($computer in $computers) { # creating a PSCredential object from plain text passwords is not a good practice, but I'm assuming here that's what you've got to work with $credentials = [System.Management.Automation.PSCredential]::new($computer.username, (ConvertTo-SecureString -String $computer.password -AsPlainText -Force)) # fetch versioninfo info from remote computer $versioninfo = Invoke-Command -ComputerName $computer.ip -Credential $credentials -ScriptBlock { Get-Item "executable.exe" | Select -ExpandProperty VersionInfo if ($versioninfo.ProductVersion -ne '3.1.2414.0') { # do something if product version isn't 3.1.2414.0 } if ($versioninfo.ProductVersionRaw.Major -lt 5) { # do something if product version major part is less than 5 (true for 1.5.5.5 but false for 5.1.1.1) } }

If you want to run several commands on the client computers, use New-PSSession and pass the session along to every call to Invoke-Command , otherwise you'd lose time and resources opening a new session every time.如果要在客户端计算机上运行多个命令,请使用New-PSSession并将 session 传递给对Invoke-Command的每次调用,否则每次打开新的 session 都会浪费时间和资源。 Here's an example on how that could be achieved:这是一个如何实现的示例:

 $session = New-PSSession -ComputerName $computer.ip -Credential $credentials $versioninfo = Invoke-Command -Session $session -ScriptBlock { # do something } if ($versioninfo.ProductVersion -lt 1) { Invoke-Command -Session $session -ScriptBlock { # do something else } } Remove-PSSession -Session $session

You might also want to check out the using: scope modifier if you find a need to pass variables along to the remote computer, which would make $localvariable visible at the remote computer with $using:localvariable (readonly)如果您发现需要将变量传递给远程计算机,您可能还想查看using: scope 修饰符,这将使$localvariable在远程计算机上可见$using:localvariable (只读)

If time is still a concern after this (especially with tcp timeouts on offline hosts), then threading is the next topic you'd want to look into.如果在此之后时间仍然是一个问题(尤其是离线主机上的 tcp 超时),那么线程是您要研究的下一个主题。

As far as I know, my code is compatible with Powershell v3.1, but I recommend using no less than v5, especially on the machine running the script.据我所知,我的代码与 Powershell v3.1 兼容,但我建议使用不低于 v5,尤其是在运行脚本的机器上。

This should be enough information to send you on your way.这应该足以在途中向您发送信息。 Good luck.:)祝你好运。:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM