如何在AzureRM和Application Gateway中使用Set-命令?

时间:2018-10-23 11:16:36

标签: azure powershell

我试图运行以下命令来更改我的Azure应用程序网关中现有规则上的设置:

    $updatedAppGW = Set-AzureRmApplicationGatewayRequestRoutingRule -ApplicationGateway $AppGW `
        -Name $ChosenSubscription.httpsRule `
        -RuleType Basic `
        -BackendAddressPool $backendPool `
        -BackendHttpSettings $httpSettings

    # Save Gateway configuration
    Write-Host "[$(__LINE__)]  Attempting to save changes to the Application Gateway..." -ForegroundColor Cyan
    Set-AzureRmApplicationGateway -ApplicationGateway $updatedAppGW | Out-Null

Set-AzureRmApplicationGatewayRequestRoutingRule命令似乎正常运行(至少是静默运行)。

但是,当我随后尝试使用命令Set-AzureRmApplicationGateway“保存”应用程序网关配置时,收到错误Set-AzureRmApplicationGateway : Object reference not set to an instance of an object.

Console output

我认为这是因为我没有正确使用这些“设置”命令。

我在线阅读到,当我运行Set-AzureRmApplicationGatewayRequestRoutingRule时,我实际上只是在更改本地内存中的规则。然后,我必须保存应用程序网关的更改。

这是真的吗?如果是这样,我该如何在此上下文中保存应用程序网关配置?在我的脚本的前面,当使用Add-AzureRm命令(例如Add-AzureRmApplicationGatewayBackendAddressPool)时,我立即(下一行)运行Set-AzureRmApplicationGateway,它已按预期工作。

我还尝试过更改本文顶部代码块中的Set-AzureRmApplicationGateway命令,以使用我原来的$AppGW变量代替我认为的此$updatedAppGW变量我的Set-AzureRmApplicationGatewayRequestRoutingRule命令正在生成。都不起作用-相同的错误。

编辑:附加诊断

添加以下Write-Host输出...

    Write-Host "[$(__LINE__)]  Retrieved AG Rule '$($rule.Name)'." -ForegroundColor Magenta
    Write-Host "[$(__LINE__)]  Attempting to change this rule to point at Backend Address Pool '$($backendPool.Name)' and HTTP Settings '$($httpSettings.Name)'..." -ForegroundColor Cyan

    # Re-retrieve the Application Gateway after saving it earlier
    $AppGW = Get-AzureRmApplicationGateway -Name $ChosenSubscription.appGateway -ResourceGroupName $ChosenSubscription.resourceGroup

    # Re-retrieve the Backend Address Pool and HTTP Settings that we've created, for the sake of updating the rule
    $backendPool = Get-AzureRmApplicationGatewayBackendAddressPool -ApplicationGateway $AppGW -Name $MaintenanceToggleBackendPool
    $httpSettings = Get-AzureRmApplicationGatewayBackendHttpSettings -ApplicationGateway $AppGW -Name $MaintenanceToggleHTTPSetting

    Write-Host "[$(__LINE__)]  `$AppGW.Name $($AppGW.Name)" -ForegroundColor Green
    Write-Host "[$(__LINE__)]  `$AppGW.ProvisioningState $($AppGW.ProvisioningState)" -ForegroundColor Green
    Write-Host "[$(__LINE__)]  `$AppGW.OperationalState $($AppGW.OperationalState)" -ForegroundColor Green

    $updatedAppGW = Set-AzureRmApplicationGatewayRequestRoutingRule -ApplicationGateway $AppGW `
        -Name $ChosenSubscription.httpsRule `
        -RuleType Basic `
        -BackendAddressPool $backendPool `
        -BackendHttpSettings $httpSettings

    Write-Host "[$(__LINE__)]  `$updatedAppGW.Name $($updatedAppGW.Name)" -ForegroundColor Green
    Write-Host "[$(__LINE__)]  `$updatedAppGW.ProvisioningState $($updatedAppGW.ProvisioningState)" -ForegroundColor Green
    Write-Host "[$(__LINE__)]  `$updatedAppGW.OperationalState $($updatedAppGW.OperationalState)" -ForegroundColor Green

    # Save Gateway configuration
    Write-Host "[$(__LINE__)]  Attempting to save changes to the Application Gateway..." -ForegroundColor Cyan
    Set-AzureRmApplicationGateway -ApplicationGateway $updatedAppGW | Out-Null

...提供以下控制台输出:

Console output for further diagnosis

1 个答案:

答案 0 :(得分:1)

好的,我自己解决了问题... 叹气

Set-AzureRmApplicationGatewayRequestRoutingRule命令上,您必须指定-HttpListener参数,否则它将以静默方式失败。

    # Re-retrieve the Backend Address Pool and HTTP Settings that we've created, for the sake of updating the rule
    $backendPool = Get-AzureRmApplicationGatewayBackendAddressPool -ApplicationGateway $AppGW -Name $MaintenanceToggleBackendPool
    $httpSettings = Get-AzureRmApplicationGatewayBackendHttpSettings -ApplicationGateway $AppGW -Name $MaintenanceToggleHTTPSetting
    $httpListener = Get-AzureRmApplicationGatewayHttpListener -ApplicationGateway $AppGW -Name "HttpListenerTest"

    $updatedAppGW = Set-AzureRmApplicationGatewayRequestRoutingRule -ApplicationGateway $AppGW `
        -Name $ChosenSubscription.httpsRule `
        -RuleType Basic `
        -BackendAddressPool $backendPool `
        -BackendHttpSettings $httpSettings `
        -HttpListener $httpListener

这就是Set-AzureRmApplicationGateway命令无法正常工作的原因-它的内存中包含错误的RequestRoutingRule