get file version of .dll in powershell from remote computers

12,879

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.

Share:
12,879
user1086732
Author by

user1086732

Updated on June 04, 2022

Comments

  • user1086732
    user1086732 almost 2 years

    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           
            }
        }
    
    • user1086732
      user1086732 over 12 years
      thanks for the quick response..
    • ravikanth
      ravikanth over 12 years
      You code doesn't really access any remote servers. It is actually not even iterting through the collection as you are reading the servers.txt content as an array. There is a WMI way of doing this using CIM_DataFile WMI Class.
  • manojlds
    manojlds over 12 years
    Avoid giving exact code when the OP has not shown what he / she has tried. This is not a place to just get whatever code someone wants, as you might know.
  • Andy Arismendi
    Andy Arismendi over 12 years
    Don't see why not in this case. This is pretty trivial
  • ravikanth
    ravikanth over 12 years
    Why not? There is no harm in offering help to a beginner, IMHO.
  • JasonMArcher
    JasonMArcher over 12 years
    @ravikanth you need to define the two path variables inside the scriptblock, otherwise they don't exist during the remote execution.
  • ravikanth
    ravikanth over 12 years
    @Jason, you are right. I overlooked that as I did not execute this code before putting it here. have updated it to add param()