Skip to content

Instantly share code, notes, and snippets.

@PCfromDCSnippets
Created December 15, 2017 20:20
Show Gist options
  • Save PCfromDCSnippets/3dd6bcf0b9d925dc13beeb6329eee391 to your computer and use it in GitHub Desktop.
Save PCfromDCSnippets/3dd6bcf0b9d925dc13beeb6329eee391 to your computer and use it in GitHub Desktop.
Update Azure Local Network Gateway IP
Param (
[string]$resourceGroup = "Networking-US-East-2",
[string]$localGatewayName = "LocalGateway-HQ",
[string]$location = "East US 2",
[string]$lgwSubnetPrefix = "192.168.0.0/21"
)
function Get-LocalIP {
$wc = New-Object net.webclient
$localIP = $wc.downloadstring("http://checkip.dyndns.com") -replace "[^\d\.]"
return $localIP
}
function Get-LocalGatewayIP ($resourceGroup, $localGatewayName) {
$lng = Get-AzureRmLocalNetworkGateway -Name $localGatewayName -ResourceGroupName $resourceGroup
return $lng.GatewayIpAddress
}
function Update-LocalGateway ($resourceGroup, $localGatewayName, $localIP, $location, $addressPrefix) {
$localGateway = New-AzureRmLocalNetworkGateway -Name $localGatewayName `
-ResourceGroupName $resourceGroup `
-Location $location `
-GatewayIpAddress $localIP `
-AddressPrefix $addressPrefix `
-Force `
-Confirm:$false
Write-Output("$localGatewayName Local Gateway updated...")
}
$localIP = Get-LocalIP
$gatewayIP = Get-LocalGatewayIP -resourceGroup $resourceGroup -localGatewayName $localGatewayName
If ($gatewayIP -ne $localIP) {
Update-LocalGateway -resourceGroup $resourceGroup `
-localGatewayName $localGatewayName `
-localIP $localIP `
-location $location `
-addressPrefix $lgwSubnetPrefix
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment