powershell - 管道输入/输出

标签 powershell get-wmiobject

我想列出C:驱动器的文件。首先,我想从逻辑磁盘wmi对象获取设备ID,并列出它。

下面的命令返回:

Get-WmiObject -class Win32_logicaldisk


DeviceID     : C:  
DriveType    : 3  
ProviderName :  
FreeSpace    : 940371968  
Size         : 125809192960  
VolumeName   :  

但是此命令:
Get-WmiObject -class Win32_logicaldisk | select deviceid | Get-ChildItem -path {$_}

给出以下错误:

Get-ChildItem : Cannot find drive. A drive with the name '@{deviceid=C' does not exist. At line:1 char:60
+ Get-WmiObject -class Win32_logicaldisk | select deviceid | Get-ChildItem -path { ...
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (@{deviceid=C:String) [Get-ChildItem], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand



Get-ChildItem -path接受管道输入,我们如何解决呢?

最佳答案

您的Select返回的对象带有名为DeviceID的属性。

使用-ExpandProperty获取属性值,然后通过管道传递该值:

Get-WmiObject -class Win32_logicaldisk | select -expandproperty deviceid | Get-ChildItem -path {$_}

关于powershell - 管道输入/输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36413827/

相关文章:

excel - 查找然后保存并关闭所有打开的 Excel 文档

windows - -在 cmdlet Get-WmiObject 中列出属性

powershell - 为什么通过 Invoke-Command 远程运行 Get-WmiObject 失败?

powershell - 从远程计算机获取特殊文件夹

用于传递日志信息的 Azure powershell 脚本

powershell - '来自 HRESULT : 0X800A01B6' error in PowerShell when trying to enter login information to Facebook 的异常

powershell - 将输入列表分成几列

c# - 电源外壳 : Import-Module