将查询结果分配给 Azure-Runbooks 中 powershell 中的变量

Assign the result of a query to a variable in powershell in Azure-Runbooks

我有一本在 Azure 中使用自动化的操作手册。它从 table 得到一个整数结果,它可以 return 正确的值。我正在使用的代码在下面并且有效。

 $SQLServerCred = Get-AutomationPSCredential -Name "SqlCredential"
 #Import the SQL Server Name from the Automation variable.
 $SQL_Server_Name = Get-AutomationVariable -Name "SqlServer"
 #Import the SQL DB from the Automation variable.
 $SQL_DB_Name = Get-AutomationVariable -Name "Database"


 $Query = "select max(je.ExecutionOrder) as LastStepExecuted
from PPoint.JobExecutionHistory je
where je.EventType = 'Start'
and je.JobRunId = PPoint.fnGetJobRunID()"

 invoke-sqlcmd -ServerInstance "$SQL_Server_Name" -Database "$SQL_DB_Name" -Credential $SQLServerCred -Query "$Query" -Encrypt

我的下一步是将查询结果分配给一个变量,然后对其求值以查看是否应调用另一个 runbook。所以我想有一个名为 LastStep 的变量,并将下面查询中 LastStepExecuted 的整数结果分配给它。然后我想沿着这条线做点什么(这是伪代码)

if LastStep = 2147483647
 call another runbook
else
 do nothing - end the runbook

我尝试了几种方法来捕获变量中的 LastStepExecuted,但我无法弄清楚。有人可以帮忙吗?

非常感谢任何帮助或建议。

您可以使用 Start-AzAutomationRunbook cmdlet 从自动化帐户中的另一本 运行 图书中触发子 运行图书。

$SQLServerCred = Get-AutomationPSCredential -Name "SqlCredential"
 #Import the SQL Server Name from the Automation variable.
 $SQL_Server_Name = Get-AutomationVariable -Name "SqlServer"
 #Import the SQL DB from the Automation variable.
 $SQL_DB_Name = Get-AutomationVariable -Name "Database"


 $Query = "select max(je.ExecutionOrder) as LastStepExecuted
from PPoint.JobExecutionHistory je
where je.EventType = 'Start'
and je.JobRunId = PPoint.fnGetJobRunID()"

$LastStep=invoke-sqlcmd -ServerInstance "$SQL_Server_Name" -Database "$SQL_DB_Name" -Credential $SQLServerCred -Query "$Query" -Encrypt

if($LastStep -eq 2147483647)
{
    Start-AzAutomationRunbook -AutomationAccountName "MyAutomationAccount" -Name "Test-ChildRunbook" -ResourceGroupName "LabRG" -DefaultProfile $AzureContext  -Parameters $params -Wait
}
else
{
    Write-Output "conditionfailed the value of $LastStep"
}

您可以参考 this 文档,关于 Azure 自动化中的模块化 运行 书籍。