Azure 函数应用不会加载依赖项



我已经检查了好几次requirements.psd1,它看起来都很好,但函数在运行时返回了这个错误。

[Warning] The Function app may be missing a module containing the 'Set-AzStorageBlobContent' command definition. If this command belongs to a module available on the PowerShell Gallery, add a reference to this module to requirements.psd1. Make sure this module is compatible with PowerShell 7. For more details, see https://aka.ms/functions-powershell-managed-dependency. If the module is installed but you are still getting this error, try to import the module explicitly by invoking Import-Module just before the command that produces the error: this will not fix the issue but will expose the root cause.
2022-09-13T22:12:00.401 [Error] ERROR: The term 'Set-AzStorageBlobContent' is not recognized as the name of a cmdlet, function, script file, or operable program.Check the spelling of the name, or if a path was included, verify that the path is correct and try again.Exception             :Type        : System.Management.Automation.CommandNotFoundExceptionErrorRecord :Exception             :Type    : System.Management.Automation.ParentContainsErrorRecordExceptionMessage : The term 'Set-AzStorageBlobContent' is not recognized as the name of a cmdlet, function, script file, or operable program.Check the spelling of the name, or if a path was included, verify that the path is correct and try again.HResult : -2146233087TargetObject          : Set-AzStorageBlobContentCategoryInfo          : ObjectNotFound: (Set-AzStorageBlobContent:String)

我不确定我错过了什么。我已经阅读了我发现的其他修复程序,相信我已经正确配置了它。感觉";童车";。下面是我的配置:

函数.json

{
"bindings": [
{
"name": "Timer",
"type": "timerTrigger",
"direction": "in",
"schedule": "0 * * * * *"
}
]
}

host.json

{
"version": "2.0",
"managedDependency": {
"Enabled": true
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[2.*, 3.0.0)"
}
}

profile.ps1

#if ($env:MSI_SECRET) {
#    Disable-AzContextAutosave -Scope Process | Out-Null
#    Connect-AzAccount -Identity
#}

要求.psd1

# This file enables modules to be automatically managed by the Functions service.
# See https://aka.ms/functionsmanageddependency for additional information.
#
@{
# For latest supported version, go to 'https://www.powershellgallery.com/packages/Az'. 
# To use the Az module in your function app, please uncomment the line below.
'Az' = '7.*'
'Az.KeyVault' = '4.*'
'Az.Storage' = '4.*'
}

以及运行的脚本

#---------------------------------------------------------[Variables]------------------------------------------------------------
$storageAccountName = 'storageaccount'
$containerName = '$web'
$logContainerName = 'logfiles'
$subscription = 'Subscription'
$resourceGroupName = 'resourcegroup'
$blob = 'info.txt'
$logBlob = 'info.log'
$uri = "https://api.binaryedge.io/v1/minions"
#----------------------------------------------------------[Execution]-------------------------------------------------------------
# Call the API to get the IP addresses
Try {
$call = Invoke-RestMethod $uri -ErrorAction Stop
$list = $call.scanners
# 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.
Set-AzContext -Subscription $subscription
# 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
$list | 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       = $blob
Context    = $storageContext
Properties = @{
ContentType = 'text/plain'
}
}
Set-AzStorageBlobContent @setAzStorageBlobContentSplat -Force
Write-Host Success!
}

Catch {
Out-Host $_.exception.message
}
Finally {
$time = Get-Date
$tempFile = New-TemporaryFile 
"Script last completed at $time" | Out-File $tempFile -Append
$setAzStorageBlobContentSplat = @{
Container  = $logContainerName
File       = $tempFile.FullName
Blob       = $logBlob
Context    = $storageContext
Properties = @{
ContentType = 'text/plain'
}
}
Set-AzStorageBlobContent @setAzStorageBlobContentSplat -Force
}

想明白了。无论出于何种原因,现在都需要Connect-AzAccount。由于我使用了托管标识,因此允许脚本运行的命令是Connect-AzAccount -Identity。您还需要将Az.Accounts添加到requirements.psd1中。这就是解决办法。

在profile.ps1文件中,我还必须取消对行的注释。这是默认的,但我这样做是为了消除其中一个错误。

我将郑重声明。。。几个月前,这还不是运行脚本的必要条件。在注释掉它之前,它在profiles.ps1文件中也不起作用。

我建议按照警告消息中的建议,在Set-AzStorageBlobContent之前调用Import-Module Az.Storage。这可能无法解决问题,但Import-Module会告诉您它无法加载模块的原因。

最新更新