PowerShell-向数组添加列
假设您正在做这样的事情:
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用哈希表(关联数组):
或者创建对象:
You can either use a hashtable (associative array):
Or create objects: