如何在 Azure 自动化中从父 (powershell) Runbook 内联调用子 (powershell) Runbook?

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

在尝试创建一些可重用的 powershell 代码时,我试图让子 Runbook 的内联调用正常工作。我对 PowerShell 和 Azure 自动化还比较陌生。

到目前为止,我从父工作簿到子 Runbook 进行内联调用的所有尝试均失败,并出现以下错误:

术语“./.ps1”不被识别为 cmdlet、函数、脚本文件或可执行程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。

两个 Runbook 都是 PowerShell(版本 7.1)。两个 Runbook 都驻留在同一个自动化帐户下。

为了清楚起见,我尝试将其简化为最简单的形式。以下是 child runbook 的内容,名为 rnbk_test_child

Write-Output "Hello, this is the child runbook."

父 Runbook 也有一行代码,如下所示:

./rnbk_test_child.ps1

在创建和测试父 Runbook 之前,我首先发布了子 Runbook。我主要根据这篇文章中的信息进行工作。

powershell azure-automation azure-runbook
3个回答
1
投票

您可以使用

Start-AzAutomationRunbook
模块中的 Az cmdlet
Az.Automation
调用子 Runbook。

$automationAccountName = '<InsertHere>'
$runbookName = '<InsertHere>'
$automationAccountResourceGroup = '<InsertHere>'

$startAutomationRunBookSplat = @{
        AutomationAccountName = $automationAccountName 
        Name                  = $runbookName
        ResourceGroupName     = $automationAccountResourceGroup 
        Parameters            = $ParametersHashtable
    }
    
    Start-AzAutomationRunbook @startAutomationRunBookSplat -verbose

有关用法的文档:https://learn.microsoft.com/en-us/powershell/module/az.automation/start-azautomationrunbook?view=azps-7.4.0


1
投票

我不确定您是否已经弄清楚这一点,但据我所知,您在父 Runbook 中需要做的就是调用子 Runbook,就好像它是内联 powershell 函数一样

例如

runbktestchild -parameter1 "value"

我认为如果 Runbook 名称中包含下划线,它将不起作用:S


0
投票

不幸的是,使用以下命令执行子脚本

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