将 Azure Function powershell 值输出到 azure 存储帐户

Output Azure Function powershell value to azure storage account

我在一个 azure 函数应用程序中有一个 powershell 脚本 运行,它获取一个 json IP 列表,解析它,然后 returns 一个 IP 列表,每行 1 个。我需要获取此输出并将其保存到特定的 Azure 存储帐户。我正在尝试使用存储帐户中的静态网站为其他人创建 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 中测试了这个函数,它在那里运行得很好。我似乎无法使函数应用程序的集成输出部分正常工作。输出设置为

Binding type - azure blob storage
blob paramter name - outputBlob
Path - test/list.txt
storage account connection - searched and selected the storage account

我找不到很多关于如何将我的 powershell 脚本输出到此存储帐户的文档。输出文件显然不起作用。

------------ 更新代码 ------------

这是现在成功将文件保存到容器中的代码,但我仍然无法保存到静态网站的 $web 容器中。 $ 不是我可以在输出绑定中使用的东西

# 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 绑定在集成 > 输出 > 和格式为 container/file 的路径下配置。我无法指定 $web/file.txt...但如果我指定 web/file.txt,它将创建一个 Web 容器并将输出作为 file.txt 放入其中。我需要这样做,但它必须放在 $web 容器中。

这是我一段时间以来一直想尝试但实际上没有时间去做的事情。今天看到你的问题决定试一试。

因此可以使用 blob 输出绑定推送内容,但功能有限。

运行.ps1

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.json

你必须在你的函数中包含一个计时器输入绑定,但我使用了 HTTP 以便我可以触发它 on-demand 来测试它是否有效。

我已经提供了 blob 输出绑定的静态路径。根据 this open GitHub issue.

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 数组,例如

这可能对您有用,也可能不适合您,但我认为没有办法使用输出绑定来获得原始列表。

但是,您可以在函数中使用 Az.Storage 模块,在函数执行中创建文件并以这种方式上传文件

运行.ps1

# 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://docs.microsoft.com/en-us/powershell/module/az.storage/set-azstorageblobcontent?view=azps-6.2.1#examples