0

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

我已经尝试过下面的脚本,但它没有启用功能应用程序

$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)
}
4

1 回答 1

0

我想您已经看到了 Runbook 的输出,它看起来像下面的屏幕截图。所以你认为你的脚本没有启用函数应用程序。

在此处输入图像描述

实际上,您的脚本启用了函数应用程序,但该行 Write-Output ($FAResource.ResourceName + " and its status is " + $FAResource.Properties.State)只是可以写出旧状态。

要解决这个问题,您可以尝试添加行$newstate = (Get-AzureRmWebApp -ResourceGroupName $RGName -Name $FAResource.ResourceName).State,启动函数应用后它会获得新状态,写出$newstate,然后它会正常工作。

完整脚本:

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
        $newstate = (Get-AzureRmWebApp -ResourceGroupName $RGName -Name $FAResource.ResourceName).State
        Write-Output ($FAResource.ResourceName + " and its status is " +  $newstate)
}

在此处输入图像描述

于 2019-04-08T05:51:06.117 回答