runbook中的powershell脚本在hybrid worker上执行,当找不到文件时出错。但是当通过ADF webhook调用时,该活动通过。有人能告诉我如何捕获ADF中的错误吗?
脚本:
param(
[Parameter (Mandatory = $false)]
[object] $WebhookData
)
Import-Module Az.Storage -force
if($WebhookData){
$parameters=(ConvertFrom-Json -InputObject $WebhookData.RequestBody)
if($parameters.callBackUri) {$callBackUri=$parameters.callBackUri}
}
TRY
{
$connectionName = ***************
$storageAccountName = *****************
$sasToken = ************************
$context = New-AzStorageContext -StorageAccountName $storageAccountName -SASToken $sasToken
$localFile = "C:VenaIntegrateDataIn"
$filename = "exporthierarchy" + (Get-Date -format "yyyyMMdd") + ".psv"
$filename2 = "exportattributes" + (Get-Date -format "yyyyMMdd") + ".psv"
$path = $localFile + $filename
$path2 = $localFile + $filename2
$filesystemName = ******
Set-AzStorageBlobContent -File $path -Container $filesystemName -Context $context -Force
Set-AzStorageBlobContent -File $path2 -Container $filesystemName -Context $context -Force
}
CATCH {
Write-Error "Error Occured"
$callBackUri = $parameters.callBackUri
# Create an error message
# Message and statuscode will show up in ADF
$body = [ordered]@{
error = @{
ErrorCode = "Error Occured"
Message = "File not found"
}
statusCode = "404"
}
# Convert the string into a real JSON-formatted string
$bodyJson = $body | ConvertTo-Json
# Call back with error message in body and a JSON contenttype
Invoke-WebRequest -UseBasicParsing -Uri $callBackUri -Method Post -Body $bodyJson -ContentType "application/json"
}
#$callBackUri = $parameters.callBackUri
If (!$callBackUri)
{
# Create an error message
# Message and statuscode will show up in ADF
$body = [ordered]@{
error = @{
ErrorCode = "ParameterError"
Message = "Required parameters where not provided"
}
statusCode = "404"
}
# Convert the string into a real JSON-formatted string
$bodyJson = $body | ConvertTo-Json
# Call back with error message in body and a JSON contenttype
Invoke-WebRequest -UseBasicParsing -Uri $callBackUri -Method Post -Body $bodyJson -ContentType "application/json"
}
else
{
$body = [ordered]@{
success = @{
SuccessCode = "Process success"
Message = "Process completed successfully"
}
}
# Convert the string into a real JSON-formatted string
$bodyJson = $body | ConvertTo-Json
# Call back with error message in body and a JSON contenttype
Invoke-WebRequest -UseBasicParsing -Uri $callBackUri -Method Post -Body $bodyJson -ContentType "application/json"
}
尽管runbook在自动化帐户中失败,但当通过runbook调用时,它在ADF中成功没有可用的输出。您可以通过在回调的请求体">
中设置输出属性来指定webhook活动输出。您需要捕获命令"set - azstorageblobcontent"的返回值。例如。
$is_uploaded_path1 = Set-AzStorageBlobContent -File $path -Container $filesystemName -Context $context -Force
$is_uploaded_path2 = Set-AzStorageBlobContent -File $path2 -Container $filesystemName -Context $context -Force
现在你可以在catch块
中做这样的事情If(!$is_uploaded_path1)
{
Invoke-WebRequest -UseBasicParsing -Uri $callBackUri -Method Post -Body $bodyJson -ContentType "application/json"
}
请注意,您也可以使用特定的文件名修改错误。