Set-AzureRmResource throwing api-version error - azure

I am trying to programmatically add tags to resources in Azure Government. When I try to set the tags on a resource that has no tags I am using the Set-AzureRmResource command. I have tried both setting the ApiVersion and without (without is supposed to use the latest) When I use the Debug flag it shows the version being set but I still get an error below.
Set-AzureRmResource : Cannot validate argument on parameter 'ResourceId'. The argument is null or empty. Provide an
argument that is not null or empty, and then try the command again.
At line:1 char:108
+ ... ONMENT=""; ORGANIZATION="" } -ResourceId $resource.ResourceId -Force ...
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Set-AzureRmResource], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implem
entation.SetAzureResourceCmdlet
The snippet I am trying to run is below.
Set-AzureRmResource -Tag #{ ENVIRONMENT=""; ORGANIZATION="" } -ResourceId $resource.ResourceId -Force
Edit: Specify Azure Government
Edit 2: Removed explicit version setting from code

I tried using the new Az commands in Azure Government:
Set-AzResource -Tag #{ ENVIRONMENT=""; ORGANIZATION="" } -ResourceId $resource.ResourceId -Force
..and it worked fine for me. (I got a serialization error using the older AzureRm commands). FYI... not sure when you tried it yesterday, but there was a DNS issue that caused some service outages, which may have resulted in erroneous errors.

I tested your snippet above against an Azure Government resource and its working correctly using AzureRM module version 6.13.1.
You can your version by running this snippet
Get-Module AzureRM -List

For anyone who sees this in the future. The problem is a known bug in the way Azure encodes the '#' symbol in a ResourceId. So do not use '#' in a resource string.
The code change we are making is to use the following:
$resource.ResourceId = $resource.ResourceId.Replace("#", "%23")
Simple fix if you have the # symbol, or change your governance to not name things with '#'.
Thanks for all the other replies. We had to open a ticket to get this information.

Related

Azure Tag for Azure Depolyment not working

I am trying to create a Azure deployment with Tag parameter to put tag on the deployment using command New-AzResourceGroupDeployment like -
New-AzResourceGroupDeployment -ResourceGroupName "demo" -TemplateFile "D:\Templates\storage.json" -Tag #{"CreatedFrom"="PS"; "Env"="Dev";}
but facing this issue -
New-AzResourceGroupDeployment : A parameter cannot be found that matches parameter name 'Tag'.
At line:1 char:102
+ ... Name "demo" -TemplateFile "D:\Templates\storage.json" -Tag #{"Crea ...
+ ~~~~
+ CategoryInfo : InvalidArgument: (:) [New-AzResourceGroupDeployment], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.N
ewAzureResourceGroupDeploymentCmdlet
According to the documentation we can put tag parameter with New-AzResourceGroupDeployment command but its not working for me.
Any help on what I am doing wrong here would be much appreciated!
The command works fine here. Make sure you have the latest version of the commands.
https://learn.microsoft.com/en-us/powershell/azure/new-azureps-module-az?view=azps-6.1.0

Get-AzResourceGroup : 'this.Client.SubscriptionId' cannot be null

Having a bit of an issue with Azure and Powershell. I'm just checking to see if a resource group exists and I keep hitting this error. The next step after this is to create the resource group if it does not exist, but that is also throwing the same error. Was hoping someone might be able to suggest some workarounds or fixes.
Please note, I have access to the subscriptions, I can see them and sucessfully set the default subscription as can be seen in the script below.
Full Error Returned
'this.Client.SubscriptionId' cannot be null.
At **********************\envir\create-env.ps1:21 char:1
+ Get-AzResourceGroup -Name $resourceGroup -ErrorVariable $doesNotExist ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Get-AzResourceGroup], ValidationException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.GetAzureResourceGroupCmdlet
I've just installed the Az module, version details below.
Version Name Repository
------- ---- ----------
1.6.0 Az PSGallery
I'm just running a pretty straight forward script (below), I thought it might be something to do with a default subscription not been set, but setting that has made no difference.
$passwd = ConvertTo-SecureString $servicePrincipalKey -AsPlainText -Force
$pscredential = New-Object
System.Management.Automation.PSCredential($servicePrincipalUserName, $passwd)
Connect-AzAccount -ServicePrincipal -Credential $pscredential -TenantId $tenantId
Select-AzureSubscription -Default -SubscriptionName $subscriptioName
Get-AzResourceGroup -Name $resourceGroup -ErrorVariable $doesNotExist
I then get the error listed above.
I found the problem, the Service Principal I had created did not have sufficient access to the subscription as suggested by #4c74356b41, I ended up giving it a role of Contributor and that resolved the problem.

Get-AzureRmAutomationDscNodeConfiguration: "There is an unterminated literal at position 40 in 'properties/configuration/name eq config''."

I have problems running a PowerShell CmdLets to get Azure Automation Node Configurations
Cmdlets:
Get-AzureRmAutomationDscNodeConfiguration -ResourceGroupName "ResourceGroup03" -AutomationAccountName "Contoso17" -ConfigurationName "config"
Like Example 2 in https://learn.microsoft.com/en-us/powershell/module/azurerm.automation/get-azurermautomationdscnodeconfiguration?view=azurermps-6.13.0#examples
If i run the cmdlets like Exampe 3 it works with no errors.
The error:
Get-AzureRmAutomationDscNodeConfiguration : There is an unterminated
literal at position 40 in 'properties/configuration/name eq config''.
At line:1 char:1
+ Get-AzureRmAutomationDscNodeConfiguration -ResourceGroupName $rg -
Aut ...
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Get-
AzureRmAuto...deConfiguration], ErrorResponseException
+ FullyQualifiedErrorId:
Microsoft.Azure.Commands.Automation.Cmdlet.GetAzureAutomationDscNodeConfiguration
If i run the cmdlet without "-ConfigurationName "config" I get no error.
Get-AzureRmAutomationDscNodeConfiguration -ResourceGroupName "ResourceGroup03" -AutomationAccountName "Contoso17"
The output of this cmdlet shows ConfigurationName "config".
ResourceGroupName : XXX
AutomationAccountName : XXX
Name : config.TestNode
CreationTime : 10.03.2019 14.10.44 +01:00
LastModifiedTime : 10.03.2019 14.10.44 +01:00
ConfigurationName : config
RollupStatus : Good
Any idea?
Welcome to Stack Overflow! :)
Good find! I was also able to reproduce it so I have reported this -> https://github.com/Azure/azure-powershell/issues/8738 issue with the concerned Microsoft Azure team.
Note that I have reported the above mentioned issue by referencing to PowerShell Az module cmdlet but not PowerShell AzureRm module cmdlet because as per this -> https://learn.microsoft.com/en-us/powershell/azure/new-azureps-module-az?view=azps-1.4.0 Microsoft article it is recommended to upgrade to Az module from AzureRm module.
For more process related information, please refer below links.
https://github.com/Azure/azure-powershell#reporting-issues-and-feedback
https://github.com/Azure/azure-powershell/issues/new/choose
Meanwhile, as a workaround to accomplish your requirement of getting metadata for DSC node configurations in Automation by specifying the name of DSC configuration for which the cmdlet gets node configuration metadata, you may just use below command.
For PowerShell Az module:
Get-AzAutomationDscNodeConfiguration -ResourceGroupName "ResourceGroup03" -AutomationAccountName "Contoso17" | ?{$_.ConfigurationName -eq "config"}
For PowerShell AzureRm module:
Get-AzureRmAutomationDscNodeConfiguration -ResourceGroupName "ResourceGroup03" -AutomationAccountName "Contoso17" | ?{$_.ConfigurationName -eq "config"}
Hope this helps!! Cheers!! :)

The term 'Set-AzureRmVMPlan' is not recognized as the name of a cmdlet

I am trying to create a VM on Azure using powershell script in Azure Automation. This VM is to be created from marketplace. So I need to use cmdlet 'Set-AzureRmVMPlan' in my script.
$vm = New-AzureRmVMConfig -VMName $inVMName -VMSize $vmSize
Set-AzureRmVMPlan -VM $vm -Publisher "kali-linux" -Product
"kali-linux" -Name "kali"
This script works fine when I run it from Powershell ISE however when I put this command in Azure Automation Runbook then Azure doesn't recognize this command. I get below error,
Set-AzureRmVMPlan : The term 'Set-AzureRmVMPlan' is not recognized as
the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try
again.
At restore-vhd-backups:8 char:8
CategoryInfo : ObjectNotFound: (Set-AzureRmVMPlan:String) [], CommandNotFoundException
FullyQualifiedErrorId : CommandNotFoundException
So I further investigate and found that this command is not available in cmdlet list at page "Edit PowerShell Runbook" (see below image)
I also got to know that this cmdlet was added in Azure Powershell last year from below link
How to include "Plan information" when creating ARM VM from a captured image using Powershell?
Can someone help me on this?
So what is happening here, Azure Automation uses the old version of the Module, you have to go to Azure Automation Blade > Assets > Modules > Browse Gallery and reimport AzureRM.Profile and AzureRM.Compute.

Invoke-DscResource on WindowsFeatureSet fails

Invoking Invoke-DSCResource on WindowsFeatureSet fails with strange error.
Invoke-DscResource -Name WindowsFeatureSet -Property #{Name = 'FileAndStorage'; Ensure = 'Present' } -Method test -ModuleName PSDesiredStateConfiguration
Invoke-DscResource : Failed to serialize properties into CimInstance.
At line:5 char:1
+ Invoke-DscResource -Name WindowsFeatureSet -Property #{Name = 'FileAn ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Invoke-DscResource], SerializationException
+ FullyQualifiedErrorId : System.Runtime.Serialization.SerializationException,Microsoft.PowerShell.DesiredStateConfiguration.Commands.InvokeDscResourceMethodCommand
Composite configurations are never loaded by the LCM. They are only used during compilation and the generated mof has the composite configuration resolved to its underlying resources. So this isn't expected to work. You can try using 'WindowsFeature' instead. This blog explains composite resources in DSC http://nanalakshmanan.com/blog/Composite-Resources-Explained/
This is a known issue. There is an issue filed in PowerShell User Voice, which is marked as 'Survey'. 'Survey' indicates 'We saw this and we are considering it...' per the PowerShell User Voice Home Page.
This is also documented in Chef Discourse here.
And there is an issue open to update the Chef Documentation [here] (https://github.com/chef/chef-web-docs/issues/59) and DSC Documentation here.

Resources