【发布时间】:2018-02-21 09:07:59
【问题描述】:
我在 Powershell 中将 UNC 位置转换为本地驱动器时遇到问题。因此,我的代码基本上将用户输入作为读取主机作为数据库备份位置的 UNC 路径。位置可能是这样的\\ServerName\m$\SQLBackups\123。
然后它会提示从哪里复制备份。这又是一个 UNC 路径,例如 \\ServerName\g$\SQLRestores\444\Filename.bak。
我需要将两个路径都转换为它们的本地路径,以便读取 m:\sqldbackups\123 和 g:\sqlrestores\444\Filename.bak。
下面是我的代码,我已经添加了我想要的最终输出的注释。
$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
【问题讨论】: