简体   繁体   English

将Get-WmiObject win32_diskdrive转换为Powershell 4中的阵列

[英]Convert Get-WmiObject win32_diskdrive to array in Powershell 4

How can I convert a Get-WmiObject command to an array and add it to a combobox? 如何将Get-WmiObject命令转换为数组并将其添加到组合框?

This is the command line: 这是命令行:

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

And this is the output result: 这是输出结果:

\\.\\PHYSICALDRIVE0 \\。\\ PHYSICALDRIVE0
\\.\\PHYSICALDRIVE1 \\。\\ PHYSICALDRIVE1

I would like to write this like 我想这样写

"\\.\\PHYSICALDRIVE0","\\.\\PHYSICALDRIVE1" “ \\。\\ PHYSICALDRIVE0”,“ \\。\\ PHYSICALDRIVE1”

Thanks for the help! 谢谢您的帮助!

The output from Get-WmiObject ... |Select-Object ... is already an array - Format-Table -HideTableHeaders simply outputs each object line-by-line. 从输出Get-WmiObject ... |Select-Object ...已经一个数组- Format-Table -HideTableHeaders简单地输出每个对象的行由行。

If you want the string you describe in your question, avoid the Format-* and the Out-String cmdlets altogether: 如果要在问题中描述的字符串,请完全避免使用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

For double-quotes ( " ) around each element, I would go for the -f format operator: 对于每个元素周围的双引号( " ),我将使用-f格式运算符:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM