在Powershell 4中将Get-WmiObject win32_diskdrive转换为数组

时间:2016-07-27 20:24:38

标签: arrays powershell get-wmiobject

如何将Get-WmiObject命令转换为数组并将其添加到组合框?

这是命令行:

Get-WmiObject -Query "select DeviceID from win32_diskdrive" | Select-Object -ExpandProperty  DeviceID | ft -HideTableHeaders -AutoSize | Out-String

这是输出结果:

  

\。\ PHYSICALDRIVE0
\。\ PHYSICALDRIVE1

我想写这个像

  

“\。\ PHYSICALDRIVE0”, “\。\ PHYSICALDRIVE1”

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

Get-WmiObject ... |Select-Object ... 的输出已经是一个数组 - Format-Table -HideTableHeaders只是逐行输出每个对象。

如果您需要在问题中描述的字符串,请完全避免使用Format-*Out-String cmdlet:

PS C:\> $DiskDrives = Get-WmiObject -Query "SELECT DeviceID FROM Win32_DiskDrive" |Select-Object -ExpandProperty DeviceID
PS C:\> $DiskDrives -join ','
\\.\PHYSICALDRIVE0,\\.\PHYSICALDRIVE1

对于每个元素周围的双引号("),我会选择-f格式运算符:

PS C:\> $DiskDrives.ForEach({'"{0}"' -f $_}) -join ','
"\\.\PHYSICALDRIVE0","\\.\PHYSICALDRIVE1"