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: Audit Domain Controller Certificates

function auditDcCerts{ try{ write-host "Gathering Domain Controller Names..." Import-Module ActiveDirectory $osInfo = Get-CimInstance -ClassName Win32_OperatingSystem…

VSS Error: Snapshots were found, but they were outside of your allowed context

Error: PS C:\Windows\system32> vssadmin delete shadows /for=c: /allvssadmin 1.1 - Volume Shadow Copy Service administrative…

PowerShell: Windows Defender Controlled Folder Access

# Default Setting of Windows Defender:PS C:\Windows\system32> get-mppreferenceAttackSurfaceReductionOnlyExclusions :AttackSurfaceReductionRules_Actions :AttackSurfaceReductionRules_Ids :CheckForSignaturesBeforeRunningScan : FalseCloudBlockLevel : 0CloudExtendedTimeout…