Home > Mobile >  PowerShell - Read file version from all files in zip
PowerShell - Read file version from all files in zip

Time:03-05

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 ?!?!

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"}
        }
    }

CodePudding user response:

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
    }    
}
  • Related