Where do I get the ResourceGroup for AzureResourceManager CmdLets? - azure

The new and soon to supercede AzureRsourceManager cmdlets all require a -ResourceGroupName parameter. Where am I expected to get this? Not including the UI, since that'd mostly defeat the purpose of automating.
Currently, I'm calling Get-AzureResourceGroup and trying each one until I get a hit. This is extremely slow - on the order of minutes. For example, to get WebApp information, I'm running this,
$groups = Get-AzureResourceGroup
foreach ($g in $groups)
{
if ($ResourceGroup) { $ResourceGroup.Value = $g.ResourceGroupName }
$app = Get-AzureWebApp -Name $Name -ResourceGroupName $g.ResourceGroupName -ErrorAction SilentlyContinue
Write-Verbose "Checking $($g.ResourceGroupName)"
if ($app)
{
$ResourceGroups[$g.ResourceGroupName] = #($ResourceGroups[$g.ResourceGroupName]) + $Name
return $app
}
}

If you go to the portal and navigate to your web app you can easily see which resource group it's in. See the screenshot below. What resource group is, is an arbitrary name to group resources. For example below you can see that it is grouping two Web Apps and two App Service Plans.

You can use the Get-AzureRmResourceGroup PowerShell command from Azure PowerShell without any parameter to get all the resource groups in very fast response time.
Reference Link: Get-AzureRMResourceGroup description
Note: You need to update your Azure PowerShell to the latest version for this new feature. The current version is 1.0.1 (November 2015 Update)

As #theadriangreen noted in his comment, it's faster to directly query the REST API. I found that some of the RM cmdlets actually do this anyway.
I've written a set of PowerShell functions to make using the API easier and faster (uses caching), in this gist.

Related

Query whether Azure App Services has backup setup

Can anyone suggestion a way to query App Services backup status?
For example, Let say I have a lot of App Services in Azure, different resource groups, instead of clicking through each one and check if backup is setup or not. I want to have a query so I can execute it and return the result?
Thanks in advance.
Based on the above shared requirement , we have written the below PowerShell script to pull the backup details of a webapp. the below script will check whether if there is backup enabled or not for a particular web under resource group.
if backup not enabled scrip will through an error -->
Get-AzWebAppBackupConfiguration : Operation returned an invalid status code 'NotFound'
if backup is enabled scrip will return -->
webappName,storageaccounturi of the backup where it is stored
$RGName='<ResourceGroupName>' ##Pass your ResourceGroupName
$list= Get-AzWebApp -ResourceGroupName $RGName
$listarray=$list
foreach($list in $listarray){
$config=Get-AzWebAppBackupConfiguration -ResourceGroupName $RGName -Name $list.name
Write-Host $config.Name,$config.StorageAccountUrl| Format-Table -AutoSize
}
Here is the sample output for reference:

Error: The Azure PowerShell context has not been properly initialized. Please import the module and try again

I have a azure powershell function app. The job of this function app is to generate azure storage account SAS token
I am using the following command to generate the token
$StartTime = Get-Date
$EndTime = $StartTime.AddMinutes(50.0)
$token = Get-AzStorageAccount -Name "<storage-account-name>" -ResourceGroupName "<resource-group-name>" | New-AzStorageContainerSASToken -Container <container-name> -Permission rdwl -StartTime $StartTime -ExpiryTime $EndTime
The code gave the valid sas token for some days. But since some time its throwing error
The Azure PowerShell context has not been properly initialized. Please import the module and try again
I saw a few questions mentioning "'session has not been properly initialized'" and nowhere mention for context.
I need help in resolving this issue
This is most likely caused by https://github.com/Azure/azure-functions-powershell-worker/issues/554. The recently released Az.Accounts 2.1.* introduced this regression. Until this is fixed in the modules, the temporary workaround is to roll back to Az.Accounts 1.9.5 by using these instructions: https://github.com/Azure/azure-functions-powershell-worker/issues/552#issue-732797653. Please note that this step is critically important:
Import-Module Az.Accounts -RequiredVersion '1.9.5'
Yes, "the context has not been properly initialized" is different with "the session has not been properly initialized". Maybe the module had been lost after run several days.
After checking the documents about using PowerShell modules, my suggestion is use Save-Module before publishing your Function. Consider deploying your function app to an App Service plan set to always on or to a Premium plan.
A more complex solution may also work is configure the context manually.

Cannot wire up Azure Scaleset to Automation DSC using powershell cmdlets

I'm trying to wire up Scaleset VMs to the Azure Automation DSC server using the DSC extension. This is not exposed via the Portal, but from this documentation it seems that it should be possible though templates and powershell cmd line.
I have the boiled things down to the following snippet (with sensitive vars masked):
$settings = #{
configurationArguments = #{
registrationUrl = "https://ne-agentservice-prod-1.azure-automation.net/accounts/xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx"
}
}
$protectedSettings = #{
configurationArguments = #{
registrationKey = #{
userName = "NOT_USED"
password = "/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=="
}
}
}
Get-AzureRmVmss -ResourceGroupName $resourceGroupName -VMScaleSetName $VmSsName |
Add-AzureRmVmssExtension -Name "DSC" -Publisher "Microsoft.Powershell" -Type "DSC" -TypeHandlerVersion "2.24" -Setting $settings -ProtectedSetting $protectedSettings |
Update-AzureRmVmss
In the portal, the extension is listed. However nothing is happening: I neither see any VMs listed in the Automation DSC "Nodes" list, nor do I see an DSC activity on the scaleset VMs - the event viewer for DSC is empty; the "c:\WindowsAzure\Logs" folder has nothing related to DSC.
I'm at wits-end as I feel tantalizingly close to getting this working, but am getting no feedback as to what is wrong...
I've managed to get the Add-AzureRmVmssExtension as part of a New-AzureRmVmss powershell pipeline flow. I needed to make two changes:
1) to not encode the $setting and $protectedSetting hashtables as Json - my original snippet was using ConvertTo-Json on the hashtable to get string json. In my defence, the documentation for those params indicates: "Specifies private configuration for the extension, as a string.". I will file a documentation bug.
2) (and this I think is the main reason), I updated the TypeHandlerVersion to 2.76 which is the current latest version - copy-paste from an Internet example gave me 2.24. I'm gonna see if I can get away with not specifying the version at all - I would always want the latest.

Conditionally deploy a resource in resource manager

I have an ARM template to deploy/update the full Azure infrastructure for my application.
Our build server should run the template, and add/update/delete resources that are add/changed/deleted. To make this work, I have chosen for the "Complete" deployment mode.
To test the ARM template, I have to following power shell script:
param(
$tenantId = "",
$subscriptionId = ""
)
Clear-Host
Login-AzureRmAccount -TenantId $tenantId -SubscriptionId $subscriptionId
New-AzureRmResourceGroupDeployment `
-Name "x" `
-ResourceGroupName "rg-test" `
-TemplateFile $PSScriptRoot/resource-template.json `
-TemplateParameterFile $PSScriptRoot/parameters-test.json `
-Mode Complete
This powershell script is only used to test the template, because a vsts release step will be responsible for the execution of the ARM template into the resource group.
We want to use 1 template to deploy everything (to keep it simple, just a Web Service plan and a web app service), but we have resources that doesn't need to be deployed in some environments. Different environments will use different pricing plans, and some of them will need a Deployment Slot, others won't (to save costs).
I have read about the nested templates, and at first it seemed to solve my problem... but it doesn't.
I cannot use the nested templates in a "Complete deployment".
Does anyone know another way, to flag if a resource needs to be deployed or not, is not the "nested-template"-approach and works for the full deployment type?
We could create different parameter files for different environments (dev, test, or production), and then we could customize the deployment by providing values that are tailored for a particular environment. Besides, as we know, we could use nested templates for conditional deployment, but only the root-level template is allowed Complete for the deployment mode. If you have to use Complete mode, you may need to write script to dynamically generate your templates for different environments based on your requirements and business logic.

Start-AzureVM: Getting struck continously

Can anyone please tell me if there are any change for "Start-AzureVM" API?
It is getting struck with following command
Start-AzureVM -ServiceName (get-azurevm | where {$_.name -eq $VMName}).ServiceName -NAME $VMName
Also help me on where to look for latest azure API changes?
Make sure that you select a valid Azure Subscription using Select-AzureSubscription cmdlet before you use Start-AzureVM cmdlet.
When you do not have a valid Azure Subscription, there is a potential chance for all other Azure cmdlets behave awkwardly like you mentioned in your question.

Resources