Error adding Alert Rule to Azure Cloud Service - powershell

I've created a script to add alert rules to several Azure resources (web apps, SQL databases, and cloud services). Everything works except for creating the cloud service alerts which returns an error:
Add-AlertRule : ResourceProviderNotSupported: The resource provider
'Microsoft.ClassicCompute' is not supported.
This is the script I'm using to add the rule:
$cloudServices = Get-AzureResource -ResourceType "Microsoft.ClassicCompute/domainNames" -ResourceGroupName $resourceGroup
Foreach ($cloudService in $cloudServices)
{
# Cloud Service - CPU Percentage
Add-AlertRule `
-RuleType Metric `
-Name "CPU Percentage (Cloud Service)" `
-Location $cloudService.Location `
-ResourceGroup $cloudService.ResourceGroupName `
-Operator GreaterThan `
-Threshold 75 `
-WindowSize 01:00:00 `
-ResourceId $cloudService.ResourceId `
-MetricName "Percentage CPU" `
-TimeAggregationOperator Average `
-SendToServiceOwners
}
I've tried other using a different ResourceType parameter to target the role instead of the cloud service, but that doesn't work either.
Has anyone had any experience scripting these cloud service alerts successfully?

Hi Garrett sometime back I had created same kind of stuff without any issues. Please have a look on this link-
http://blogs.technet.com/b/keithmayer/archive/2014/11/08/scripts-to-tools-automate-monitoring-alert-rules-in-microsoft-azure-with-powershell-and-the-azure-service-management-rest-api.aspx

Related

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;

Unable to set SKU using Set-AzureRmResource

I am using Azure PowerShell and using Azure Resource Manager commandlets to set SKU to "Standard". The following examples isn't working
$azsite = Get-AzureRmResource -ResourceType "Microsoft.Web/Sites" -ResourceName "azwebapp" -ResourceGroupName "DefaultResourceGroupName"
$azsite.properties.sku = "standard"
$az | set-azurermresource -Force
The resource still has the free tier.
I also tried.
Set-AzureRmResource -ResourceType "Microsoft.Web/Sites" -ResourceName "azwebapp" -ResourceGroupName "DefaultResourceGroupName" -Sku #{"name" = "standard"} -Force
And still it doesn't change the resource sku to standard.
The best thing is to use the dedicated app service plan cmdlets more over the sku is an app service plan property, you can use the following example
Set-AzureRmAppServicePlan -ResourceGroupName DefaultResourceGroupName -Name <AppServicePlanName> -Tier Standard

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.

Update Azure Website instance size (Small, Medium, Large) with Powershell

Is there a way to scale an Azure WebSite instance size (not instance count) using PowerShell? I haven't found the way.
I know it can be done using Azure CLI or from the Portal, but I want to be able to do it with PowerShell. Is this posible?
I want to update an existing WebSite instance size, not create a new website.
Yes, you first need to know the name of the Resource Group and Web Hosting Plan to which your website belongs, which you can get from the new Azure Portal. Then you can change the worker size to 0 (Small), 1 (Medium) or 2 (Large).
Switch-AzureMode AzureResourceManager
$resourceGroup = "MyResourceGroup"
$webHostingPlan = "MyWebHostingPlan"
$whp = Get-AzureResource `
-Name $webHostingPlan `
-ResourceGroupName $resourceGroup `
-ResourceType "Microsoft.Web/serverFarms" `
-ApiVersion 2014-04-01
$newWorkerSize = 1
$whp.Properties.workerSize = $newWorkerSize
$whp.Properties.workerSizeId = $newWorkerSize
Set-AzureResource `
-Name $webHostingPlan `
-ResourceGroupName $resourceGroup `
-ResourceType "Microsoft.Web/serverFarms" `
-ApiVersion 2014-04-01 `
-PropertyObject $whp.Properties

How to script scaling a Website from Small to Large? [duplicate]

Is there a way to scale an Azure WebSite instance size (not instance count) using PowerShell? I haven't found the way.
I know it can be done using Azure CLI or from the Portal, but I want to be able to do it with PowerShell. Is this posible?
I want to update an existing WebSite instance size, not create a new website.
Yes, you first need to know the name of the Resource Group and Web Hosting Plan to which your website belongs, which you can get from the new Azure Portal. Then you can change the worker size to 0 (Small), 1 (Medium) or 2 (Large).
Switch-AzureMode AzureResourceManager
$resourceGroup = "MyResourceGroup"
$webHostingPlan = "MyWebHostingPlan"
$whp = Get-AzureResource `
-Name $webHostingPlan `
-ResourceGroupName $resourceGroup `
-ResourceType "Microsoft.Web/serverFarms" `
-ApiVersion 2014-04-01
$newWorkerSize = 1
$whp.Properties.workerSize = $newWorkerSize
$whp.Properties.workerSizeId = $newWorkerSize
Set-AzureResource `
-Name $webHostingPlan `
-ResourceGroupName $resourceGroup `
-ResourceType "Microsoft.Web/serverFarms" `
-ApiVersion 2014-04-01 `
-PropertyObject $whp.Properties