gpt4 book ai didi

powershell - 管道输入/输出

转载 作者:行者123 更新时间:2023-12-03 00:36:37 24 4
gpt4 key购买 nike

我想列出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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com