将 UNC 路径转换为 NTFS 本地路径 [英] Convert UNC Path to NTFS local path
问题描述
我在 powershell 中将 UNC 位置转换为本地驱动器时遇到问题.所以我的代码基本上将用户的输入作为读取主机作为数据库备份位置的 UNC 路径.位置可能是这样的 \\ServerName\m$\SQLBackups\123
.
I have a problem converting UNC location to local drive in powershell. So my code basically takes input from user as read Host as UNC path for a DB Backup location. The location could be like this \\ServerName\m$\SQLBackups\123
.
然后它会提示从哪里复制备份.同样,这是一个 UNC 路径,例如 \\ServerName\g$\SQLRestores\444\Filename.bak
.
It then prompts where the backup is being copied from. Again this is a UNC path such as \\ServerName\g$\SQLRestores\444\Filename.bak
.
我需要将两个路径都转换为它们的本地路径,以便它读取 m:\sqldbackups\123
和 g:\sqlrestores\444\Filename.bak
.
I need to convert both paths to their local path so that it reads m:\sqldbackups\123
and g:\sqlrestores\444\Filename.bak
.
下面是我的代码,我添加了我想要的最终输出的注释.
Here is my code below and I have added a comment of what I want the final output to be.
$BackupPath = "\\ServerName\m$\SQLBackups\123"
$Dumps = Split-Path -Path $BackupPath -Leaf
$Drive = Split-Path -Path $BackupPath -Parent
$LastTwo = $Drive.Substring($Drive.get_Length()-2)
$FullNTFS = $LastTwo + "\" + $Dumps
$FullNTFS = $FullNTFS.Replace("$",":")
$FullNTFS
#This should be M:\SQLBACKUPS\123
$RestorePath = "\\ServerName\g$\SQLRestores\444\Filename.bak"
$Dumps = Split-Path -Path $RestorePath -Leaf
$Drive = Split-Path -Path $RestorePath -Parent
$LastTwo = $Drive.Substring($Drive.get_Length()-2)
$FullNTFS = $LastTwo + "\" + $Dumps
$FullNTFS = $FullNTFS.Replace("$",":")
$FullNTFS
#This should be read as g:\sqlrestores\444\FileName.bak
推荐答案
Split-Path
将不起作用,除非路径是共享根目录下的目录或文件.
Split-Path
won't work unless the path is to a directory or file at the root of the share.
使用 Path.GetPathRoot()
改为获取主机和共享名称:
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"
这篇关于将 UNC 路径转换为 NTFS 本地路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!