任务计划程序中具有异步功能的 PowerShell 脚本

问题描述 投票:0回答:2

我有一个相当大的 PowerShell 脚本,我已将其分成两个单独的脚本。第一个脚本以调用异步函数结束,然后退出。

该函数检查上一个脚本中生成的电子邮件导出的状态,执行 WHILE 循环,每 60 秒调用一次 Google 以查看导出是否准备就绪。导出准备就绪后,它会更新几个 SQL 数据库,我的第二个脚本就会接管。

当我在 shell/控制台中运行“第一个脚本”时,这 100% 有效。但我开始注意到,当我的计划任务在任务计划程序中触发时......什么也没有发生。我有大量的日志记录,所以我知道参数正在发送到异步函数,但它似乎只是抛出而不是继续循环 WHILE 并每六十秒检查一次。

我觉得我已经在谷歌搜索中完成了尽职调查,但是......任务计划程序作业中我是否缺少一些东西来确保包含 WHILE 循环的函数能够正确运行?

编辑如下

为了更好地解释我正在做的事情,我将包括从我的函数中剥离的代码以及从下面的主脚本中对函数的调用。

首先,调用“script_01”最末尾的函数。

# Let's send the Google Vault export information over to our function.
        Try {
            $VaultParameters = @{
                employee_name = "$employee_name"
                export_name = "$export_name"
                sql_id = "$sql_id"
                vault_status_id = "$vault_status_id"
            }
            VaultExport @VaultParameters

            $LoggingParameters = @{
                logfile = "C:\script_logs\log.log"
                log = "INFO: Sent the Google Vault export information over to our async function."
            }
            EventLogging @LoggingParameters
        } Catch {
            $LoggingParameters = @{
                logfile = "C:\script_logs\log.log"
                log = "ERROR: Could not send the Google Vault export information over to our async function.`n$_"
            }
            EventLogging @LoggingParameters
        }

现在是函数本身。很大...

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)
        Import-Module SimplySQL
        $logfile = "C:\script_logs\log.log"
        $now = (Get-Date).tostring("MM-dd-yyyy hh:mm:ss")

        # Let's define our MySQL database credentials for later use.
        # DEFINING SQL CREDS HERE

        # Let's generate secure credentials for our MySQL 'terms' db.
        # GENERATING SECURE CREDS HERE

        # And now we'll connect to our SQL db...
        # CONNECTING TO SQL HERE

        $vault_ready = "no"
        Add-Content $logfile "$now INFO: Beginning the WHILE loop while $export_name completes..."
        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]
            if ($vault_status -eq "COMPLETED") {
                $vault_ready = "yes"
                $completed = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
                Invoke-SqlUpdate -Query "UPDATE ``table`` SET ``vault_status`` = '$vault_status', ``vault_completed`` = '$completed' WHERE ``id`` = '$vault_status_id'"
                Invoke-SqlUpdate -Query "UPDATE ``table`` SET ``vault_status`` = '1' WHERE ``id`` = '$sql_id'"
                $now = (Get-Date).tostring("MM-dd-yyyy hh:mm:ss")
                Add-Content $logfile "$now INFO: $export_name is ready to download. Updated vault_status in our dbs."
            } 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]
                Invoke-SqlUpdate -Query "UPDATE ``table`` SET ``vault_status`` = '$vault_status' WHERE ``id`` = '$vault_status_id'"
                $now = (Get-Date).tostring("MM-dd-yyyy hh:mm:ss")
                Add-Content $logfile "$now INFO: $export_name is not yet ready: ($vault_status). Checking again in sixty seconds."
                Start-Sleep 60
            }
        }
    }
    Start-Job -ScriptBlock $scriptBlock -ArgumentList @($employee_name,$export_name,$sql_id,$vault_status_id)
}
exit

powershell function asynchronous windows-task-scheduler
2个回答
0
投票

我不确定为什么不让任务每 30 秒执行一次,而不是让进程无限期运行并使用它自己的计时器

满足您为 WHILE 语句设置的条件,这就是循环不继续的原因。 将条件更改为永远不会满足的条件,问题就会消失,即

$Value1 = 0
WHILE($Value1 -ne 1){
#do things, never updating the $value1 variable
}

0
投票

最终的“修复”是将“-NoExit”添加到任务计划程序操作中的参数列表中。

© www.soinside.com 2019 - 2024. All rights reserved.