关于Azure应用网关外部站点重定向配置的PowerShell脚本咨询
Analysis & Improvements for Your Azure Application Gateway Redirect Script
First, let's break down the gaps in your current script and walk through a fully functional, updated version that works reliably.
Key Correctness Issues in Your Current Script
- Outdated Azure Module: You’re using the deprecated
AzureRmcmdlets. Microsoft has fully moved to theAzmodule for all Azure PowerShell operations, andAzureRmno longer receives updates or support—this will cause errors in newer Azure environments. - Incomplete Redirect Configuration: Your
Add-AzureRmApplicationGatewayRedirectConfigurationline is truncated. It’s missing mandatory parameters like-ApplicationGateway,-RedirectType, and-TargetUrl—without these, the command can’t create a valid redirect setup. - Unlinked Redirect to Rule: You specified an existing rule name, but there’s no step to connect the new redirect configuration to this rule. The rule won’t actually use the redirect until you explicitly associate them.
- Typo in Listener Variable:
$listnerNamehas a typo (missing an "e"—should be$listenerName). This will break the script when it tries to reference your listener.
Fully Improved & Functional Script
Here’s a polished version using the modern Az module, with all missing steps and best practices included:
# Configuration Variables - Update these to match your environment $AppgwName = "appgwname" $rgName = "rgname" $ExternalSite = "http://www.google.com" $RedirectConfigurationName = "ExternalSiteRedirect" $ListenerName = "your-listener-name" # Fixed typo here $ExistingRuleName = "your-existing-rule-name" # 1. Retrieve the target Application Gateway object $gw = Get-AzApplicationGateway -Name $AppgwName -ResourceGroupName $rgName # 2. Add the redirect configuration to the gateway $gw = Add-AzApplicationGatewayRedirectConfiguration -ApplicationGateway $gw ` -Name $RedirectConfigurationName ` -RedirectType Found ` # Use "Permanent" for 301 (SEO-friendly) or "Found" for 302 (temporary) redirects -TargetUrl $ExternalSite ` -IncludePath $false ` # Set to $true if you want to pass the original request path to the external site -IncludeQueryString $false # Set to $true if you want to forward query strings # 3. Link the redirect configuration to your existing routing rule $existingRule = Get-AzApplicationGatewayRequestRoutingRule -ApplicationGateway $gw -Name $ExistingRuleName $gw = Set-AzApplicationGatewayRequestRoutingRule -ApplicationGateway $gw ` -Name $ExistingRuleName ` -RuleType Basic ` # Use "PathBasedRouting" if your rule uses path maps -Listener $existingRule.Listener ` -RedirectConfiguration $gw.RedirectConfigurations[$RedirectConfigurationName] # 4. Commit all changes to the Application Gateway (critical step!) Set-AzApplicationGateway -ApplicationGateway $gw
Additional Production-Grade Tips
- Redirect Type: Pick
Permanent(301) for permanent URL changes (helps with SEO) orFound(302) for temporary redirects. - HTTPS Compatibility: If your listener uses HTTPS, ensure the external site URL uses
https://to maintain secure connections. - Error Handling: For production scripts, add
try/catchblocks to catch issues like missing gateways, listeners, or rules, and include verbose logging for debugging. - Path/Query Control: Adjust
-IncludePathand-IncludeQueryStringbased on whether you want to pass the original request’s path or query parameters to the external site.
内容的提问来源于stack exchange,提问作者Eldos
相关产品推荐
相关产品推荐

