【问题标题】:Converting Output to CSV and Out-Grid将输出转换为 CSV 和 Out-Grid
【发布时间】:2020-10-16 16:35:08
【问题描述】:

我有一个文件如下。 我希望它将其转换为 CSV,并希望仅对 Drives、Drive Type、Total Space、Current allocation 和 Remaining space 项进行网格视图。

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']
        }
    }
}

【问题讨论】:

  • $fileSys 是单个多行字符串,还是字符串数组?
  • @mathias - 我已经添加了相关文件系统的输出。
  • @GURUSINGH 我认为您误解了 Mathias 的问题。运行$fileSys[0].GetType().FullName 是否返回System.CharSystem.String
  • 或者,首先向我们展示您是如何创建$fileSys
  • $filesys 基本上是存储一个存储盒命令的输出

标签: powershell csv pscustomobject


【解决方案1】:

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

来自documentation

命名匹配子表达式

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

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

$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  

【讨论】:

  • (?=\r) 的两个实例更改为(?=\r?\n)
  • @MathiasR.Jessen-代码在进行更改后工作。是否有可能如果我想要括号中的唯一值,如 135. 9T 和 98.9T
  • 有可能,猜你得等@mathias回答......
  • 通常人们通过投票和/或接受答案来感谢堆栈溢出的帮助。
  • @Doug-朋友我以完全不同的方式解决了我的问题,但我仍然会为你的努力和时间投票赞成你的答案。谢谢大家
【解决方案2】:
$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)
}``

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-03
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    • 2021-01-01
    • 2019-05-01
    • 2014-11-26
    • 2012-05-19
    相关资源
    最近更新 更多