[英]Output Azure Function powershell value to azure storage account
我有一個 powershell 腳本在 azure function 應用程序中運行,該應用程序獲取 json IP 列表,解析它並返回一個 IP 列表,每行 1 個。 我需要將這個 output 保存到特定的 azure 存儲帳戶中。 我正在嘗試使用存儲帳戶中的 static 網站為其他人創建一個 HTTP 端點以檢索 IP 列表。
這是腳本
# Input bindings are passed in via param block.
param($Timer)
# Get the current universal time in the default string format.
$currentUTCtime = (Get-Date).ToUniversalTime()
# The 'IsPastDue' property is 'true' when the current function invocation is later than scheduled.
if ($Timer.IsPastDue) {
Write-Host "PowerShell timer is running late!"
}
# Write an information log with the current time.
Write-Host "PowerShell timer trigger function ran! TIME: $currentUTCtime"
#-------------------------------------------------------------------------------------------
$url = "https://ip-ranges.atlassian.com/"
Invoke-RestMethod $url
$iplist = Invoke-RestMethod $url
$iplist.items | select-object -ExpandProperty cidr
out-file $outputBlob
我已經在 azure 中測試了 function,它在那里運行得很好。 我似乎無法使 function 應用程序的集成輸出部分正常工作。 輸出的設置是
Binding type - azure blob storage
blob paramter name - outputBlob
Path - test/list.txt
storage account connection - searched and selected the storage account
我沒有找到太多關於如何將我的 powershell 腳本 output 添加到此存儲帳戶的文檔。 輸出文件顯然不起作用。
----------更新代碼----------
這是現在成功將文件保存到容器中的代碼,但我仍然無法保存到 static 網站的 $web 容器中。 $ 不是我可以在 output 綁定中使用的東西
# Input bindings are passed in via param block.
param($Timer)
# Get the current universal time in the default string format.
$currentUTCtime = (Get-Date).ToUniversalTime()
# The 'IsPastDue' property is 'true' when the current function invocation is later than scheduled.
if ($Timer.IsPastDue) {
Write-Host "PowerShell timer is running late!"
}
# Write an information log with the current time.
Write-Host "PowerShell timer trigger function ran! TIME: $currentUTCtime"
#------------------------
$url = "https://ip-ranges.atlassian.com/"
Invoke-RestMethod $url
$call = Invoke-RestMethod $url
$iplist = $call.items | select-object -ExpandProperty cidr
$output = out-string -InputObject $iplist
Push-OutputBinding -Name outputBlob -Value $output
outputBlob 綁定在 integration > outputs > 和 Path 下配置,格式為容器/文件。 我無法指定 $web/file.txt...但如果我指定 web/file.txt,它將創建一個 web 容器並將 output 作為 file.txt 放入其中。 我需要這樣做,但它必須在 $web 容器中為 go。
這是我一直想嘗試一段時間但實際上並沒有抽出時間去做的事情。 當我看到你的問題時決定今天試一試。
因此可以使用 blob output 綁定推送內容,但功能有限。
using namespace System.Net
# Input bindings are passed in via param block.
param (
$Request,
$TriggerMetadata
)
# Call the atlassian API to get the address ranges
$atlassianUri = "https://ip-ranges.atlassian.com/"
$iplist = Invoke-RestMethod $atlassianUri -ErrorAction Stop
$cidrList = $iplist.items | select-object -ExpandProperty cidr
# Push the contents to blob storage using the outputBinding
Push-OutputBinding -Name myOutputBlob -Value $cidrList
# Return a simple response so I know it worked
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = [HttpStatusCode]::OK
Body = 'Successfully Updated Blob Storage'
})
您必須在 function 中包含一個計時器輸入綁定,但我使用了 HTTP 以便我可以按需觸發它以測試它是否有效。
我提供了 static 到 blob output 綁定的路徑。 根據這個開放的 GitHub 問題,還不能從 function 中動態分配Path
屬性。
{
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "Request",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "Response"
},
{
"name": "myOutputBlob",
"type": "blob",
"path": "functioncopy/ipRanges.txt",
"connection": "MyStorageConnectionAppSetting",
"direction": "out"
}
]
}
但是,當將數據發送到存儲帳戶中的 blob 文件時,上面的代碼有效Push-OutputBinding
將內容序列化為 JSON 數組,例如
這可能對你有用,也可能不適合你,但我認為沒有辦法使用 output 綁定來獲得原始列表。
但是,您可以在 function 中使用Az.Storage
模塊,在 function 執行中創建文件並以這種方式上傳
# Variables required - Fill these out
$storageAccountName = '<Insert Storage Account Here'
$containerName = '<Insert StorageContainer Name Here>'
$resourceGroupName = '<Insert resourceGroup Name Here>'
$subscriptionId = '<Insert subscriptionId Here>'
# Call the atlassian API to get the address ranges
$atlassianUri = "https://ip-ranges.atlassian.com/"
$iplist = Invoke-RestMethod $atlassianUri -ErrorAction Stop
$cidrList = $iplist.items | select-object -ExpandProperty cidr
# New-TemporaryFile uses [System.IO.Path]::GetTempPath() location
$tempFile = New-TemporaryFile
# Set the context to the subscription you want to use
# If your functionApp has access to more than one subscription it will load the first subscription by default.
# Possibly a good habit to be explicit about context.
Set-AzContext -Subscription $subscriptionId
# Get the Storage Account Key to authenticate
$storAccKeys = Get-AzStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccountName
$primaryKey = $storAccKeys | Where-Object keyname -eq 'key1' | Select-Object -ExpandProperty value
# Write the CIDR list to the temp file created earlier
$cidrList | Out-File $tempFile
# Create a Storage Context which will be used in the subsequent commands
$storageContext = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $primaryKey
# Upload the temp file to blob storage
$setAzStorageBlobContentSplat = @{
Container = $containerName
File = $tempFile.FullName
Blob = 'ipranges.txt'
Context = $storageContext
Properties = @{
ContentType = 'text/plain'
}
}
Set-AzStorageBlobContent @setAzStorageBlobContentSplat
# Return a simple response so I know it worked
Push-OutputBinding -Name Response -Value (
[HttpResponseContext]@{
StatusCode = [HttpStatusCode]::OK
Body = 'Successfully Updated Blob Storage'
}
)
您可以在此處查看有關 Set-AzStorageBlobContent 的文檔以獲取相關示例: https://learn.microsoft.com/en-us/powershell/module/az.storage/set-azstorageblobcontent?view=azps-6.2.1#examples
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.