Conditionally Execute Function Call Statements in PowerShell

Viewed 21

I want to conditionally call a function defined inside another function, but the condition block doesn't seem to evaluate when the function is called.

Suppose, a parent function is defined with mandatory boolean parameters that would act as sort of activation variables for the function call statements for the child functions inside the parent function. So that their truth value will determine whether the child function will be executed or not. That's my goal.

function __Input_Dispatch_Center_Control_Function__ {
        [CmdletBinding()] param(

            [Parameter(Position = 0,  Mandatory = $True)] [bool] $Global:SFA_CHKDSK_EXECUTION_FUNCTION_STATUS

        )

       # Condition to check if variable is true
       if($Global:SFA_CHKDSK_EXECUTION_FUNCTION_STATUS -eq $True) {

                function Run_CHKDSK_Utility_Execution_Function {

                    Write-Host "[*] Running CHKDSK" -ForegroundColor Yellow
    
                    # Determine volumes present in the system, and run chkdsk on all those volumes
                    $Volume = Get-Volume
                    $Global:VolumeNumber = $Volume.Count
                    $i = 0

                    foreach ($Letter in $Volume.DriveLetter) {
                        if($i -eq $Global:VolumeNumber) {

                            break
                        } else {

                            # run chkdsk on all volumes
                            Write-Host "[*] Currently checking drive: $($Volume.DriveLetter[$i])" -ForegroundColor Yellow
                            chkdsk "$($Volume.DriveLetter[$i]):" /r
                            if($LASTEXITCODE -eq 0) {

                                Write-Host "[+] No errors were found." -ForegroundColor Green
                            } elseif ($LASTEXITCODE -eq 1) {

                                Write-Host "[+] Errors were found and fixed." -ForegroundColor Green
                            } elseif ($LASTEXITCODE -eq 2) {

                                Write-Host "[+] Performed disk cleanup (such as garbage collection) or did not perform cleanup because /f was not specified." -ForegroundColor Green
                            } elseif ($LASTEXITCODE -eq 3) {

                                Write-Host "[-] Could not check the disk, errors could not be fixed, or errors were not fixed because /f was not specified." -ForegroundColor Red
                            }
                            $i++
                        }
                    }
                }
                
                # Function call
                Run_CHKDSK_Utility_Execution_Function
            }
}

# Call to IDCCF
__Input_Dispatch_Center_Control_Function__

Questions

  1. When I run this, I noticed that no matter what the value of $Global:SFA_CHKDSK_EXECUTION_FUNCTION_STATUS, the function call statement → Run_CHKDSK_Utility_Execution_Function, is always executed. I also tried moving the function definition outside the conditional call block (function call inside the if block), but that doesn't work either. I don't see why?

  2. Is there is any way that a different script plugs-in these parameter values? I mean, plugging parameter values to a function seems to be a manual process. How can another script do this automatically, given that parameter values are determined?

0 Answers
Related