azure cli - 使用 azure CLI 为 azure 中的 web 应用程序列表检索并创建 webjobs

问题描述 投票:0回答:1

我正在尝试创造网络就业机会(如果不存在的话)。 对于单个 web 应用程序,这行命令有效。

az webapp deployment source config-zip --resource-group "rg1" --name "webapp1" --src "path\file.zip"

Zip 文件结构的创建方式如下所述 https://github.com/projectkudu/kudu/wiki/WebJobs

我可以看到创建的网络作业,但是当我将其放入循环中时出现错误。 这是我到目前为止的代码。

# Resource group name........
resourceGroup="rg1"

# Path to the zip file containing the web job
zipFilePath="zipfilepath/file.zip"

# Web job name
webJobName="webjob1"

# Get a list of all web apps within the resource group
webAppNames=$(az webapp list --resource-group $resourceGroup --query "[].name" -o tsv)

# Loop over each web app name
for webAppName in $webAppNames; do
    # Check if the web job already exists in the web app
    if az webapp webjob triggered list --resource-group $resourceGroup --name $webAppName --query "[?name=='$webJobName']" -o tsv | grep -q "$webJobName"; then
        echo "Web job '$webJobName' already exists in web app '$webAppName'. Skipping deployment."
    else
        echo "Deploying web job '$webJobName' to web app: $webAppName"
        az webapp deployment source config-zip --resource-group $resourceGroup --name $webAppName --src $zipFilePath
    fi
done

错误

At line:14 char:4
+ for webAppName in "${webAppNames[@]}"; do
+    ~ Missing opening '(' after keyword 'for'. At line:14 char:42
+ for webAppName in "${webAppNames[@]}"; do
+                                          ~ Missing statement body in do loop.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingOpenParenthesisAfterKeyword
azure azure-web-app-service azure-webjobs azure-cli azure-webapps
1个回答
0
投票

在运行脚本之前,我在将代码粘贴到我的 PowerShell 环境中时发现错误。

enter image description here

由于您提供的脚本完全是CLI代码格式,因此在Cloud Shell环境中可以正常执行,如下所示。

enter image description here

要使其在 Windows PowerShell ISE 环境中执行时按预期工作,请使用 PowerShell 语法运行以下修改后的脚本。

在这里,我使用了

foreach
循环来循环遍历所有列出的 Web 应用程序,并使用
if
循环来检查是否存在。

$resourceGroup = "xxxx"
$zipFilePath = "C:\Users\xxx\source\repos\ConsoleApp7\ConsoleApp7\xxxx"
$webJobName = "xxxx"
$webAppNames = (az webapp list --resource-group $resourceGroup --query "[].name" -o tsv)
foreach ($webApp in $webAppNames) {
    # Check if the web job already exists in the web app
    $existing = az webapp webjob triggered list --resource-group $resourceGroup --name $webApp --query "[?name=='$webJobName']" -o tsv
    if ($existing -contains $webJobName) {
        Write-Host "Web job '$webJobName' already exists in web app '$webApp'. Skipping deployment."
    }
    else {
        Write-Host "Deploying web job '$webJobName' to web app: $webApp"
        az webapp deployment source config-zip --resource-group $resourceGroup --name $webApp --src $zipFilePath
    }
}

enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.