2016-04-04 155 views
1

我想列出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的管道输入,怎么样 我们能解决这个问题?

回答

3

Select正在返回一个名为DeviceID的属性。

使用-ExpandProperty来获得属性值,然后通过管道是:

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

你也可以只选择被返回的对象的属性。在这种情况下,$_.DeviceID

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