响应内容不是有效的JObject:Azure函数和PowerShell
我正试图通过数据工厂管道执行此Azure函数,并收到以下错误“响应内容不是有效的JObject”。为此,我已将此错误添加到PowerShell脚本的最后一节-->Body=“{”“响应”“:”“+$contents+”“}” 现在它正在向data Lake写入数据,但我的管道失败,出现错误:“响应内容不是有效的JObject”。我在这里做错了什么 Azure函数中的PowerShell脚本:响应内容不是有效的JObject:Azure函数和PowerShell,powershell,azure-functions,azure-data-factory,azure-data-lake-gen2,azure-data-factory-pipeline,Powershell,Azure Functions,Azure Data Factory,Azure Data Lake Gen2,Azure Data Factory Pipeline,我正试图通过数据工厂管道执行此Azure函数,并收到以下错误“响应内容不是有效的JObject”。为此,我已将此错误添加到PowerShell脚本的最后一节-->Body=“{”“响应”“:”“+$contents+”“}” 现在它正在向data Lake写入数据,但我的管道失败,出现错误:“响应内容不是有效的JObject”。我在这里做错了什么 Azure函数中的PowerShell脚本: using namespace System.Net # Input bindings are pas
using namespace System.Net
# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)
# Connect to Azure Account
$username = "xyz@abc.onmicrosoft.com"
$pass = ConvertTo-SecureString "**********" -AsPlainText -Force
$cred = New-Object PSCredential($username,$pass)
Connect-AzAccount -Credential $cred
# Input Variables
$dataFactoryName="production-gen2"
$resourceGroupName="DataLake-Gen2"
# get dataFactory triggers
$triggers=Get-AzDataFactoryV2Trigger -DataFactoryName $dataFactoryName -ResourceGroupName $resourceGroupName
$datas=@()
foreach ($trigger in $triggers) {
# get the trigger run history
$today = Get-Date
$yesterday = $today.AddDays(-1)
$splat = @{
ResourceGroupName = $trigger.ResourceGroupName
DataFactoryName = $trigger.DataFactoryName
TriggerName = $trigger.Name
TriggerRunStartedAfter = $yesterday
TriggerRunStartedBefore = $today
}
$historys =Get-AzDataFactoryV2TriggerRun @splat
if($historys -ne $null){
# create date
foreach($history in $historys){
$obj =[PsCustomObject]@{
'TriggerRunTimestamp ' = $history.TriggerRunTimestamp
'ResourceGroupName ' =$history.ResourceGroupName
'DataFactoryName' =$history.DataFactoryName
'TriggerName ' = $history.TriggerName
'TriggerRunId'= $history.TriggerRunId
'TriggerType'=$history.TriggerType
'Status' =$history.Status
}
# add data to an array
$datas += $obj
}
}
}
# convert data to csv string
$contents =(($datas | ConvertTo-Csv -NoTypeInformation) -join [Environment]::NewLine)
# upload to Azure Data Lake Store Gen2
#1. Create a sas token
$accountName="dna2020gen2"
# $path = New-Item -ItemType Directory -Path ".\$((Get-Date).ToString('yyyy-MM-dd'))"
$YY = (Get-Date).year
$MM = (Get-Date).month
$DD = get-date –f dd
$fileSystemName="dev"
$filePath="Input/Triggers/YYYY=$YY/MM=$MM/DD=$DD/data.csv"
$account = Get-AzStorageAccount -ResourceGroupName 'DataLake-Gen2' -Name $accountName
$sas= New-AzStorageAccountSASToken -Service Blob -ResourceType Service,Container,Object `
-Permission "racwdlup" -StartTime (Get-Date).AddMinutes(-10) `
-ExpiryTime (Get-Date).AddHours(2) -Context $account.Context
$baseUrl ="https://{0}.dfs.core.windows.net/{1}/{2}{3}" -f $accountName , $fileSystemName, $filePath, $sas
#2. Create file
$endpoint =$baseUrl +"&resource=file"
Invoke-RestMethod -Method Put -Uri $endpoint -Headers @{"Content-Length" = 0} -UseBasicParsing
$r = Invoke-WebRequest -Uri https://management.azure.com/subscriptions/{guid}/resourcegroups?api-version=2016-09-01 -Method GET -Headers $authHeaders
$r.Headers["x-ms-ratelimit-remaining-subscription-reads"]
#3 append data
$endpoint =$baseUrl +"&action=append&position=0"
Invoke-RestMethod -Method Patch -Uri $endpoint -Headers @{"Content-Length" = $contents.Length} -Body $contents -UseBasicParsing
#4 flush data
#$endpoint =$baseUrl + ("&action=flush&position={0}" -f $contents.Length)
#Invoke-RestMethod -Method Patch -Uri $endpoint -UseBasicParsing
#Check the result (get data)
Invoke-RestMethod -Method Get -Uri $baseUrl -UseBasicParsing
# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = [HttpStatusCode]::OK
Body = "{ ""Response"":"""+$contents+"""}"
})
尝试跟踪您分配给Body的值。很可能,这不是客户端所期望的:例如,不是格式良好的Json。根据
$contents
值,用双引号将其括起来可能不够。您可能希望改为这样做:
Body = @{ Response = $contents } | ConvertTo-Json
我从来没有这样想过。是的,我试过你的解决方案,效果很好!非常感谢