0

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.

  • 2
    You shouldn't call PowerShell functions with parentheses. e.g. `Test-ValidDatapack($datapackPath)` should be `Test-ValidDatapack $datapackPath`, or more clearly: `Test-ValidDatapack -DatapackPath $datapackPath`. Give it a go and see if it helps. – boxdog Jul 25 '22 at 07:45
  • [Powershell function acting weird and only returns the param](https://stackoverflow.com/q/49050507/995714), [Parenthesis Powershell functions](https://stackoverflow.com/q/11060833/995714) – phuclv Jul 25 '22 at 08:24

0 Answers0