83 lines
3.3 KiB
PowerShell
83 lines
3.3 KiB
PowerShell
function Get-NormalizedPath {
|
|
[CmdletBinding()]
|
|
[OutputType([string])]
|
|
param(
|
|
[Parameter(Mandatory = $true)]
|
|
[string]$FilePath,
|
|
[Parameter(Mandatory = $false)]
|
|
[string]$ComputerName = (hostname)
|
|
)
|
|
|
|
$logLead = (Get-LogLeadName)
|
|
|
|
if (!(Test-Path -IsValid -Path $FilePath)) {
|
|
Write-Warning "$logLead : Presented file path [$FilePath] is not valid, and can not be parsed. Returning input string."
|
|
return $FilePath
|
|
}
|
|
|
|
$computerNameIsLocalComputer = (Compare-StringToLocalMachineIdentifiers -StringToCheck $ComputerName)
|
|
$networkComputerNameIsLocalComputer = $false
|
|
|
|
$remoteDriveName = $null
|
|
$networkComputerName = $null
|
|
|
|
$executingLocation = (Get-Location)
|
|
$executingQualifier = (Split-Path ($executingLocation.Drive.Root) -Qualifier)
|
|
|
|
$pathQualifier = (Split-Path -Path $FilePath -Qualifier -ErrorAction SilentlyContinue)
|
|
$pathQualifierPresent = ([string]::IsNullOrWhiteSpace($pathQualifier))
|
|
|
|
$isNetworkPath = $FilePath.StartsWith("\\")
|
|
|
|
$pathRemainder = ""
|
|
|
|
if ($isNetworkPath) {
|
|
$filePathSplits = $FilePath.Split("\",[StringSplitOptions]::RemoveEmptyEntries)
|
|
$networkComputerName = $filePathSplits[0]
|
|
$networkComputerNameIsLocalComputer = (Compare-StringToLocalMachineIdentifiers -StringToCheck $networkComputerName)
|
|
if ($networkComputerNameIsLocalComputer -and $computerNameIsLocalComputer) {
|
|
Write-Verbose "$logLead : Provided path is the local computer, resolving is just gonna be fast."
|
|
return $FilePath
|
|
}
|
|
|
|
# Let's figure out what the path would've been on that computer, and apply it to this computer as a local path
|
|
$potentialDriveName = $filePathSplits[1]
|
|
if ([string]::IsNullOrWhiteSpace($potentialDriveName)) {
|
|
throw "$logLead : Can't parse the network computer name alone as a path. Please provide a more complete path."
|
|
}
|
|
$remoteDriveNameIsProbablyDrive = (![string]::IsNullOrWhiteSpace($potentialDriveName) -and $potentialDriveName.Length -eq 2 -and $potentialDriveName.EndsWith('$'))
|
|
if ($remoteDriveNameIsProbablyDrive) {
|
|
$pathQualifier = $potentialDriveName.Replace('$',':')
|
|
} else {
|
|
throw "$logLead : Can't parse the second segment of the network path as a drive letter (1 character and a `$). Please provide a path that matches this pattern."
|
|
}
|
|
if ($filePathSplits.Count -gt 2) {
|
|
$pathRemainder = $filePathSplits[2]
|
|
for($i = 3; $i -le $filePathSplits.Count; $i++) {
|
|
$pathRemainder = (Join-Path $pathRemainder $filePathSplits[$i])
|
|
}
|
|
}
|
|
} else {
|
|
$pathRemainder = (Split-Path -Path $FilePath -NoQualifier)
|
|
}
|
|
|
|
$pathRemainder = $pathRemainder.TrimStart('\')
|
|
|
|
if ([string]::IsNullOrWhiteSpace($pathQualifier)) {
|
|
$pathQualifier = $executingQualifier
|
|
}
|
|
|
|
$finalPath = ""
|
|
# Return the local path if UNC pathing isn't required.
|
|
if($computerNameIsLocalComputer) {
|
|
$finalPath = (Join-Path $pathQualifier $pathRemainder)
|
|
} else {
|
|
$remoteQualifier = $pathQualifier.Replace(':','$')
|
|
$finalPath = "\\$ComputerName\$remoteQualifier\$pathRemainder"
|
|
}
|
|
|
|
$finalPath = $finalPath.TrimEnd('\')
|
|
|
|
Write-Verbose "$logLead : Determined final path to be [$finalPath]"
|
|
return $finalPath
|
|
} |