Posted On June 13, 2020

PowerShell: Convert Local Path to UNC Path

kimconnect 2 comments
blog.KimConnect.com >> Codes >> PowerShell: Convert Local Path to UNC Path
function convertLocalToUnc($localPath,$computername){    
    $uncPath=.{$x=$localPath -replace "^([a-zA-Z])\:","\\$computername\`$1`$";
                if($x -match '\\$'){return $x.Substring(0,$x.length-1)}else{return $x}
                }
    $validLocal=if($localPath){test-path $localPath -ErrorAction SilentlyContinue}else{$false}
    $validUnc=if($uncPath){test-path $uncPath -ErrorAction SilentlyContinue}else{$false}
    if($validUnc){write-host "$uncPath is reachable, using credentials of $(whoami)"}
    else{write-warning "$computername is unreachable, using credentials of $(whoami)"}
    return $uncPath
}
# Usage Example
PS C:\Windows\system32> convertLocalToUnc $defaultBackupDirectory $sqlServer
\\sqlSherverXOXO\C$\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\Backup is reachable, using credentials of intranet\bowow

2 thoughts on “PowerShell: Convert Local Path to UNC Path”

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

The Process of Adding a New Hyper-V Server Into a Cluster and the Associated Virtual Machine Manager (VMM)

These are the steps: Install WindowsWindows 2019 Data Center Edition is the standard as of…

PowerShell: Set Virtual Machine Default Paths on Hyper-V Host of a Cluster

$newVirtualMachinePath='D:\VirtualMachines' $newVirtualHardDiskPath='D:\VirtualMachines' function getHyperVHostsInForest{ function includeRSAT{ $ErrorActionPreference='stop' [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 #$rsatWindows7x32='https://download.microsoft.com/download/4/F/7/4F71806A-1C56-4EF2-9B4F-9870C4CFD2EE/Windows6.1-KB958830-x86-RefreshPkg.msu' $rsatWindows7x64='https://download.microsoft.com/download/4/F/7/4F71806A-1C56-4EF2-9B4F-9870C4CFD2EE/Windows6.1-KB958830-x64-RefreshPkg.msu' $rsatWindows81='https://download.microsoft.com/download/1/8/E/18EA4843-C596-4542-9236-DE46F780806E/Windows8.1-KB2693643-x64.msu' $rsat1709 =…

PowerShell: Convert Array to Hash and back to Array

# convert 2D Array to Hash Table while removing any duplicate keys function arrayToHash($arr){ $hash…