Set-AzureRmDiagnosticSetting : A parameter cannot be found that matches parameter name 'Name' - azure

Set-AzureRmDiagnosticSetting : A parameter cannot be found that matches parameter name 'Name'.
After executing below command in Azure powershell task
Set-AzureRmDiagnosticSetting -ResourceId $resourceId -Name $diagnosticsettingname -Enabled $true -Categories $logarray -MetricCategory $metricsarray -WorkspaceId $work
I am getting exception as
"Set-AzureRmDiagnosticSetting : A parameter cannot be found that matches parameter name 'Name'."
As per the specification there is the -Name parameter in it. So why I am getting this error

As mentioned in the comment, I think your version of AzureRM.Insights module is old, please update the module with:
Update-Module -Name AzureRM.Insights -Force
And you should note, the AzureRm module has been deprecated, it will not be updated anymore, so I recommend you to use the new Az module instead of AzureRm, see this link to migrate to Az, then use the command Set-AzDiagnosticSetting.

Adding to this, if you want to do both Azure Diagnostics and leveraging an Azure Pipeline to do it, while programmatically doing this at scale with Azure Policy, you can check out this solution which should make this a bit easier to consume.
https://aka.ms/azpolicyPipeline
Without pipeline but automating the policies / policy initiative you can take a look at this project here: https://aka.ms/AzPolicyScripts
I found this post looking for something else related to a bug in Az cmdlets so figured I'd post since I had this detail handy.

Related

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.

Do ARM templates provide a way to capture the logs during creation of resources

we have ARM template which creates the azure resources. While we run the powershell script which created the resources, we want to log all the info.
we want to know if any logging functionality which azure ARM template provides
the list of resources we are as follows :
- storage account
- automation account
- key-vault
- Sql server pool
- functionApp etc
Below are some of the general configurations you can use for the New-AzureRmResourceGroupDeployment to get the most amount of logging from the command. You may lookup the equivalent AZ cmdlet, if you need to.
You can use the -Verbose and -DeploymentDebugLogLevel All parameters to get some more logging info. Note that the Deployment debug parameter will cause a warning to show up in your console output stream.
You can use the -ErrorVariable ErrorMessages parameter to get the error messages and then use them appropriately in your script.
Lastly, you can use the JSON output of the cmdlet to get some insights, if any.
Below is how the cmldet typically looks like for me..
$jsonOutput = New-AzureRmResourceGroupDeployment -Name $DeploymentName `
-ResourceGroupName $ResourceGroupName `
-TemplateFile $TemplateFileToDeploy `
-TemplateParameterObject $TemplateParameters `
-Force -Verbose `
-ErrorVariable ErrorMessages -DeploymentDebugLogLevel All
closest you can get is launch new-azresourcegroupdeployment with the -verbose switch, that would give you some idea whats going on, but I dont really think you are interested in that, you are mostly interested in errors. Fairly certain new-azresourcegroupdeployment outputs those.

Set-AzureRmAppServicePlan - Input string was not in a correct format

When running this command as a Runbook:
Set-AzureRmAppServicePlan -ResourceGroupName "mygroup" -Name "myname" -Tier "Premium" -WorkerSize "Small" -NumberofWorkers 1 -PerSiteScaling $FALSE
I get the error: "Input string was not in a correct format."
I noticed someone else report this on an earlier version of AzureRM and appears to be fixed in 5.0.0:
https://github.com/Azure/azure-powershell/issues/5181
So is it possible that this bug still exists? How do I find out what version of AzureRM is being used within the automation Runbooks?
Per my test, the command works fine in local on my side.
So is it possible that this bug still exists? How do I find out what version of AzureRM is being used within the automation Runbooks?
I think it may caused by the bug, you could check the powershell module version of runbook in your automation account -> Modules.

Unable to Start an Azure VM using Powershell script. We get Parameter set cannot be resolved using the specified named parameters

We use powershell script to start and stop the VMs using the build job. Please see the screenshot of the build job below
It is a simple powershell script to start an VM. The issue is when this job runs, we are getting an error and the build fails.
We get Parameter set cannot be resolved using the specified named parameters
But when we run it locally using the powershell console, the VMs get started.
Please find the error screenshot below
Am I missing something here.. Any help would be very much appreciated.
EDIT 1
Powershell script
$machines = #("machinename")
Select-AzureSubscription -Default "XXXYYYZZZ"
Foreach ($machine in $machines)
{
Try
{
Start-AzureVM -ServiceName $machine -Name $machine
}
Catch
{
}
}
I check your screenshot, I find you want to stop classic VMs. However, you logon Azure with cmdlet Add-AzureRmAccount and select subscription with cmdlet select-azurermsubscription, am I right? The two cmdlets are ARM mode cmdlets, you should use classic mode cmdlets.
Add-AzureAccount
According to your error, you could not use Select-AzureSubscription -Default "XXXYYYZZZ". Default could not add parameters. More information please refer to this link.
Try to use the following cmdlets.
Get-AzureSubscription
Select-AzureSubscription -SubscriptionName <YourSubscriptionName> -Default

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