Have a variable run through a hash table in Powershell

大城市里の小女人 提交于 2020-01-06 14:47:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!