PowerShell-向数组添加列

发布于 2024-12-10 14:20:50 字数 955 浏览 0 评论 0原文

假设您正在做这样的事情:

$partitions = get-wmiobject -query "Associators of {Win32_DiskDrive.DeviceID=""$DevID""} WHERE AssocClass = Win32_DiskDriveToDiskPartition" -computer $ComputerName

如果您想获取每个磁盘(“C:”)的磁盘标题,您需要这样的东西:

function GetDiskCaption {
   param ($Partition, $ComputerName)

    $DeviceID = $Partition.DeviceID
    $colLogicalDisk = get-wmiobject -query "Associators of {Win32_DiskPartition.DeviceID=""$DeviceID""} WHERE AssocClass = Win32_LogicalDiskToPartition" -computer $ComputerName

   If ($colLogicalDisk.Caption -ne $null) {
      return $colLogicalDisk.Caption
   }
   Else {
       return "UNASSIGNED"
   }
}

我不是一个非常高级的 PowerShell 用户,所以问题是:将磁盘标题添加到阵列以便可以将其通过管道传输到 ft 或其他脚本中的最佳 PowerShell 方法是什么?

现在我意识到我可以只做一个 foreach 循环来打印出来,但我想弄清楚如何用额外的列扩展数组,以便我可以使用管道运算符来格式化数组。

微软网站上关于数组处理的示例都是基于一维数组的,因此添加数组似乎不是答案。

我专门尝试编写一个脚本来转储磁盘列表,及其 iSCSI 映射(如果有)以及该磁盘上的各个分区。

Let’s say you're doing something like this:

$partitions = get-wmiobject -query "Associators of {Win32_DiskDrive.DeviceID=""$DevID""} WHERE AssocClass = Win32_DiskDriveToDiskPartition" -computer $ComputerName

If you then want to get the disk caption for each disk (the 'C:') you need something like this:

function GetDiskCaption {
   param ($Partition, $ComputerName)

    $DeviceID = $Partition.DeviceID
    $colLogicalDisk = get-wmiobject -query "Associators of {Win32_DiskPartition.DeviceID=""$DeviceID""} WHERE AssocClass = Win32_LogicalDiskToPartition" -computer $ComputerName

   If ($colLogicalDisk.Caption -ne $null) {
      return $colLogicalDisk.Caption
   }
   Else {
       return "UNASSIGNED"
   }
}

I'm not a very advanced PowerShell user, so the question is: What's the best PowerShell way to add the disk caption to the array so that can pipe it into, say, ft or some other script?

Now I realise I can just do a foreach loop to print this out, but I want to work out how to extend an array with extra columns so that I can then use piping operators to, say, format the array.

The examples on the Microsoft site on array handling are all based on one-dimensional arrays, so adding arrays does not seem to be the answer.

I'm specifically trying to write a script to dump out the list of disks, with their iSCSI mapping (if any) and the various partitions on that disk.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜灵血窟げ 2024-12-17 14:20:50

您可以使用哈希表(关联数组):

$hash = @{}
$partitions | %{ $caption = GetDiskCaption $_ $computername; $hash[$caption]=$_ }
$hash

或者创建对象:

$combined = $partitions | %{ $caption = GetDiskCaption $_ $computername
                $obj = new-object psobject
                $obj | add-member -name caption -type noteproperty -value $caption
                $obj | add-member -name partition -type noteproperty -value $_
                $obj
            }

You can either use a hashtable (associative array):

$hash = @{}
$partitions | %{ $caption = GetDiskCaption $_ $computername; $hash[$caption]=$_ }
$hash

Or create objects:

$combined = $partitions | %{ $caption = GetDiskCaption $_ $computername
                $obj = new-object psobject
                $obj | add-member -name caption -type noteproperty -value $caption
                $obj | add-member -name partition -type noteproperty -value $_
                $obj
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文