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
.
It then prompts where the backup is being copied from. Again this is a UNC path such as \\ServerName\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