Remove Custom Domain/Host from App Service via Powershell - 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;

Related

Run cosmosdb commands from powershell

I am trying to connect to azure cosmosdb from my local machine via powershell but every command I tried to run it returns the "Argument passed in is not serializable."
Here are a few of my commands,
Get-AzCosmosDBAccount -ResourceGroupName "cosmosbackup"
Invoke-AzCosmosDBSqlDatabaseThroughputMigration -ResourceGroupName "cosmosbackup" -AccountName "liabilitydata" -Name liability
New-AzCosmosDBSqlContainer -AccountName "liabilitydata"-DatabaseName "dailyliability"-ResourceGroupName "cosmosbackup"-Name schemes -PartitionKeyPath /Id -PartitionKeyKind Hash
Get-AzCosmosDBSqlContainer `
-ResourceGroupName "cosmosbackup" `
-AccountName "liabilitydata" `
-DatabaseName "dailyliability"
All of them fail for the same reason Argument passed in is not serializable.
Am I missing something? Please help
The issue here is that you need to set the context for running the script,
Step 1 : Connect with your Azure account
Connect-AzAccount
Step 2 : Pass the resource group and the cosmosdb account name as follows,
Get-AzCosmosDBAccount -ResourceGroupName cosmosbackup

Creating OS disk from VHD stored in another storage account

Have an existing PowerShell script that is creating VM's using pre-created VHD's stored in a storage account (copied across regional storage accounts for speed).
In PS we can use the following:
New-AzureRmDisk -DiskName $osDiskName -Disk `
(New-AzureRmDiskConfig -AccountType Premium_LRS `
-Location $location -CreateOption Import `
-StorageAccountId $storageAccountId `
-SourceUri $osVHDUri) `
-ResourceGroupName $resourceGroupName
$osDisk = Get-AzureRMDisk -DiskName $osDiskName -ResourceGroupName $resourceGroupName
$VirtualMachine = Set-AzureRmVMOSDisk -VM $VirtualMachine -ManagedDiskId $osDisk.Id -CreateOption Attach -Windows -StorageAccountType Premium_LRS
Where $storageAccountId is similar to:
/subscriptions/{0}/resourceGroups/my-snapshot/providers/Microsoft.Storage/storageAccounts/mysnapshots -f $sourceSnapshotSubscriptionId
In the .net Azure SDK, I don't see a way to replicate this? When I try to create, it's saying unable to find, yet my PS works ok.
If you want to use an existing disk instead of a marketplace image, use this code:
var managedDisk = azure.Disks.Define("myosdisk")
.WithRegion(location)
.WithExistingResourceGroup(groupName)
.WithWindowsFromVhd("https://mystorage.blob.core.windows.net/vhds/myosdisk.vhd")
.WithSizeInGB(128)
.WithSku(DiskSkuTypes.PremiumLRS)
.Create();
azure.VirtualMachines.Define("myVM")
.WithRegion(location)
.WithExistingResourceGroup(groupName)
.WithExistingPrimaryNetworkInterface(networkInterface)
.WithSpecializedOSDisk(managedDisk, OperatingSystemTypes.Windows)
.WithExistingAvailabilitySet(availabilitySet)
.WithSize(VirtualMachineSizeTypes.StandardDS1)
.Create();
Check this link for further reference. Hope it helps.

List instances of Azure App Service with Powershell RM

My aim is to find all the instance Ids of a particular App Service in Azure so I can write a warm-up routine and test it against all running instances (ARRAfinity).
I can do the following with ASM Powershell but I need it in ARM (RM) as Octopus is configured for it.
(Get-AzureWebsite -Name "site-name" -Slot "Production").Instances
I have found the documentation around RM sparing, and the following hasn't led me to anything helpful:
Get-AzureRmWebApp -Name "site-name"
Any help would be really helpful.
Try something like this:
Get-AzureRmResource -ResourceGroupName $ResourceGroupName -ResourceType Microsoft.Web/sites/instances -Name $WebAppName -ApiVersion 2016-03-01
See also here for a helper function that also works on slots.
Or you can use:
Get-AzWebApp -ResourceGroupName $ResourceGroupName -Name $WebAppName
Resource cmdlets should run faster than Get-AzResource (Get-AzureRmResource), specially with heavy scripts.

Azure Resource Manager Network Security Group Configuration using Powershell

I need to disable some rules configured under network security groups with RDP and SSH port open. I am facing some issues with removing the rule configuration :
This is the command I execute :
Get-AzureRmNetworkSecurityGroup -Name $securityGroupName -ResourceGroupName $resourceGroupName | Remove-AzureRmNetworkSecurityRuleConfig -Name $enabledSecurityRDPRule.Name
However when I check the portal or execute the get cmdlet I don't see the earlier command took effect.
I even tried with Set-AzureRmNetworkSecurityRuleConfig to set the access to deny and got the same result.
The service principal that I use to access my environment has contributor privileges.
The Remove-AzureRmNetworkSecurityRuleConfig command just removes the rule from your local NSG object. In order to sync the cloud side, you need to run the Set-AzureRmNetworkSecurityGroup.
Here is a complete script.
$nsg = Get-AzureRmNetworkSecurityGroup -Name <your nsg name> `
-ResourceGroupName <your resource group name>
$nsg = Remove-AzureRmNetworkSecurityRuleConfig -Name <your rule name> `
-NetworkSecurityGroup $nsg
Set-AzureRmNetworkSecurityGroup -NetworkSecurityGroup $nsg

Unable to delete Azure Virtual Network Gateway using Resource Manager

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.