从远程计算机获取 powershell 中 .dll 的文件版本

发布于 2024-12-19 21:22:57 字数 1075 浏览 2 评论 0原文

我正在尝试在多台服务器上使用 Powershell 远程获取 dll 的文件版本,并将服务器名称、dll 版本和 dll 文件位置写入 csv 或 html 报告。问题是某些服务器是 Win 2003 和 Win 2008。因此该文件可能驻留在例如 C:\Program Files\WinZip\WZCAB.DLL 或 C:\Program Files (x86)\WinZip\WZCAB.DLL 中。该脚本将检查一个位置,如果不存在,则会检查另一个位置,然后将其写出。有人可以帮我吗?

只是几件事 - 该脚本将用于访问 2003 和 2008 年的 200 多个服务器或 VM。2003 年它需要 Powershell 2.0,2008 年它将及时启用远程处理。我在想也许我需要利用 WMI。我还有另外两个脚本,它们利用 WMI 来获取补丁和重新启动时间。我尝试了 Ravikanth 脚本(再次感谢您),但在传递服务器的 txt 文件时出现以下错误 - 一个或多个计算机名称无效。如果您尝试传递 Uri,请使用 -ConnectionUri 参数或传递 Uri 对象而不是字符串。同样,由于我不会在每台服务器上启用远程处理,是否还有其他方法可以做到这一点?我修改了 Ravikanth 脚本(如下)并在本地尝试过,效果很好。当我远程尝试时,却没有。有什么想法吗?

$servers = "D:\scripts\winzip\servers.txt" 
$x86Path = 'C:\Program Files (x86)\WinZip\WZCAB.DLL'
$x64Path = 'C:\Program Files\WinZip\WZCAB.DLL'

    foreach ($computername in $servers){
        if (Test-Path $x86Path) {
            (Get-Item $x86Path | Select -ExpandProperty VersionInfo).FileVersion
        } elseif (Test-Path $x64Path) {
            (Get-Item $x64Path | Select -ExpandProperty VersionInfo).FileVersion           
        }
    }

I am trying to get the file version of a dll remotely using Powershell on several servers and have write the server name, dll version, and dll file location to a csv or html report. The catch is some of the servers are Win 2003 and Win 2008. So the file may reside in for example C:\Program Files\WinZip\WZCAB.DLL or C:\Program Files (x86)\WinZip\WZCAB.DLL. The script would check one location and if it did not exist it would check the other and then write it out. Can anyone help me out?

Just a few things - This script will be used to access 200+ servers or VM's both 2003 and 2008. On 2003 it needs Powershell 2.0 and 2008 it will be timely to enable remoting. I was thinking maybe I need to utilize WMI. I have two other scripts that utilize WMI to get patches and reboot times. I did though try Ravikanth script (thank you again) and got the following error when passing a txt file of servers - One or more computer names is not valid. If you are trying to pass a Uri, use the -ConnectionUri parameter or pass Uri objects instead of strings. Again since I will not be enabling remoting on each server, is there another way of doing this? I modified Ravikanth script (below) and tried it locally, works greats. When I try it remotely, it does not. Any thoughts?

$servers = "D:\scripts\winzip\servers.txt" 
$x86Path = 'C:\Program Files (x86)\WinZip\WZCAB.DLL'
$x64Path = 'C:\Program Files\WinZip\WZCAB.DLL'

    foreach ($computername in $servers){
        if (Test-Path $x86Path) {
            (Get-Item $x86Path | Select -ExpandProperty VersionInfo).FileVersion
        } elseif (Test-Path $x64Path) {
            (Get-Item $x64Path | Select -ExpandProperty VersionInfo).FileVersion           
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

咆哮 2024-12-26 21:22:57

如果您在这些计算机上启用了远程处理:

$servers = 'server1','server2'
$x86Path = 'C:\Program Files (x86)\WinZip\WZCAB.DLL'
$x64Path = 'C:\Program Files\WinZip\WZCAB.DLL'
Invoke-Command $servers {
    Param ($x86Path, $x64Path)
    if (Test-Path $x86Path) {
        (Get-Item $x86Path | Select -ExpandProperty VersionInfo).FileVersion
    } elseif (Test-Path $x64Path) {
        (Get-Item $x64Path | Select -ExpandProperty VersionInfo).FileVersion           
    }
} -ArgumentList $x86Path,$x64Path

这是执行此操作的 WMI 方式:

$servers = @(Get-Content C:\servers.txt) 
$servers | % { Get-WMIObject -ComputerName $_ -Query "SELECT * FROM CIM_DataFile WHERE Drive ='C:' AND Path='\\Program Files (x86)\\WinZip\\' AND FileName='WZCAB' AND Extension='dll'" } | select Version

这只是您需要的一部分。您需要放置一个 try-catch 块以确保在该路径中找不到文件时处理错误。我会把它留给你的研发部门。 D .

If you have remoting enabled on those computers:

$servers = 'server1','server2'
$x86Path = 'C:\Program Files (x86)\WinZip\WZCAB.DLL'
$x64Path = 'C:\Program Files\WinZip\WZCAB.DLL'
Invoke-Command $servers {
    Param ($x86Path, $x64Path)
    if (Test-Path $x86Path) {
        (Get-Item $x86Path | Select -ExpandProperty VersionInfo).FileVersion
    } elseif (Test-Path $x64Path) {
        (Get-Item $x64Path | Select -ExpandProperty VersionInfo).FileVersion           
    }
} -ArgumentList $x86Path,$x64Path

This is the WMI way of doing it:

$servers = @(Get-Content C:\servers.txt) 
$servers | % { Get-WMIObject -ComputerName $_ -Query "SELECT * FROM CIM_DataFile WHERE Drive ='C:' AND Path='\\Program Files (x86)\\WinZip\\' AND FileName='WZCAB' AND Extension='dll'" } | select Version

This is only part of what you need. You need to put a try-catch block to make sure you handle the errors in case the file is not found at that path. I will leave that for your R & D.

逆光下的微笑 2024-12-26 21:22:57

使用 Test-Path cmdlet 查看该路径是否存在。如果没有,请使用备用路径。

Program Files 路径的差异并非来自 Win 2003 或 2008,而是来自操作系统是 32 位还是 64 位以及应用程序支持 32 位还是 64 位。

Use the Test-Path cmdlet to see if the path exists. If not use the alternate path.

And the difference in the Program Files path doesn't arise from Win 2003 or 2008, but from whether the OS is 32-bit or 64-bit and the application has 32bit or 64bit support.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文