Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Param(
[Parameter(Mandatory=$true)]
[string]$subnetResourceId, ## Something along the lines of '/subscriptions/[subscriptionid]/resourceGroups/[resourceGroupName]/providers/Microsoft.Network/virtualNetworks/[vnetname]/subnets/[subnetname]'
[Parameter(Mandatory=$true)]
[string]$webappName
)
function Get-AccessToken {
$context = Get-AzureRmContext
$tokenCache = $context.TokenCache
$cachedTokens = $tokenCache.ReadItems() `
| Sort-Object -Property ExpiresOn -Descending
$accessToken = $cachedTokens[0].AccessToken
$accessToken
}
function Set-VNetIntegration {
$app = Get-AzureRMWebApp -Name $webappName
$resourceGroup = $app.ResourceGroup
$location = $app.Location
$body = "{
""location"": ""$location"",
""properties"": {
""subnetResourceId"": ""$subnetResourceId""
}
}"
$url = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroup/providers/Microsoft.Web/sites/$webappName/config/virtualNetwork?api-version=2018-02-01"
$accessToken = Get-AccessToken
Invoke-RestMethod -Uri $url -Method PUT -Headers @{Authorization = "Bearer $accessToken"} -ContentType 'application/json' -Body $body
}
$subscriptionId = (Get-AzureRMContext).Subscription.Id
if ($subscriptionId -eq $null) {
throw "Not logged in. Please login using Connect-AzureRmAccount and select the correct subscription using Select-AzureRmSubscription; then try again"
}
Set-VNetIntegration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.