Skip to content

Instantly share code, notes, and snippets.

@hymkor
Last active October 13, 2017 04:21
Show Gist options
  • Save hymkor/53730d6955c10004afa9c27b0c656b98 to your computer and use it in GitHub Desktop.
Save hymkor/53730d6955c10004afa9c27b0c656b98 to your computer and use it in GitHub Desktop.
Windowsの実行ファイルのバージョン情報、bit数、md5sum を表示するバッチファイル(PowerShellコード内蔵)
@set args=%*
@powershell "iex ((@('')*3+(cat '%~f0' | select -skip 3))-join [char]10)"
@exit /b %ERRORLEVEL%
function Byte2DWord($a,$b,$c,$d){
return ($a+256*($b+256*($c+256*$d)))
}
function Get-Bits($path){
$bin = [System.IO.File]::ReadAllBytes($path)
$addr = (Byte2DWord $bin[60] $bin[61] $bin[62] $bin[63])
if( $bin[$addr] -eq 0x50 -and $bin[$addr+1] -eq 0x45 ){
if( $bin[$addr+4] -eq 0x4C -and $bin[$addr+5 ] -eq 0x01 ){
return 32
}
if( $bin[$addr+4] -eq 0x64 -and $bin[$addr+5] -eq 0x86 ){
return 64
}
}
return $null
}
function Split-Quote($s){
$rx = [regex]'"[^"]*"'
while( $true ){
$m = $rx.Match($s)
if( -not $m.Success ){
break
}
$left = $s.SubString(0,$m.Index)
$right = $s.SubString($m.Index+$m.Length)
$mid = (($m.Value -replace " ",[char]1) -replace '"','')
$s = $left + $mid + $right
}
($s -split " ") |
ForEach-Object{ $_ -replace [char]1," " }
}
$args = ( Split-Quote($env:args) |
ForEach-Object {
if( $_ -match "\*" -or $_ -match "\?" ){
Get-ChildItem $_ |
Where-Object { $_.Mode -notlike 'd*' } |
ForEach-Object { Write-Output $_.FullName }
}else{
Write-Output $_
}
} | Where-Object{ $_ -ne $null } )
foreach($fname in $args ){
if( Test-Path $fname ){
Write-Output ([string]$fname)
$v = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($fname)
if( $v ){
$bits = (Get-Bits $fname)
$bits = switch( $bits ){
32 { "32bit or AnyCPU" }
64 { "64bit" }
$null { "unknown"}
}
Write-Output (" Architecture: {0}" -f $bits)
Write-Output (" FileVersion: `"{0}`" ({1},{2},{3},{4})" -f
$v.FileVersion,
$v.FileMajorPart,
$v.FileMinorPart,
$v.FileBuildPart,
$v.FilePrivatePart)
Write-Output (" ProductVersion: `"{0}`" ({1},{2},{3},{4})" -f
$v.ProductVersion,
$v.ProductMajorPart,
$v.ProductMinorPart,
$v.ProductBuildPart,
$v.ProductPrivatePart)
}
$data = [System.IO.File]::ReadAllBytes($fname)
$md5 = New-Object System.Security.Cryptography.MD5CryptoServiceProvider
$bs = $md5.ComputeHash($data)
Write-Output (" md5sum: {0}" -f ([System.BitConverter]::ToString($bs).ToLower() -replace "-",""))
Write-Output ""
}else{
Write-Output ("{0}: not found" -f $fname)
}
}
# vim:set ft=ps1:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment