Convert UNC Path to NTFS local path

余生长醉 提交于 2019-12-04 21:09:35

Split-Path won't work unless the path is to a directory or file at the root of the share.

Use Path.GetPathRoot() instead to get the host and share name:

$Drive = [System.IO.Path]::GetPathRoot($BackupPath)
$Dumps = $BackupPath.Substring($Drive.Length)
$Drive = $Drive.Substring($Drive.LastIndexOf('\') + 1).Replace('$',':')
$NTFSPath = "$Drive$Dumps"

I think the answer above is the correct way to do it, but still, sometimes simple pattern matching might be enough:

"\\server2.net\g$\foo\bar\baz", "\\server1.com\c$\baz\bar\foo" |
Select-String -Pattern "([A-z])\`$(.*)" |
%{ "$($_.Matches.Groups[1].value):$($_.Matches.Groups[2].value)" }
# -replace operator with regex does the trick
PS C:\> '\\ServerName\m$\SQLBackups\123' -replace '(?:.+)\\([a-z])\$\\','$1:\'
m:\SQLBackups\123
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!