如何在Azure中启用/禁用资源组中的功能应用程序

时间:2019-04-05 09:54:26

标签: azure powershell azure-functions azure-runbook

我正在编写 powershell 脚本,该脚本将在 runbook 中运行。 该脚本将首先禁用资源组中的功能应用,然后另一个类似脚本将启用资源组中的功能应用

我已经尝试过以下脚本,但未启用功能应用

$connectionName = "AzureRunAsConnection"
try {
    # Get the connection "AzureRunAsConnection "
    $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName         

    "Logging in to Azure..."
    Add-AzureRmAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint  } catch {
    if (!$servicePrincipalConnection)
    {
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
    } else{
        Write-Error -Message $_.Exception
        throw $_.Exception
    }
}

$RGName = Get-AutomationVariable -Name 'DRPrimaryResourceGroupName'
$FAppResources = Get-AzureRmResource -ResourceType "Microsoft.Web/sites" -ResourceGroupName $RGName -ExpandProperties  
$LAppResources = Get-AzureRmResource -ResourceType "Microsoft.Logic/workflows" -ResourceGroupName $RGName -ExpandProperties 

ForEach ($FAResource in $FAppResources) {
        Write-Output ("*****************************************************************************************")
        Write-Output ("Starting FunctionApp "+ $FAResource.ResourceName+ " in Resource Group " +$RGName)
        Write-Output ("*****************************************************************************************")
        Start-AzureRmWebApp -ResourceGroupName $RGName -Name $FAResource.ResourceName
        Write-Output ($FAResource.ResourceName + " and its status is " +  $FAResource.Properties.State)
}
ForEach ($LAResource in $LAppResources) {  
        Write-Output ("*****************************************************************************************")
        Write-Output ("Enabling Logic App "+ $LAResource.ResourceName +" in Resource Group"+ $RGName)  
        Write-Output ("***************************************************************************************")
        Set-AzureRmLogicApp -ResourceGroupName $RGName -Name $LAResource.ResourceName -State Enabled -Force
        Write-Output ($LAResource.ResourceName + " and its status is " +  $LAResource.Properties.State)
}

1 个答案:

答案 0 :(得分:0)

我想您已经看到了Runbook的输出,它看起来像下面的屏幕截图。因此,您认为您的脚本无法启用功能应用。

enter image description here

实际上,您的脚本启用了功能应用,但该行  def puzzleA(wordList): tempList1 = [] tempList2 = [] tempList3 = [] for word in wordList: if word[-2:]=='er': tempList1.append(word) if word[-3:]=='est': tempList2.append(word) for word1 in wordList: for word2 in tempList1: if word1==word2[:-2]: tempList3.append(word1) for word1 in tempList3: for word2 in tempList2: if word1==word2[:-3]: print('{}, {}er, {}'.format(word1,word1,word2)) 可能会写出旧状态。

要解决此问题,您可以尝试添加行Write-Output ($FAResource.ResourceName + " and its status is " + $FAResource.Properties.State),启动功能应用程序后它会获得新状态,写出$newstate = (Get-AzureRmWebApp -ResourceGroupName $RGName -Name $FAResource.ResourceName).State,然后它将正常工作。

完整脚本:

$newstate

enter image description here