-1

我有一个文件如下。我希望它将其转换为 CSV,并希望仅对项目驱动器、驱动器类型、总空间、当前分配和剩余空间拥有它的网格视图。

PS C:\> echo $fileSys
Storage system address: 127.0.0.1
Storage system port: 443
HTTPS connection

1:    Name               = Extreme Performance
      Drives             = 46 x 3.8T SAS Flash 4
      Drive type         = SAS Flash
      RAID level         = 5
      Stripe length      = 13
      Total space        = 149464056594432 (135.9T)
      Current allocation = 108824270733312 (98.9T)
      Remaining space    = 40639785861120 (36.9T)

我是 Powershell 的新手,但我已经尝试了以下两件事的代码,但它甚至没有得到我想要的输出。

$filesys | ForEach-Object {
    if ($_ -match '^.+?(?<Total space>[0-9A-F]{4}\.[0-9A-F]{4}\.[0-9A-F]{4}).+?(?<Current allocation>\d+)$') {
        [PsCustomObject]@{
            'Total space'  = $matches['Total space']
            'Current allocation' = $matches['Current allocation']
        }
    }
}
4

2 回答 2

1

首先,命名的捕获组不能包含空格。

文档

命名匹配子表达式

其中 name 是一个有效的组名,而 subexpression 是任何有效的正则表达式模式。name 不能包含任何标点符号,并且不能以数字开头。

假设这是一个字符串,因为您的模式尝试从多行中获取信息,您可以放弃循环。但是,即使纠正了这一点,您的模式似乎也与数据不匹配。我不清楚您要匹配什么或您想要的输出。希望这会让你走上正轨。

$filesys = @'
Storage system address: 127.0.0.1
Storage system port: 443
HTTPS connection

1:    Name               = Extreme Performance
      Drives             = 46 x 3.8T SAS Flash 4
      Drive type         = SAS Flash
      RAID level         = 5
      Stripe length      = 13
      Total space        = 149464056594432 (135.9T)
      Current allocation = 108824270733312 (98.9T)
      Remaining space    = 40639785861120 (36.9T)
'@

if($filesys -match '(?s).+total space\s+=\s(?<totalspace>.+?)(?=\r?\n).+allocation\s+=\s(?<currentallocation>.+?)(?=\r?\n)')
{
    [PsCustomObject]@{
        'Total space'  = $matches['totalspace']
        'Current allocation' = $matches['currentallocation']
    }
}

Total space              Current allocation     
-----------              ------------------     
149464056594432 (135.9T) 108824270733312 (98.9T)

编辑

如果您只想要括号中的值,则修改为此即可。

if($filesys -match '(?s).+total space.+\((?<totalspace>.+?)(?=\)).+allocation.+\((?<currentallocation>.+?)(?=\))')
{
    [PsCustomObject]@{
        'Total space'  = $matches['totalspace']
        'Current allocation' = $matches['currentallocation']
    }
}

Total space Current allocation
----------- ------------------
135.9T      36.9T  
于 2020-10-16T18:53:17.420 回答
0
$unity=[Regex]::Matches($filesys, "\(([^)]*)\)") -replace '[(\)]',''  -replace "T",""

$UnityCapacity = [pscustomobject][ordered] @{

Name = "$Display"

"Total" =$unity[0]

"Used" = $unity[1]

"Free" = $unity[2]

'Used %' = [math]::Round(($unity[1] / $unity[0])*100,2)
}``
于 2020-10-17T08:18:44.170 回答