Skip to content

Instantly share code, notes, and snippets.

@manualbashing
Created April 4, 2023 10:41
Show Gist options
  • Save manualbashing/23325bde3ccacbce50c0540044adbdd7 to your computer and use it in GitHub Desktop.
Save manualbashing/23325bde3ccacbce50c0540044adbdd7 to your computer and use it in GitHub Desktop.
function Set-PublicIpAzSqlFirewallRule {
[CmdletBinding()]
param (
# Name of the SQL server
[Parameter(Mandatory)]
[string]
$ServerName,
# Name of the SQL server's resource group
[Parameter(Mandatory)]
[string]
$ResourceGroupName,
# Name of the fireall rule that will be updated
[Parameter()]
[string]
$RuleName = 'MyCurrentPublicIp'
)
Write-Verbose "Getting current public IP"
$publicIp = Invoke-RestMethod -Method GET -Uri 'https://api.ipify.org'
Write-Verbose "Removing existing firewall rule"
$null = Get-AzSqlServerFirewallRule -ServerName $ServerName `
-ResourceGroupName $ResourceGroupName |
Where-Object FirewallRuleName -eq $RuleName |
Remove-AzSqlServerFirewallRule
Write-Verbose "Adding current public IP $publicIp to allowed IP range"
New-AzSqlServerFirewallRule -ServerName $ServerName `
-ResourceGroupName $ResourceGroupName `
-FirewallRuleName $RuleName `
-StartIpAddress $publicIp `
-EndIpAddress $publicIp
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment