使用powershell从字符串获取子字符串

win*_*nce 1 powershell

我想从MFC*.rc文件中提取verssion编号.看起来像:

  VALUE "FileVersion", "1.22.333.4444\0"
Run Code Online (Sandbox Code Playgroud)

实际上我需要两个值 - 版本1.22.333.4444和主要版本1.22

我写了下面的代码,它给了我版本,但它看起来很难看

  $version = Get-Content -Path $rcPath | Select-String -Pattern 'FileVersion' -CaseSensitive –SimpleMatch -List | %{$_ -replace '[\\0]', ''} | %{$_ -replace '[^\d.]', ''}
Run Code Online (Sandbox Code Playgroud)

所以我的问题是:

  • 有没有简单的方法来获得该版本?
  • 我如何获得主要版本?

mjo*_*nor 6

您可以使用[版本]类型:

$text = 'VALUE "FileVersion", "1.22.333.4444\0"'
$version = [version]($text -replace '^.+?([0-9.]+)\\.+','$1')
$version


Major  Minor  Build  Revision
-----  -----  -----  --------
1      22     333    4444    
Run Code Online (Sandbox Code Playgroud)

然后:

$version.ToString()

1.22.333.4444

'{0}.{1}' -f $version.major,$version.minor

1.22
Run Code Online (Sandbox Code Playgroud)