function Test-PathIsInApprovedPackageLocation { <# .SYNOPSIS Test to ensure a path is in an approved package location. Could be in a chocolatey location or elsewhere. .PARAMETER SourcePath [string] The path to the package install location .INPUTS Requires the SourcePath .EXAMPLE Test-PathIsInApprovedPackageLocation -SourcePath C:\ProgramData\chocolatey\lib\Alkami.Apps.Authentication This will return true .EXAMPLE Test-PathIsInApprovedPackageLocation -SourcePath C:\OrbLogs\Alkami.Client.WebClient.log This obviously wrong test-case will return false #> [CmdletBinding()] [OutputType([System.Boolean])] Param( [Parameter(Mandatory=$true, Position=0)] [Alias('Path')] [string]$SourcePath ) $logLead = (Get-LogLeadName) $testPaths = @() ## Ensure the path actually exists if (!(Test-Path $SourcePath)) { Write-Error "$logLead : Can not find path at [$SourcePath]" return $false } ## Ensure the path is actually a folder if (!(Test-Path $SourcePath -PathType Container)) { Write-Error "$logLead : Path at [$SourcePath] is not a folder" return $false } ## Add the chocolatey path for testing against ## When we introduce a new package location, ex: we leave Chocolatey, we add those checks here too $testPaths += (Get-ChocolateyInstallPath) $containsApprovedPath = $false foreach($testPath in $testPaths) { ## Look for a test-path that matches the SourcePath exactly ## This is an indeterminate case and is almost certainly wrong $indeterminateTestPath = (Join-Path (Split-Path $TestPath) (Split-Path $TestPath -Leaf)) $indeterminateSourcePath = (Join-Path (Split-Path $SourcePath) (Split-Path $SourcePath -Leaf)) if ($indeterminateTestPath.ToLower() -eq $indeterminateSourcePath.ToLower()) { $containsApprovedPath = $false } elseif ($SourcePath.ToLower().Contains($testPath.ToLower())) { $containsApprovedPath = $true } else { $containsApprovedPath = $false } } return $containsApprovedPath }