Created
December 15, 2017 21:21
-
-
Save PCfromDC/a8ccb989b331023be7ba073b23f469c6 to your computer and use it in GitHub Desktop.
Update Azure S2S Gateway IP Address
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Param ( | |
[string]$userName = "svc_network-updater@yourDomain.onmicrosoft.com", | |
[string]$password = "MyPassword#12345", | |
[string]$subscriptionName = "mySubscription", | |
[string]$resourceGroup = "Networking-US-East-2", | |
[string]$localGatewayName = "LocalGateway-HQ", | |
[string]$location = "East US 2", | |
[string]$lgwSubnetPrefix = "192.168.0.0/21" | |
) | |
#region Login to Azure | |
$securePassword = ConvertTo-SecureString -String $password -AsPlainText -Force | |
$cred = New-Object System.Management.Automation.PSCredential($userName, $securePassword) | |
Login-AzureRmAccount -Credential $cred -SubscriptionName $subscriptionName | |
$subscription = (Get-AzureRmSubscription | Where-Object {$_.SubscriptionName -eq $subscriptionName}).SubscriptionId | |
Select-AzureRmSubscription -Subscriptionid $subscription | |
#endregion | |
function Update-Logs ($content) { | |
$logPath = 'C:\S2S Logs' | |
if (-not (Test-Path -Path $logPath)) {New-Item -Path $logPath -ItemType Directory} | |
$date = Get-Date | |
$lastMonth = $date.AddMonths(-1) | |
$fileName = $date.ToString("yyyy-MM-dd") + "- S2S Log.txt" | |
$filePath = ($logPath + "\" + $fileName) | |
$exists = Test-Path $filePath | |
if ($exists) { | |
$string = (Get-Date).ToShortTimeString().ToString() + " $content" | |
$string | Out-File -FilePath $filePath -Append | |
} | |
if (-not $exists) { | |
$string | Out-File -FilePath $filePath | |
} | |
# Clean Up Logs Older than 1 month | |
$items = Get-ChildItem -Path $logPath -Recurse -Filter *.txt | Where-Object {$_.CreationTime.Date -lt $lastMonth} | |
$items | Remove-Item -Force | |
} | |
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...") | |
} | |
function Connect-LocalGateway { | |
$connections = (Get-VpnS2SInterface).Name | |
foreach ($connection in $connections) { | |
Connect-VpnS2SInterface -Name $connection | |
} | |
Update-Logs -content ((Get-VpnS2SInterface).Name.toString() + "-" + (Get-VpnS2SInterface).ConnectionState.toString()) | |
} | |
#region Execute | |
# Get Local IP Address Endpoint | |
$localIP = Get-LocalIP | |
# Get Azure Local Gateway IP Address | |
$gatewayIP = Get-LocalGatewayIP -resourceGroup $resourceGroup -localGatewayName $localGatewayName | |
# Update Log File | |
Update-Logs -content ("Gateway IP = $gatewayIP and Local IP = $localIP") | |
# If IP Addresses don't match update Azure Local Gateway IP Address | |
If ($gatewayIP -ne $localIP) { | |
Update-LocalGateway -resourceGroup $resourceGroup ` | |
-localGatewayName $localGatewayName ` | |
-localIP $localIP ` | |
-location $location ` | |
-addressPrefix $lgwSubnetPrefix | |
Update-Logs -content ("Azure Local Gateway Updated") | |
} | |
# Make sure all RRAS connections are connected. | |
Connect-LocalGateway | |
#endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment