powershell - PowerShell匹配,包含不适用于网络共享路径

标签 powershell

我正在尝试识别与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-ListGet-MemberSelect-Object检查您的对象以查看更多信息。

关于powershell - PowerShell匹配,包含不适用于网络共享路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54593978/

相关文章:

c# - 性能:System.Diagnostics.Process 与 System.Management.Automation.PowerShell

PowerShell 内联 If (IIf)

xml - 如何将元素插入子节点

powershell - Powershell中有没有办法在其他文件系统中提供虚拟文件系统?

Powershell:在第一个函数参数已经定义时隐藏第二个函数参数

powershell - Powershell将字节转换为GB

.net - 防止 ArrayList.Add() 返回索引

regex - Powershell:拆分路径在 -Replace 中不起作用

xml - 如何使用 ConvertTo-Xml 和 Select-Xml 加载或读取 XML 文件?

powershell - 如何替换连续的特殊字符?