Powershell Where-Object Match

Sve*_*ten 1 powershell

我想使用Where-Object限制输出Get-PSDrive仅限于网络共享.

Get-PSDrive 告诉我以下内容:

Name           Used (GB)     Free (GB) Provider      Root                                       CurrentLocation
----           ---------     --------- --------      ----                                       ---------------
A                                      FileSystem    A:\                                                       
Alias                                  Alias                                                                   
C                  16.19         43.47 FileSystem    C:\                                  Users\HansB\Documents
Cert                                   Certificate   \                                                         
D                                      FileSystem    D:\                                                       
Env                                    Environment                                                             
Function                               Function                                                                                                 
HKCU                                   Registry      HKEY_CURRENT_USER                                         
HKLM                                   Registry      HKEY_LOCAL_MACHINE                                        
V                 451.39        159.76 FileSystem    \\192.168.71.31\fs_log_target                             
Variable                               Variable                                                                
W                 197.72               FileSystem    \\192.168.71.32\perf200                                   
WSMan                                  WSMan                                                                   
X                 197.72               FileSystem    \\192.168.71.32\perf100                                   
Y                 271.52         34.33 FileSystem    \\192.168.71.30\group200                                  
Z                 271.52         34.33 FileSystem    \\192.168.71.30\group100                                  

然后我想获得\\192.168.71.30\group100网络共享:

Get-PSDrive | Where-Object  { $_.Root -match "\\\\192.168.71.30\\group100" }
Run Code Online (Sandbox Code Playgroud)

但我什么都没得到,为什么 - 匹配不起作用?

Ada*_*ich 6

使用DisplayRoot而不是Root属性

Get-PSDrive | Where-Object  { $_.DisplayRoot -match "\\\\192.168.71.30\\group100" }
Run Code Online (Sandbox Code Playgroud)

试着跑

Get-PSDrive | Where-Object  { $_.DisplayRoot -match "\\\\192.168.71.30\\group100" } | select *
Run Code Online (Sandbox Code Playgroud)

Root将是您映射的驱动器号,DisplayRoot是您的UNC路径

编辑:作为旁注.对于转义正则表达式,请使用[regex] :: Escape()方法.

PS > [regex]::Escape("\\192.168.71.30\group100")
\\\\192\.168\.71\.30\\group100
Run Code Online (Sandbox Code Playgroud)