I'm trying to create a test function in PowerShell that checks a folder to see if two items exist, and if they don't exist the function returns a false value. However, even if I change the function to blatantly use 'return $false' it still continues with the if statement.
The if statement that calls the function is as follows:
if (Test-Path $datapackPath) {
if (Test-ValidDatapack($datapackPath)) {
$files = foreach ($file in Get-ChildItem -Path $datapackPath -Force -Recurse) {
$file.FullName
}
return $files
}
And the function that is supposed to return false is as follows:
function Test-ValidDatapack {
#Parameter for the filepath
[Parameter(Mandatory=$true)]
[String]$datapackPath
$valid = $true #Return variable
$files = Get-ChildItem $datapackPath #Collects filenames included in datapack
if ($files.Name -notcontains "pack.mcmeta") { #Checks datapack for 'pack.mcmeta'
Write-Host "No 'pack.mcmeta' file found. Invalid datapack." -ForegroundColor Red
$valid = $false
}
if ($files.Name -notcontains "data") { #Checks datapack for 'data'
Write-Host "No 'data' directory found. Invalid datapack." -ForegroundColor Red
$valid = $false
}
return $valid
}
It seems like the function call just isn't getting the return value and I'm not sure why.