问题
so right now I am working in Powershell and am working with my first Hash Table. So with my code I have a variable that gets the Metadata of the file and I want that variable to pass through a hash table to get to shorten the Metadata. For example the file I'm working with is version v2.0.50727 which I want to turn into "lib\net20" I know the set up I have now doesn't work, I think I need to do a substring to get what I want but I have no idea how to set that up.
$retCode = 0
write-verbose "Getting version of $file..."
$version = (Get-Item $File).VersionInfo.FileVersion
$id = $file.Substring(0, $File.LastIndexof('.'))
$filepath = Get-ChildItem "$File"
$netVer = ildasm /text $File| findstr Metadata
$OriginalFilename = (Get-Item $File).VersionInfo.OriginalFilename
#Some type of substring to turn v2.0.50727 into 2.0
$netVerConv = @{2.0 = "lib\net20";}
I thinking that I need some type of foreach loop but I honestly have no idea here. Ultimately the Hash Table will look something like this
$netVerConv = @{
2.0 = "lib\net20";
3.0 = "lib\net30";
3.5 = "lib\net35";
4.0 = "lib\net40";
}
回答1:
Try this:
$file = 'C:\windows\explorer.exe'
$version = (Get-Item $file).VersionInfo
$ShortVer = '{0}.{1}'-f $version.FileMajorPart,$version.FileMinorPart
$ShortVer
6.1
来源:https://stackoverflow.com/questions/27510561/have-a-variable-run-through-a-hash-table-in-powershell