PowerShell functions that return true/false

function, powershell, return-value

Solution

Don't use True or False, instead use $true or $false

function SuccessConnectToDB {
 param([string]$constr)
 $successConnect = .\psql -c 'Select version();' $constr
    if ($successConnect) {
        return $true;
    }
    return $false;
}

Then call it in a nice clean way:

if (!(SuccessConnectToDB($connstr))) {
    exit  # "Failure Connecting"
}

Problem

I am pretty new with using PowerShell and was wondering if anyone would have any input on trying to get PowerShell functions to return values. I want to create some function that will return a value: ``` Function Something { # Do a PowerShell cmd here: if the command succeeded, return true # If not, then return false } ``` Then have a second function that will only run if the above function is true: ``` Function OnlyTrue { # Do a PowerShell cmd here... } ```

Original source

Related problems