Unable to delete Azure Virtual Network Gateway using Resource Manager - powershell

I created an Azure Virtual Network using PowerShell:
$gwpip= New-AzureRmPublicIpAddress -Name TestVNet -ResourceGroupName TestRg -Location 'West Europe' -AllocationMethod Dynamic -DomainNameLabel TestVNet
$vnet = Get-AzureRmVirtualNetwork -Name TestVNet -ResourceGroupName TestRg
$subnet = Get-AzureRmVirtualNetworkSubnetConfig -Name 'GatewaySubnet' -VirtualNetwork $vnet
$gwipconfig = New-AzureRmVirtualNetworkGatewayIpConfig -Name TestVNet -SubnetId $subnet.Id -PublicIpAddressId $gwpip.Id
New-AzureRmVirtualNetworkGateway -Name TestVNet -ResourceGroupName TestRg -Location 'West Europe' -IpConfigurations $gwipconfig -GatewayType Vpn -VpnType RouteBased
This worked perfectly. I now want to delete this Virtual Network Gateway, but that doesn't seem to work. When I use the delete button in the Portal I get a message: "Successfully saved configuration changes to virtual network TestVNet"
When I use PowerShell it doesn't return anything, unless I use the -debug and -verbose switch.
Remove-AzureRmVirtualNetworkGateway -Name TestVNet -ResourceGroupName TestRg -Force -Verbose -Debug
The final HTTP response says:
Body:
{
"status": "Failed",
"error": {
"code": "InternalServerError",
"message": "An error occured.",
"details": []
}
When I check the audit logs in the Portal I see two Microsoft.Network/virtualNetworkGateways/delete events, both Informational. The first has status Started, the second Accepted. After that, nothing happens anymore. Using the Portal or PowerShell doesn't make a difference in the audit log.
Any suggestions on how I can remove the gateway?

Whilst this isn't the nicest way, I've found you can do the following
Remove Azure PowerShell 1.0 or use a machine with Azure PowerShell 0.9 installed
Connect up to your Tenant
Import-Module Azure
Add-AzureAccount
Switch-AzureMode -Name AzureResourceManager
Then from here force remove the Resource Group housing everything - Be warned, this will delete everything in the resource group
Remove-AzureResourceGroup -Name TestRg -Force -Verbose
After a while, everything will be gone. Whilst it doesn't remove just the VNet, or the Gateway, or just the Connection, it will give you a blank test bed again.
Hope this helps.

Related

Switch-AzureRmWebAppSlot does not swap the physical site only the application settings

I have been running the following command via powershell for AZURE but all that gets swapped are the application settings:
Switch-AzureRmWebAppSlot -ResourceGroupName 'myresourcegroup' -Name 'mywebsitename' -SourceSlotName "staging" -DestinationSlotName "production" -confirm -verbose
The same thing happens when I run this command:
Switch-AzureRmWebAppSlot -ResourceGroupName 'myresourcegroup' -Name 'mywebsitename' -SourceSlotName "staging" -DestinationSlotName "production" -SwapWithPreviewAction CompleteSlotSwap -confirm -verbose
I cannot use Switch-AzureWebsite as I cannot set a default subscription with my permissions.
Using the Login-AzureRMAccount the only way I found to switch slots is as follows:
$ParametersObject = #{targetSlot = "production"}
$RGN = 'resource-group-name-'
Invoke-AzureRmResourceAction -ResourceGroupName $RGN -ResourceType Microsoft.Web/sites/slots -ResourceName website-name/staging -Action slotsswap -Parameters $ParametersObject -Verbose -force
This was hard to find, in part because many examples required you to set your Azure default subscription prior to executing Switch-AzureWebsite and the other switch only moved the configuration elements over. I am curious if anyone has a clever way to discover power shell commands or ARM template commands aside from the ones generated for deployments on Azure. Ideally, I could perform an action on Azure an then see same thing scripted as PowerShell.
REF: Microsoft Docs Here

How do I deploy to Azure App Service with PowerShell?

I have looked around and with the thousands of commands in the Azure and AzureRM commandlets in PowerShell, I'm still not sure how to do this.
What I have working so far:
Installed Azure and AzureRM modules and imported them to the script
Generated the "*.publishsettings" file from the get-AzurePublishSettingsFile command
Imported the "*.publishsettings" file
Can acccess the website with the "Stop-AzureWebsite" and "Start-AzureWebsite" commandlets
What I need to do:
create a new deployment and push files to the app-service site.
Notes: I do not have a Visual Studio project and .csproj file configs. I simply want to take the contents of a folder and push that to the website.
Any help would be useful as the documentation is really bad on details and there are thousands of commands in PowerShell to go through.
You could check this blog:Deploy an App Service using Azure PowerShell to a Deployment Slot.
Get-AzurePublishSettingsFile
Import-AzurePublishSettingsFile .\Your-Publish-Settings-credentials.publishsettings
Get-AzureSubscription
Select-AzureSubscription -SubscriptionName "The Subscription Name containing the slot"
Set-AzureSubscription -SubscriptionId "ID of subscription"
$WebAppName = "standard(staging)"
Get-AzureWebsite -Name $WebAppName
Publish-AzureWebsiteProject -Name $WebAppName -Package "C:\PowerShell\standard.zip" -Slot "staging"
The above link (https://blogs.msdn.microsoft.com/benjaminperkins/2016/10/01/deploy-an-app-service-using-azure-powershell-to-a-deployment-slot/)talks about a GIT based deployment. OP wanted something from a folder.
Check this one out -
Create an Azure Website with PowerShell and FTP
Unfortunately the accepted answer gave me the following error:
Get-AzureWebSite : Requested value 'PremiumV2' was not found
This StackOverflow answer suggests to use Get-AzureRmWebApp instead, but this introduces some challenges with authentication. After some searching I found the following article which explained exactly what I needed: an approach to do a publish to Azure without any human interaction.
Please see a very simplified version of the script below.
#In the Azure portal go to (search for) "Azure Active Directory" ->
#"Properties" -> Directory ID
$TenantId = "<Azure Active Directory Id>"
#In the Azure portal go to (search for) "Subscriptions" -> Subscription ID
$SubscriptionId = "<Azure Subscription Id>"
#In the Azure portal go to (search for) "Azure Active Directory" -> "App registrations" ->
#Create a new registration, this will give you the ID and Secret below.
#Make sure to give your new app registration sufficient rights to your app service
$ServicePrincipleApplicationId = "<Service Principle Id>"
$ServicePrincipleApplicationSecret = "<Service Principle Secret>"
$WebAppPath = "<Local folder where your package is located>"
$ResourceGroupName = "<The name of the Azure resource group that contains your app service>"
$WebAppName = "<The name of your Azure app service>"
$WebAppSlot = "<The name of the deployment slot you want to publish to>"
$MSDeployPath = "C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe"
$source = "-source:contentPath=$WebAppPath"
$publishProfileOutputPath = Join-Path -Path $ENV:Temp -ChildPath 'publishprofile.xml'
$dest = "-dest:contentPath=d:\home\site\wwwroot\,publishSettings=$publishProfileOutputPath"
$SecurePassword = $ServicePrincipleApplicationSecret | ConvertTo-SecureString -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $ServicePrincipleApplicationId, $securePassword
$connectParameters = #{
Credential = $Credential
TenantId = $TenantId
SubscriptionId = $SubscriptionId
}
Add-AzureRmAccount #connectParameters -ServicePrincipal
Get-AzureRmWebAppSlotPublishingProfile -OutputFile $publishProfileOutputPath -Format WebDeploy -ResourceGroupName $ResourceGroupName -Name $WebAppName -Slot $WebAppSlot
Stop-AzureRmWebAppSlot -ResourceGroupName $ResourceGroupName -Name $WebAppName -Slot $WebAppSlot
& $MSDeployPath #('-verb:sync', $source, $dest)
Start-AzureRmWebAppSlot -ResourceGroupName $ResourceGroupName -Name $WebAppName -Slot $WebAppSlot
To deploy your zip package to Azure Web App Service using PowerShell cmdlet.
Refer MS Docs.
Connect to Azure Subscription via PowerShell. Execute Publish-AzWebApp to deploy Web App.
$webAppName = "<NameOfWebAppService>"
$resourceGroup = "<WebAppResourceGroupName>"
$zipArchiveFullPath = "<zip-package-filePath\FileName.zip>"
Publish-AzWebApp -ResourceGroupName "$resourceGroup" -Name "$webAppName" -ArchivePath "$($zipArchiveFullPath)" -Force

Remove Custom Domain/Host from App Service via Powershell

I am unable to find a way to remove a host name from a azure web app/app service.
I have tried to use the following filtering our unwanted hosts, but nothing is removed.
Set-AzureWebsite -Name "<<name>>" -HostNames $hosts
and
Set-AzureRmWebApp -Name "<<name>>" -ResourceGroupName "<<name>>" -HostNames $hosts
I have around 200 hosts to delete, however, I can't seem to find an automated way of doing it.
at a top level this is what you need to do:
Get the websites resource
Manipulate the hostnames collection
Post the changes back to azure
Here is an example of how I did it:
$webApp = Get-AzureRmWebApp -ResourceGroupName "<<Resource-Group-Name>>" -Name "<<App_Name>>"
$webApp.HostNames.Clear()
$webApp.Hostnames.Add($webApp.DefaultHostName)
set-AzureRmWebApp -ResourceGroupName "<<Resource-Group-Name>>" -Name <<App_Name>> -HostNames $webApp.HostNames
This will remove all custom hostnames and leave only the default one.
If you want to remove a specific hostname for the collection you could use:
$webApp.HostNames.Remove("your_hostname_goes_here")
NOTE
If your hostname has SSL bindings you will need to remove those first and then delete the hostname.
Just an update
AzureRm has now been replaced with Az so the statement would now be
$webApp = Get-AzWebApp -ResourceGroupName "<Resource-Group-Name>" -Name "<App-Name>";
$webApp.HostNames.Clear();
$webApp.Hostnames.Add($webApp.DefaultHostName);
Set-AzWebApp `
-ResourceGroupName "<Resource-Group-Name>" `
-Name <App-Name> `
-HostNames $webApp.HostNames;

AzureRM Automation PSWorkflow Runbook - error while attaching NIC to subnet

I'm trying to create a parallel Workflow script but facing some challenges while attaching NIC to existing Subnet. I'm getting the following error.
Microsoft.PowerShell.Utility\Write-Error : Cannot validate argument on
parameter 'SubnetId'. The argument is null or empty. Provide an
argument that is not null or empty, and then try the command again.
Following is my Workflow script which creates VNet, Subnet, NSG, PIP and tries to create NIC from AzureRM automation Runbook.
"Get Virtual Network Information" $gvnet = Get-AzureRmVirtualNetwork
-Name $VNetName -ResourceGroupName $SharedResourcesRGName
$nic = New-AzureRmNetworkInterface -Name "$VMName-NIC"
-ResourceGroupName $VMName -Location $VMLocation `
        -SubnetId $gvnet.Subnets[0].ID -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $gnsg.Id -Force
How do I get the value of Subnet in AzureRM Workflow runbook
Gulab Pasha

Azure Microsoft.Azure.Diagnostics Extension

Trying to configure the BootDiagnostics on a VM on a storage account in a resource group different from the VMs. To the best of my knowledge this can only be done by installing the VmDiagnosticsExtension. I'm executing the following:
Set-AzureRmVMDiagnosticsExtension -ResourceGroupName $RgName -VMName $VmName `
-DiagnosticsConfigurationPath 'D:\DiagnosticsConfig.xml' `
-StorageAccountEndpoint '/subscriptions/ff26f7/resourceGroups/rgroup/providers/Microsoft.Storage/storageAccounts/amsdiag01'`
-StorageAccountName 'amsdiag01' `
-StorageAccountKey 'key''>
I get this error message:
ErrorMessage: Handler 'Microsoft.Azure.Diagnostics.IaaSDiagnostics'
has reported failure for VM Extension
'Microsoft.Insights.VMDiagnosticsSettings' with terminal error code
'1009' and error message: 'Enable failed for plugin (name:
Microsoft.Azure.Diagnostics.IaaSDiagnostics, version 1.6.3.0) with
exception Command
C:\Packages\Plugins\Microsoft.Azure.Diagnostics.IaaSDiagnostics\1.6.3.0\DiagnosticsPluginLauncher.exe
of Microsoft.Azure.Diagnostics.IaaSDiagnostics has exited with Exit
code: -106'
No further information available in C:\WindowsAzure\Logs\WaAppAgent.log on server. In the portal under 'Diagnostics' I can see the correct configuration. But the extension is in a '(unavailable)' state. Container in storage account is not created. Also, can't reboot the server due to the extension. I can remove and re-add the extension using PowerShell, but always same result. When performing a Get-AzureRmVMExtension I see the following 'Public Settings' entry:
PublicSettings : {
"storageAccount": "amsdiag01",
"xmlCfg": "PFdhZENmZz4NCiAgICAgIDxEaWFnbm9zdGljTW9uaXRvckNvbmZpZ3VyYXRpb24gb3ZlcmFsbFF1b3RhSW5NQj0iNDA5NiI+DQogICAgICAgIDxQZXJmb3JtYW5jZUNvdW50ZXJzIHNjaGVkdWxlZFRyYW5zZmVyUGVyaW9kPSJQVDFNIj4NCsKgwqDCoMKgwqAgICAgIDxQZXJmb3JtYW5jZUNvdW50ZXJDb25maWd1cmF0aW9uIGNvdW> eU5hbWU9IkNQVSBwcml2aWxlZ2VkIHRpbWUiIGxvY2FsZT0i
I've chosen not to use extensions but instead using the following:
$RgName = 'RG1'
$VmName = 'VM01'
$Vm = Get-AzureRmVM -ResourceGroupName $RgName -Name $VmName
$Vm = Set-AzureRmVMBootDiagnostics -Enable -VM $Vm -ResourceGroupName $RgName -StorageAccountName diagss01
Update-AzureRmVM -ResourceGroupName $RgName -VM $Vm