2017-08-30 248 views
0

我試圖測試一個UNC路徑是否存在,但所有嘗試都失敗了。 此文件夾例子存在,並返回true:帶通配符的PowerShell測試路徑UNC

\\Server\Path1 

我想確認所有文件夾有存在類似的名稱,如:

\\Server\Path2 
\\Server\Path3 etc. 

我我在這些例子中試過使用通配符:

test-path "\\Server\Path*" 
resolve-path "\\Server\Path*" 
[System.IO.Directory]::Exists('\\Server\Path*'); 
Test-Path $('filesystem::\\Server\Path*') 

...除了\''*組合的許多排列組合。 但是,使用通配符時,我嘗試過的任何內容都不會返回此類路徑的「真」,即使它似乎可以正常工作:例如,Test-Path c:\windows\system3*

+0

你的道路已經是'\\服務器\ C $ \路徑*' –

+1

[相關](https://stackoverflow.com/q/35068220/1630171)。 –

回答

1

我不認爲Windows支持在共享名通配符選擇。

但是,如果你有足夠的(遠程)訪問文件服務器,你可以得到的共享列表:

Get-WmiObject -class 'Win32_Share' -ComputerName 'Server' 
+0

這回答了這個問題,非常感謝。 爲了完整起見,這是我用來辨別'Pathx'存在的線: 'Get-WmiObject -class'Win32_Share' - 計算機名'Server'|名稱類似「*路徑*」' – ScriptMonkey

1

這個片段將用於映射UNC路徑工作:

Get-PSDrive| where{$_.DisplayRoot -like "\\server\test*" } | foreach{test-path -path $_.DisplayRoot} 

如果您有WMI訪問,則:

Get-WmiObject -Class Win32_Share -ComputerName server | where{$_.name -like "test*"} | foreach{Test-Path "\\server\$($_.name)"}