Tampilkan postingan dengan label FileVersionInfo. Tampilkan semua postingan
Tampilkan postingan dengan label FileVersionInfo. Tampilkan semua postingan

Sabtu, 10 Desember 2011

FileVersionInfo Part II

# Powershell v3.0 code
#
Recurses current directory to gather file version information of a boolean property
#
Returns number of Debug,Patched,PreRelease,Private,Special builds
#
Creates csv of those properties in current directory
#
Takes up to three arguments:
#
[mandatory]$filename (e.g. *.dll),$exportflag (e.g. "0" to output csv;default is off), $filetime (default is now)

function Global:Get-fileinfo {
[CmdletBinding()]
Param(
[Parameter(ValueFromPipeline=$true)]
[object]$filename,
[bool]$exportflag=1,
$filetime=[DateTime]::Now.ToFileTime()
)

$Files=ls -Filter $filename -recurse -File
## $Files=ls -ea 0 -Filter $filename -recurse #remove '-File' to create 2.0 code. Add '-ea 0' as desired.

$FileInfo=$Files |
% {[System.Diagnostics.FileVersionInfo]::GetVersionInfo("$(($_.DirectoryName)+"\"+($_.Name))")}


$Global:DebugBuild=$FileInfo | % {if ($_.IsDebug) {$_}}
$Global:PatchedBuild=$FileInfo | % {if ($_.IsPatched) {$_}}
$Global:PrereleaseBuild=$FileInfo | % {if ($_.IsPreRelease) {$_}}
$Global:PrivateBuild=$FileInfo | % {if ($_.IsPrivateBuild) {$_}}
$Global:SpecialBuild=$FileInfo | % {if ($_.IsSpecialBuild) {$_}}

[hashtable]$Global:Report=@{
"DebugBuild" = '$DebugBuild';
"PatchedBuild" = '$PatchedBuild';
"PrereleaseBuild" = '$PrereleaseBuild';
"PrivateBuild" = '$PrivateBuild';
"SpecialBuild" = '$SpecialBuild' }

if ($exportflag -eq 0)
{
[array]$hasharray=foreach ($i in $Report){$i.Values}
foreach ($i in $hasharray) {invoke-expression $($i.trimEnd("$")) | Export-Csv -ea 0 -Path $filetime$i.csv }
}

write "Total files: $(($Files).count)"
write "Marked Debug: $(($DebugBuild).count)"
write "Marked Patched: $(($PatchedBuild).count)"
write "Marked Prerelease: $(($PrereleaseBuild).count)"
write "Marked Private: $(($PrivateBuild).count)"
write "Marked Special: $(($SpecialBuild).count)"
}

FileVersionInfo Part I

Retrieving FileVersionInfo in Powershell involves calling [System.Diagnostics.FileVersionInfo]::GetVersionInfo(). "ls ' or 'Get-childitem' has a scriptproperty named "VersionInfo" that can be used for this:



PS C:\ps1> $a=ls -recurse | % {$_.VersionInfo}


TypeName   : System.IO.FileInfo
Name       : VersionInfo
MemberType : ScriptProperty
Definition : System.Object VersionInfo {get=[System.Diagnostics.FileVersionInfo]::GetVersionInfo($this.FullName);}


System.Diagnostics.FileVersionInfo contains five boolean properties for Debug,Patched,PreRelease,Private,Special builds:


PS C:\ps1> $a | gm




   TypeName: System.Diagnostics.FileVersionInfo


Name               MemberType Definition
----               ---------- ----------
Equals             Method     bool Equals(System.Object obj)
GetHashCode        Method     int GetHashCode()
GetType            Method     type GetType()
ToString           Method     string ToString()
Comments           Property   System.String Comments {get;}
CompanyName        Property   System.String CompanyName {get;}
FileBuildPart      Property   System.Int32 FileBuildPart {get;}
FileDescription    Property   System.String FileDescription {get;}
FileMajorPart      Property   System.Int32 FileMajorPart {get;}
FileMinorPart      Property   System.Int32 FileMinorPart {get;}
FileName           Property   System.String FileName {get;}
FilePrivatePart    Property   System.Int32 FilePrivatePart {get;}
FileVersion        Property   System.String FileVersion {get;}
InternalName       Property   System.String InternalName {get;}
IsDebug            Property   System.Boolean IsDebug {get;}
IsPatched          Property   System.Boolean IsPatched {get;}
IsPreRelease       Property   System.Boolean IsPreRelease {get;}
IsPrivateBuild     Property   System.Boolean IsPrivateBuild {get;}
IsSpecialBuild     Property   System.Boolean IsSpecialBuild {get;}
Language           Property   System.String Language {get;}
LegalCopyright     Property   System.String LegalCopyright {get;}
LegalTrademarks    Property   System.String LegalTrademarks {get;}
OriginalFilename   Property   System.String OriginalFilename {get;}
PrivateBuild       Property   System.String PrivateBuild {get;}
ProductBuildPart   Property   System.Int32 ProductBuildPart {get;}
ProductMajorPart   Property   System.Int32 ProductMajorPart {get;}
ProductMinorPart   Property   System.Int32 ProductMinorPart {get;}
ProductName        Property   System.String ProductName {get;}
ProductPrivatePart Property   System.Int32 ProductPrivatePart {get;}
ProductVersion     Property   System.String ProductVersion {get;}
SpecialBuild       Property   System.String SpecialBuild {get;}


We can select for these booleans easy enough:


PS C:\ps1> $a | Select Filename,Is* | fl *| more
{ls -recurse | % {$_.VersionInfo} | Select Filename,Is* | fl *| more}


FileName       : C:\ps1\CTPv3\app.config
IsDebug        : False
IsPatched      : False
IsPrivateBuild : False
IsPreRelease   : False
IsSpecialBuild : False


FileName       : C:\ps1\CTPv3\AssemblyInfo.cs
IsDebug        : False
IsPatched      : False
IsPrivateBuild : False
IsPreRelease   : False
IsSpecialBuild : False


...