1

I need to read all file version for each dll and exe files that located inside a zip. This is what I have so far. I was able to hold a dll but there is no .fileversion for the object

$vips = Get-ChildItem -path C:\_Projects\temp\Patch_1296247.vip
$checkfiles= "*.dll"
#C:\_Projects\temp\Patch_1296247.vip

foreach ($file in $vips) {
    try {
        $zip = [System.IO.Compression.ZipFile]::Open($file, "Read")
    }
    catch {
        Write-Warning $_.Exception.Message
        continue
    }
    $dlls = $zip.Entries.Where({ $_.Name -like "*.dll" })
    foreach($dll in $dlls) {

    $dll.FileVersion
    $zip.Dispose()}}

The dll have only those properties

enter image description here

If I run this line I can get the dll version.Maybe because it is not in a ZIP? How can I add it to my script?

(Get-Item C:\_Projects\temp\Patch_1296247\ADWalk_4.0.0.101\JobAdWalk\AdWalk\a.Client.ServicesProvider.dll).VersionInfo.FileVersion

Its ok to write the if like that? I want to print all dlls with 1.0.0.0 and 0.0.0.0 I get that all dlls is ok although all of them with 1.0.0.0 or 0.0.0.0 (($dll.VersionInfo) -eq "1.0.0.0") --> False ?!?!

Maybe it should be like this?

if ((($dll.VersionInfo) -eq "1.0.0.0") -or (($dll.VersionInfo) -eq"0.0.0.0")

Or should be:

if (($dll.VersionInfo) -eq "1.0.0.0")
{}
elseif (($dll.VersionInfo) -eq "0.0.0.0")
{}



foreach ($file in $vips) {
    try {
        $zip = [System.IO.Compression.ZipFile]::ExtractToDirectory($file, $tempFolder)
        $dlls = Get-ChildItem $tempFolder -Include "*.dll","*.exe" -Recurse
        foreach($dll in $dlls) {
            #$dll.VersionInfo
        if (($dll.VersionInfo) -eq "1.0.0.0" -or "0.0.0.0")
        {
            write-host
            Write-host "The version of $($dll.Name) is wrong!!!" -ForegroundColor Red
          
        }
        else {write-host "All dlls are ok"}
        }
    }
Bandit
  • 399
  • 3
  • 10
  • I am not aware of a file system that stores metadata like "fileversion". So maybe you need a tool to read such metadata especially from DLL files. – stackprotector Mar 04 '22 at 07:07
  • You have to temporarily extract the file to [get the file version](https://stackoverflow.com/q/30686/7571258). Theoretically you could do it without temp file by parsing it from the stream of the ZIP entry in-memory, if you find an external library for this. – zett42 Mar 04 '22 at 09:02
  • Sounds great but I don't know how to do it – Bandit Mar 04 '22 at 09:22

1 Answers1

0

The $dll in your example has System.IO.Compression.ZipArchiveEntry type. This type doesn't have VersionInfo property. This property is member of System.IO.FileInfo type. Thus, you need firstly to convert the zipped file into the System.IO.FileInfo type and then you can read your property.

You can do it for example by extracting the archive to temporary directory and then clean-up:

$vips = Get-ChildItem -path C:\_Projects\temp\Patch_1296247.vip
$checkfiles= "*.dll"
$tempFolder = "c:\temp\tempzip"

foreach ($file in $vips) {
    try {
        $zip = [System.IO.Compression.ZipFile]::ExtractToDirectory($file, $tempFolder)
        $dlls = Get-ChildItem $tempFolder "*.dll" 
        foreach($dll in $dlls) {
            $dll.VersionInfo
        }
    }
    catch {
        Write-Warning $_.Exception.Message
        continue
    }
    finally {
        Remove-Item $tempFolder -Force -Recurse
    }    
}
WiktorM
  • 61
  • 2
  • 2
    Nice! The code could be slightly improved by not hard-coding the temp directory, e. g. `$tempFolder = Join-Path ([IO.Path]::GetTempPath()) (New-GUID).ToString('n')` – zett42 Mar 04 '22 at 09:46
  • What the ('n') do? – Bandit Mar 04 '22 at 11:36
  • This line is opening many folders in temp without deletion $tempFolder = Join-Path ([IO.Path]::GetTempPath()) (New-GUID).ToString('n') – Bandit Mar 05 '22 at 12:24