[英]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.