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

PowerShell: Running Commands on Remote Computers

# runCommandsOnRemoteComputers.ps1 # User defined variables $computernames=@( 'SERVER001', 'SERVER002' ) $expectedExecutable='racadm.exe' $expectedInstallPath='C:\program files\Dell\SysMgt\iDRACTools\racadm' # Execution…

PowerShell: Automating Microsoft Failover Cluster Maintenance – FileServer, SQL AlwaysOn, Hyper-V Guest VMs, Disks Operations

##################################################################################################### # MsClusterMaintenance_v0.0.2.ps1 # Author: KimConnect.com # License: GPLv3 # Description: this program automates the…

Create Desktop Application with Electron using JavaScript

Electron packager > get release build folder cd testappnpm initdescription: calculator.jsauthor: kimconnect.com npm install --save…