gpt4 book ai didi

windows - 选择对象更改所选值

转载 作者:行者123 更新时间:2023-12-03 01:30:50 24 4
gpt4 key购买 nike

问题简述:

我正在尝试在PowerShell中获取网络驱动器的路径。使用Get-PSDrive,我可以获得所需的所有信息。

PS X:\> Get-PSDrive -Name X
Name           Used (GB)     Free (GB) Provider      Root                                               CurrentLocation
---- --------- --------- -------- ---- ---------------
X 1527,59 214,61 FileSystem \\Path\to\network\drive


现在我想将Root保存在变量中,但是当我这样做时

PS X:\> Get-PSDrive -Name X | Select-Object Root

我懂了
X:\

为什么选择对象会更改值?如何获得“原始”值(value)?

最佳答案

缺省的Get-PSDrive表显示将DisplayRoot属性值显示为Root,除非它为空。您可以通过将Get-PSDrive用管道传输到Format-List *来查看此行为。要控制输出,可以将计算的属性与Select-Object一起使用。

Get-PSDrive -Name X | Select-Object @{
Name='Root'
Expression = {
if ([string]::IsNullOrEmpty($_.DisplayRoot)) {
$_.Root
}
else {
$_.DisplayRoot
}
}
}

说明:

如果检查 $pshome\PowerShellCore.format.ps1xml文件,它将使用以下表达式定义根值,这就是您看到此行为的原因。
if($_.DisplayRoot -ne $null) { $_.DisplayRoot } else { $_.Root }

关于windows - 选择对象更改所选值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58340602/

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