拆分路径与根目录UNC
所以,我已经有了一个UNC路径,像这样:拆分路径与根目录UNC
“\\服务器\文件夹”
我希望得到公正的路径,服务器,如\\服务器'。 拆分路径“\\ server \ folder”-Parent返回“”。任何我试图处理根的东西都会失败。 例如,Get-Item“\\ server”也会失败。
如何在PowerShell中从“\\ server \ folder”安全地获取“\\ server”的路径?
通过使用System.Uri
类和查询其host
属性:
$uri = new-object System.Uri("\\server\folder")
$uri.host # add "\\" in front to get exactly what you asked
注:对于UNC路径,根目录是服务器名加上共享名称的一部分。
我喜欢在New-Object上使用URI类型来转换事物,但使用的是'$ URI = [URI]“\\ Server \ Folder”''$ URI.Host' –
$fullpath = "\\server\folder"
$parentpath = "\\" + [string]::join("\",$fullpath.Split("\")[2])
$parentpath
\\server
$ parentPath =“\\”+ [string]: :加入( “\”,$ fullPath.Split( “\”)[2..3]) –
使用正则表达式的一个例子:
'\\server\share' -replace '(?<!\\)\\\w+'
这是服务器故障后有什么用处? http://*.com/questions/297690/how-to-split-parent-path-of-a-unc-path-by-using-powershell –
不是真的,拆分路径等工作正常的路径窗体“\\ server \ folder1 \ folder2” – mamidon
对于UNC路径,根目录是servername +共享名称。 –