另一个grep/awk q,解析diskpart输出 [英] Another grep / awk q, parsing diskpart output

查看:55
本文介绍了另一个grep/awk q,解析diskpart输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在谷歌上搜索了很多,有很多类似的问题,但我不知道如何将它们放在一起以使其对我有用.此外,MS 决定将动态卷排除在其 PowerShell cmdlet 之外的事实确实令人沮丧.

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 leave dynamic volumes out of their PowerShell cmdlets is really frustrating.

在下面的代码中,我试图确定磁盘 2"是动态的.

In the following code I'm trying to identify that "Disk 2" is dynamic.

PS C:\Windows\system32> echo 'list disk' | diskpart

Microsoft DiskPart version 10.0.14393.0

Copyright (C) 1999-2013 Microsoft Corporation.
On computer: AHPAP2704

DISKPART> 
  Disk ###  Status         Size     Free     Dyn  Gpt
  --------  -------------  -------  -------  ---  ---
  Disk 0    Online           65 GB      0 B         
  Disk 1    Online           20 GB      0 B        *
  Disk 2    Offline          50 GB      0 B   *     

理想情况下,从上面的输出中,我将设置一个变量来标识动态卷(我的脚本将始终只有一个),因此完成后我会得到类似 $DynDisk = 2.

Ideally from the output above I'm going to set a variable to identify the dynamic volume (my script will always only have one) so when complete I'm left with something like $DynDisk = 2.

当我将输出通过管道传送到 Get-Member 时,名称中包含属性的唯一成员类型是 CharsLength.

When I pipe the output to Get-Member the only member types containing property in the name are Chars and Length.

有没有一种简单的方法可以将数据放入数组中,或者有更好的方法吗?或者,是否有可能存在一些隐藏的 grepawk 之类的 cmdlet?

Is there an easy way to get the data into an array or a better method? Or, any chance there is some hidden grep and awk like cmdlets out there?

推荐答案

diskpart 输出未修剪,因此您可以从字符串的末尾解析相关信息,例如像这样:

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

这篇关于另一个grep/awk q,解析diskpart输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆