Hello
I am usring a runbook which fuction is to run a script of VM.
The runbook code is here.
param(
[Parameter(Mandatory = $true)]
[string] $ResourceGroupName,
[Parameter(Mandatory = $true)]
[string] $VirtualMachineName)
$connectionName = 'AzureRunAsConnection'
$conn = Get-AutomationConnection -Name $connectionName
Connect-AzAccount -ServicePrincipal -Tenant $conn.TenantID `
-ApplicationId $conn.ApplicationID -CertificateThumbprint $conn.CertificateThumbprint
$GetAcsessToken = Get-AzAccessToken
$auth = $GetAcsessToken.token
$authHeader = @{
'Content-Type'='application/json'
'Accept' = 'application/json'
'Authorization'="Bearer $auth"
}
$restUri = 'https://management.azure.com/subscriptions/xxxxxxxxxxxxxx/resourceGroups/' + $ResourceGroupName + '/providers/Microsoft.Compute/virtualMachines/' + $VirtualMachineName + '/runCommand?api-version=2019-03-01'
$a_body = @{
'commandId' = 'RunShellScript'
'script' = @( '/tmp/test.sh')
}
\# Rest API
$response = Invoke-webrequest -Uri $restUri -Method post -Headers $authHeader -Body $($a_body | ConvertTo-Json) -UseBasicParsing
$asyncstatus = $($response.headers.'Azure-AsyncOperation')
$status = 'InProgress'
While($status -eq 'InProgress')
{
Start-Sleep -s 5
$response = invoke-webrequest -uri $asyncstatus -Headers $authHeader -UseBasicParsing
$status = $($response.Content | ConvertFrom-Json).status
Write-Output "Status : $status"
}
The script code is here named test.sh
# !/bin/bash
if [ -d ./foo ]; then
echo "there is an dirctory"
exit 0
else
echo "there is no dirctory"
exit 9
fi
There is no dirctory in the OS.
when i use this runbook how to get the number 9 to runbook.
I want a Failure status when I run this runbook by the return code.
Could someone help me to solve this problem.
If cannot get the return code from script plz let me know the detail.
Thanks so much.
Best regards.