Azure - Runbook 내에서 또 다른 Runbook 스크립트를 실행
Runbook 스크립트에서 또 다른 Runbook 스크립트를 당연히 호출할 수 있습니다. 가령 호출하고 싶은 기존 Runbook이 "appRunner"라는 이름으로 다음과 같이 정의한 경우,
param (
[Parameter(Mandatory=$true)][PSObject]$targetWebApp
)
$azureConnection = Get-AutomationConnection -Name AzureRunAsConnection
Add-AzureRmAccount -ServicePrincipal -Tenant $azureConnection.TenantID -ApplicationID $azureConnection.ApplicationID -CertificateThumbprint $azureConnection.CertificateThumbprint
if ($targetWebApp.Mode -Match "Start")
{
Start-AzureRmWebApp -Name $targetWebApp.WebAppName -ResourceGroupName "MyGroup"
}
else
{
Stop-AzureRmWebApp -Name $targetWebApp.WebappName -ResourceGroupName "MyGroup" -Force
}
새로운 Runbook에서 다음과 같이 호출할 수 있습니다. (실제로는 Azure 웹 편집 화면의 좌측에 Runbook 명령이 나열되므로 선택만 하면 됩니다.)
.\appRunner.ps1 -targetWebApp ...[인자]...
문제는 저 인자를 전달하는 방법인데요, 저 환경은 엄연히 PowerShell 스크립트이기 때문에
그 문법에 맞게 JSON 객체를 넘겨줘야 합니다. 그리고 이를 위해서는 다음과 같은 식으로 전처리를 해야 합니다.
$jsonText = @"
{ "WebAppName": "MyTestSite", "Mode": "Stop" }
"@
$jsonObj = $jsonText | ConvertFrom-Json
.\appRunner.ps1 -targetWebApp $jsonObj
참고로, Azure 편집 화면에서 인자를 전달하는 것처럼,
다음과 같은 식으로 전달하는 경우,
.\appRunner.ps1 -targetWebApp { "WebAppName": "MyTestSite", "Mode": "Stop" }
.\appRunner.ps1 -targetWebApp "{ 'WebAppName' : 'MyTestSite', 'Mode' : 'Stop' }"
.\appRunner.ps1 -targetWebApp '{ "WebAppName": "MyTestSite", "Mode": "Stop" }'
모두 이런 식의 오류가 발생합니다.
C:\Temp\jirtb0ff.nua\appRunner.ps1 : A positional parameter cannot be found that accepts argument 'WebAppName\ : \MyTestSite\, \Mode\ : \Stop\ }'.
At line:1 char:1
+ .\appRunner.ps1 -targetWebApp "{ \"WebAppName\" : \"MyTestSite ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [appRunner.ps1], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,appRunner.ps1
{[AzureChinaCloud, AzureChinaCloud], [AzureCloud, AzureCloud], [AzureGermanCloud, AzureGermanCloud], [AzureUSGovernme...
Stop-AzureRmWebApp : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At C:\Temp\jzibymnp.155\appRunner.ps1:14 char:30
+ Stop-AzureRmWebApp -Name $targetWebApp.WebappName -ResourceGroupN ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Stop-AzureRmWebApp], ParameterBindingValidationException
+ FullyQualifiedErrorId :
ParameterArgumentValidationError,Microsoft.Azure.Commands.WebApps.Cmdlets.WebApps.StopAzureWebAppCmdlet
[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]