我正在尝试识别与Root类似的映射网络共享的驱动器名称
\\SERVER05\DIR$
这根本行不通:
Get-PSDrive | Where-Object {$_.Root -like "SERVER05" }
尝试使用
-match
,-contains
和什么都没有有任何想法吗?
最佳答案
这里的问题是这些PowerShell陷阱之一。 PSDrives返回的默认属性是“名称”,“描述”,“提供程序”,“根”和“当前位置”。让我们看一下我的M盘
Get-PSDrive -Name M
Name Used (GB) Free (GB) Provider Root CurrentLocation
---- --------- --------- -------- ---- ---------------
M 769.66 112.47 FileSystem \\s5000\Computer Srv\Matt
您遇到的问题是,当您将
Root
的名称发送到默认输出流时,您实际上正在查看DisplayRoot
的值。这具有误导性,但您想要的信息仍然存在。Get-PSDrive -Name M | Format-List *
Used : 826416230400
Free : 120764563456
CurrentLocation :
Name : M
Provider : Microsoft.PowerShell.Core\FileSystem
Root : M:\
Description :
Credential : System.Management.Automation.PSCredential
DisplayRoot : \\s5000\Computer Srv\Matt
这是使用PowerShell格式化xml文件在幕后处理的。具体来说,这来自PowerShellCore.format.ps1xml中的一行,该行将
Root
的值(如果它具有值)定义为DisplayRoot
...否则为Root
<TableColumnItem>
<ScriptBlock>if($_.DisplayRoot -ne $null) { $_.DisplayRoot } else { $_.Root }</ScriptBlock>
</TableColumnItem>
到目前为止,如果您跟上我的话..
它试图通过显示您想要的内容来帮您一个忙。知道这一点...尝试对
DisplayRoot
进行过滤。Get-PSDrive | Where-Object {$_.DisplayRoot -like "*SERVER05*" }
当您尝试进行部分匹配时,请不要忘记在使用
-like
时使用星号。如有疑问,请使用cmdlet列表Format-List
,Get-Member
和Select-Object
检查您的对象以查看更多信息。
关于powershell - PowerShell匹配,包含不适用于网络共享路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54593978/