'Performing a "WHILE" Loop in an Asnyc Function

I definitely feel like I've done my due diligence in trying to sort this one out, so bear with me as I try to explain what I'd like to accomplish.

I have a PowerShell script that automates many different offboarding tasks. Some of these tasks can take quite some time. For example, waiting on an e-mail export to complete can take hours depending on the employee and their length of time with the company. So, rather than performing my "while" loop in the script itself (and log jamming all of the next steps) I am trying to pass it off to an async function to run in the background while the rest of my offboarding script moves along.

I can tell that the async function successfully runs and the required variables are being passed, but... nothing is happening. I'm getting no updates printed to my log file. I'm getting no updates to the entry in my SQL db. I'm truly at a loss with this one.

Here is the call to my function in the main script:

$VaultParameters = @{
    employee_name = "$employee_name"
    export_name = "$export_name"
    sql_id = "$sql_id"
    vault_status_id = "$vault_status_id"
}
VaultExport @VaultParameters

And here is the function itself:

function VaultExport {
    param (
        [cmdletbinding()]
        [parameter()]
        [string]$employee_name,
        [parameter()]
        [string]$export_name,
        [parameter()]
        [string]$sql_id,
        [parameter()]
        [string]$vault_status_id
    )

    $scriptBlock = {
        param ($employee_name,$export_name,$sql_id,$vault_status_id)
        $vault_ready = "no"
        while ($vault_ready -eq "no") {
            $vault_status = gam info export "Email Exports" "$export_name"
            $vault_status = $vault_status -Match "COMPLETED"
            $vault_status = $vault_status -split(": ")
            $vault_status = $vault_status[1]
            Invoke-SqlUpdate -Query "UPDATE ``db_name``.``table_name`` SET ``vault_status`` = '$vault_status' WHERE ``id`` = '$vault_status_id'"
            if ($vault_status -eq "COMPLETED") {
                $vault_ready = "yes"
                $completed = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
                Invoke-SqlUpdate -Query "UPDATE ``db_name``.``table_name`` SET ``vault_completed`` = '$completed' WHERE ``id`` = '$vault_status_id'"
                Invoke-SqlUpdate -Query "UPDATE ``db_name``.``table_name`` SET ``vault_status`` = '1' WHERE ``id`` = '$sql_id'"
                $LoggingParameters = @{
                    logfile = "C:\script_logs\threxit.log"
                    log = "INFO: (GVEF, $employee_name) $export_name is ready ($vault_status). Downloading now..."
                }
                EventLogging @LoggingParameters
                Write-Output "$export_name is ready ($vault_status). Downloading now..."
            } else {
                $vault_status = gam info export "Email Exports" "$export_name"
                $vault_status = $vault_status -Match "IN_PROGRESS"
                $vault_status = $vault_status -split(": ")
                $vault_status = $vault_status[1]
                $LoggingParameters = @{
                    logfile = "C:\script_logs\threxit.log"
                    log = "INFO: (GVEF, $employee_name) $export_name is not yet ready ($vault_status). Checking again in ten seconds."
                }
                EventLogging @LoggingParameters
                Write-Output "$export_name is not yet ready ($vault_status). Checking again in ten seconds."
                Start-Sleep 10
            }
        }
    }
    Start-Job -ScriptBlock $scriptBlock -ArgumentList @($employee_name,$export_name,$sql_id,$vault_status_id)
}
exit


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source