#============================================================================== # Author: Gunther Pippèrr ( http://www.pipperr.de ) # Desc: Search for Jar and show the version # Date: Jan 2021 # Site: https://github.com/gpipperr/OraPowerShell # The code is based on an answers at https://stackoverflow.com/a/37561878/101151 # Check if the version is inside the Jar! # & "C:\Program Files\Java\jdk1.8.0_191\bin\jar.exe" xvf junit-4.10.jar META-INF/MANIFEST.MF # get-content .\META-INF\MANIFEST.MF # #============================================================================== <# Security: (see http://www.pipperr.de/dokuwiki/doku.php?id=windows:powershell_script_aufrufen ) To switch it off (as administrator) get-Executionpolicy -list set-ExecutionPolicy -scope CurrentUser RemoteSigned .NOTES Created: 01.2021 : Gunther Pippèrr (c) http://www.pipperr.de .SYNOPSIS Search for Jar Files and read version .DESCRIPTION Search for Jar Files and read version over the manifest information .COMPONENT Oracle GPI Script Lib .EXAMPLE Backup the Database .\checkJarVersion.ps1. log4j-core-2.9.1.jar #> param( [Parameter(Mandatory=$true)][string]$startDirectory ) Add-Type -assembly "system.io.compression.filesystem" $Invocation = (Get-Variable MyInvocation -Scope 0).Value $scriptpath=Split-Path $Invocation.MyCommand.Path $starttime=get-date #Output File $logfile_name=$scriptpath+"\search_jar_result_"+$env:COMPUTERNAME+".csv" function searchVersion{ param ( [String] $jarfile , [String] $jardir , [String] $jarname ) try { if (![System.IO.File]::Exists($jarfile)) { $version=-1 throw "Jar file ""$jarfile"" not found." } $jar = [io.compression.zipfile]::OpenRead($jarfile) $file = $jar.Entries | where-object { $_.Name -eq "MANIFEST.MF"} if ($file) { $stream = $file.Open() $reader = New-Object IO.StreamReader($stream) $text = $reader.ReadToEnd() $stream.Close() $lines =$text -split "`r`n" $version_line=$lines | Select-String -InputObject {$_} -Pattern 'Implementation-Version' $version = $version_line -split "`r`n" $version = $version -replace "Implementation-Version:",""; } else { $version=-1 } echo "$jardir $jarname $version" } catch { echo "$jardir $jarname -1" } finally { if ($reader) { $reader.Dispose() } if ($jar) { $jar.Dispose() } } } # check if \ is missing if ($startDirectory -notmatch '\\$') { $startDirectory += '\' } $dir_param="$startDirectory"+"log4*.jar"+" /a-d /-c /s /b" # Search over the directory echo "--------------------------------------------------------------" echo "Start to search for Log4J Jars at $starttime" echo "" echo "Parameter startDirectory : $startDirectory" echo "Use cmd file search to speed : $dir_param " echo "" echo "Write results to : $logfile_name" echo "--------------------------------------------------------------" # to slow need 7min to 1 min with cmd #$files=Get-ChildItem -Path $startDirectory -Include log4*.jar -File -Recurse -ErrorAction SilentlyContinue #https://dmfrsecurity.com/2021/08/23/get-childitem-performance/ # much better performance $files=(cmd /c "dir $dir_param" ) #init logfile echo "Directory JarName Version"> $logfile_name $file_count=0 $searchtime=get-date $duration = [System.Math]::Round(($searchtime- $starttime).TotalMinutes,2) echo "Start to check Jars at $searchtime - need for search :: $duration Minutes" echo "--------------------------------------------------------------" #check jahrs foreach ( $file in $files ) { if ($file) { $fpath = Split-Path $file $fName = Split-Path $file -Leaf echo "Start to check $fName at directory $fpath" searchVersion -jarfile $file -jardir $fpath -jarname $fName >> $logfile_name $file_count=$file_count+1 } else { echo "$file is empty" } } echo "--------------------------------------------------------------" $endtime=get-date $duration = [System.Math]::Round(($endtime- $starttime).TotalMinutes,2) echo "Finish Search $file_count Jars from $starttime until $endtime - Duration:: $duration Minutes" echo "Check $logfile_name for results" echo "-----------------------Finish-------------------------------"