Another grep / awk q, parsing diskpart output

后端 未结 2 1615
没有蜡笔的小新
没有蜡笔的小新 2020-12-22 09:12

I\'ve googled this a lot and there are a lot of similar questions but I can\'t figure out how to put them together to make it work for me. Also, the fact that MS decided to

相关标签:
2条回答
  • 2020-12-22 09:57

    diskpart output isn't trimmed, so you can parse the relevant information from the end of the string, e.g. like this:

    $re = 'disk (\d+)\s+(\w+)\s+(\d+ .?b)\s+(\d+ .?b)  (.*)'
    
    'list disk' | diskpart | Select-String -Pattern $re | ForEach-Object {
      New-Object -Type PSObject -Property @{
        ID        = [int]$_.Matches.Groups[1].Value
        Status    = $_.Matches.Groups[2].Value -eq 'online'
        Size      = $_.Matches.Groups[3].Value
        FreeSpace = $_.Matches.Groups[4].Value
        Dynamic   = $_.Matches.Groups[5].Value.Substring(0, 3).Trim() -eq '*'
        GPT       = $_.Matches.Groups[5].Value.Substring(4, 3).Trim() -eq '*'
      }
    }
    
    0 讨论(0)
  • 2020-12-22 10:04

    All I needed was another half hour of googling. https://gallery.technet.microsoft.com/DiskPartexe-Powershell-0f7a1bab

    That script creates 3 objects which have properties I know now to handle

    0 讨论(0)
提交回复
热议问题