Skip to content

Weird behavior - Inline Azure PowerShell #16

@bertrandpons

Description

@bertrandpons

Assume I have the following script:

####################################################
########      CONFIGURATION PARAMETERS     #########
####################################################
# Web App Resource Group Name
$webAppRgName = "$($env:RG_NAME)";
# Web App Name
$webAppName = "$($env:WEBAPP_NAME)";
# SQL Server Resource Group Name
$sqlRgName = "$($env:RG_NAME)";
# SQL Server Name
$sqlServerName = "$($env:SQL_SERVER_NAME)";

# Define methods used for logging
Function LogSection ($message) {
    Write-Host ""
    Write-Host "** $($message) **" -ForegroundColor "magenta" #-BackgroundColor "blue"
}
Function LogWaiting ($message) {
    Write-Host "$($message)... " -NoNewline -ForegroundColor "white" -BackgroundColor "blue"
}
Function LogInfo ($message) {
    Write-Host $message #-ForegroundColor "white" -BackgroundColor "blue"
}
Function LogWarning ($message) { 
    Write-Host $message -ForegroundColor "yellow" #-BackgroundColor "blue" 
}
Function LogError ($message) { 
    Write-Host $message -ForegroundColor "red" #-BackgroundColor "blue" 
}
Function LogSuccess ($message) { 
    Write-Host $message -ForegroundColor "green" #-BackgroundColor "blue" 
}
Function LogDuration ($startDate) {
    $duration = (Get-Date) - $startDate
    LogInfo "Script duration: $duration `n`n"
}



$cmdName = "Add-AzureRmAccount"
if (Get-Command $cmdName -errorAction SilentlyContinue)
{
	try {
		LogSection "Add SQL Firewall Rules from Web App outbound ips"
		$startDate = (Get-Date)
		LogInfo ("Script started: " + $startDate.ToString())

		# Get Web App OutboundIpAddresses
		LogWaiting "Get Web App OutboundIpAddresses"
		$webapp = Get-AzureRmWebApp -ResourceGroupName $webAppRgName -Name $webAppName -errorAction SilentlyContinue
		if ($webapp){
			LogSuccess "$webAppName was found."
			# Extract Outbound IPs
			$ips = $webapp.PossibleOutboundIpAddresses
                        Write-Host $ips
			if ($ips){
				$ipsArr = $ips -split ','
				$ipsArr | ForEach {
					$index = $ipsArr.IndexOf($_)
					$ruleName = $webapp.SiteName + "-" + $index
					$startIp = $_
					$endIp = $_
					Write-Host "Adding IP $index to SQL Server rule: $ruleName , StartIP: $startIp , EndIP $endIp"

					$fr = Get-AzureRmSqlServerFirewallRule -ResourceGroupName $sqlRgName -ServerName $sqlServerName -FirewallRuleName $ruleName -errorAction SilentlyContinue
					if ($fr){
						# No changes on the IPs
						if (($fr.StartIpAddress -eq $startIp) -and ($fr.EndIpAddress -eq $endIp)){
							LogSuccess "$ruleName was already created and keep the same IPs ($startIp - $endIp)"
						} else {
							Set-AzureRmSqlServerFirewallRule -ResourceGroupName $sqlRgName -ServerName $sqlServerName -FirewallRuleName $ruleName -StartIpAddress $startIp -EndIpAddress $endIp
							LogSuccess "$ruleName was already created but IPs has been updated to ($startIp - $endIp)"
						}
					}
					else{
						New-AzureRmSqlServerFirewallRule -ResourceGroupName $sqlRgName -ServerName $sqlServerName -FirewallRuleName $ruleName -StartIpAddress $startIp -EndIpAddress $endIp
						LogSuccess "done"
					}
				}
			}
			else{
				Write-Host "No Outbound IPs founded for $webAppName " -ForegroundColor Red
			}
		}
		LogDuration $startDate
	}
	catch
	{
		Write-Host "Errors found:`n$_" -ForegroundColor Red
	}
}
else{
	LogError "$cmdName doesn't exists. Please install latest version of Azure Powershell 1.0: http://aka.ms/webpi-azps"
}

If I execute this script from my laptop, it works fine. When I run it with the task Inline Azure PowerShell from my Release Definition, it doesn't work, as it is not able to retrieve the Outbound IPs as requested in the script: No Outbound IPs founded

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions