ps/Modules/Alkami.PowerShell.Common/Public/Test-PathMatch.ps1
2023-05-30 22:51:22 -07:00

60 lines
1.9 KiB
PowerShell

function Test-PathMatch {
<#
.SYNOPSIS
Used to test if two file paths are equal. This is useful to determine if two paths are the same even if the
Symlink name is different.
.PARAMETER FirstPath
First path to test
.PARAMETER SecondPath
Second path to test
.NOTES
Files that are the target of Symlinks can have multiple Symlinks targeting a given file or folder.
This function will handle many targets of a linked file/folder.
.EXAMPLE
New-Symlink Source 'C:\ProgramData\chocolatey\lib\Alkami.Apps.Authentication\content\Areas\App' -Target 'C:\orb\WebClient\Areas\' -Name 'Test'
Test-PathMatch -Source 'C:\ProgramData\chocolatey\lib\Alkami.Apps.Authentication\content\Areas\App' -Target 'C:\orb\WebClient\Areas\Test'
.OUTPUTS
[bool] $True if the two paths match, $False if not
#>
[CmdletBinding()]
[OutputType([System.Boolean])]
param(
[Parameter(Mandatory = $true)]
[Alias("Source")]
[string]$FirstPath,
[Parameter(Mandatory = $true)]
[Alias("Target")]
[string]$SecondPath
)
if ([string]::IsNullOrWhiteSpace($FirstPath) -or [string]::IsNullOrWhiteSpace($SecondPath)) {
# Can't really test if an input is null
return $false
}
$path1 = Get-Item $FirstPath
$path2 = Get-Item $SecondPath
$path1FullPath = [array]$path1.FullName.Trim('\')
$path2FullPath = [array]$path2.FullName.Trim('\')
$isFirstPathSymlink = ($path1.LinkType -eq "SymbolicLink")
$isSecondPathSymlink = ($path2.LinkType -eq "SymbolicLink")
if($isFirstPathSymlink) {
$path1FullPath = $path1.Target.Trim('\')
}
if($isSecondPathSymlink) {
$path2FullPath = $path2.Target.Trim('\')
}
# If any of the items in the array match, return $true
$isMatch = ($path1FullPath | Where-Object{$_ -ieq $path2FullPath}).Count -gt 0
return $isMatch
}