Dresse
Dresse

Reputation: 483

Azure Automation Runbook missing mandatory parameters

I'm trying to set a Tag on all virtual machines in my subscription but I keep getting errors when running the Runbook. The error is the following:

Get-AzureRmVM : Cannot process command because of one or more missing mandatory parameters: ResourceGroupName. At line:30

Here is my Runbook:

    $azureConnection = Get-AutomationConnection -Name 'AzureRunAsConnection'

#Authenticate
try {
    Clear-Variable -Name params -Force -ErrorAction Ignore
    $params = @{
        ServicePrincipal = $true
        Tenant = $azureConnection.TenantID
        ApplicationId = $azureConnection.ApplicationID
        CertificateThumbprint = $azureConnection.CertificateThumbprint
    }
    $null = Add-AzureRmAccount @params
}
catch {
    $errorMessage = $_
    Throw "Unable to authenticate with error: $errorMessage"
}

#  Discovery of all Azure VM's in the current subscription.

$azurevms = Get-AzureRmVM | Select-Object -ExpandProperty Name
Write-Host "Discovering Azure VM's in the following subscription $SubscriptionID  Please hold...."

Write-Host "The following VM's have been discovered in subscription $SubscriptionID"
$azurevms

foreach ($azurevm in $azurevms) {

    Write-Host "Checking for tag $vmtagname on $azurevm"
    $tagRGname = Get-AzureRmVM -Name $azurevm | Select-Object -ExpandProperty ResourceGroupName

    $tags = (Get-AzureRmResource -ResourceGroupName $tagRGname -Name $azurevm).Tags

If ($tags.UpdateWindow){
Write-Host "$azurevm already has the tag $vmtagname."
}
else
{
Write-Host "Creating Tag $vmtagname and Value $tagvalue for $azurevm"
$tags.Add($vmtagname,$tagvalue)

    Set-AzureRmResource -ResourceGroupName $tagRGname -ResourceName $azurevm -ResourceType Microsoft.Compute/virtualMachines -Tag $tags -Force `
   }

}

Write-Host "All tagging is done"

I tried importing the right modules but this doesn't seem to affect the outcome. Running the same commands in Cloud Shell does work correctly.

Upvotes: 0

Views: 1275

Answers (1)

Joy Wang
Joy Wang

Reputation: 42073

I can reproduce your issue, the error was caused by this part Get-AzureRmVM -Name $azurevm, when running this command, the -ResourceGroupName is needed.

enter image description here

You need to use the Az command Get-AzVM -Name $azurevm, it will work.

enter image description here

Running the same commands in Cloud Shell does work correctly.

In Cloud shell, azure essentially uses the new Az module to run your command, you can understand it runs the Enable-AzureRmAlias before the command, you could check that via debug mode.

Get-AzureRmVM -Name joyWindowsVM -debug

enter image description here


To solve your issue completely, I recommend you to use the new Az module, because the AzureRM module was deprecated and will not be updated.

Please follow the steps below.

1.Navigate to your automation account in the portal -> Modules, check if you have imported the modules Az.Accounts, Az.Compute, Az.Resources, if not, go to Browse Gallery -> search and import them.

2.After import successfully, change your script to the one like below, then it should work fine.

$azureConnection = Get-AutomationConnection -Name 'AzureRunAsConnection'

#Authenticate
try {
    Clear-Variable -Name params -Force -ErrorAction Ignore
    $params = @{
        ServicePrincipal = $true
        Tenant = $azureConnection.TenantID
        ApplicationId = $azureConnection.ApplicationID
        CertificateThumbprint = $azureConnection.CertificateThumbprint
    }
    $null = Connect-AzAccount @params
}
catch {
    $errorMessage = $_
    Throw "Unable to authenticate with error: $errorMessage"
}

#  Discovery of all Azure VM's in the current subscription.

$azurevms = Get-AzVM | Select-Object -ExpandProperty Name
Write-Host "Discovering Azure VM's in the following subscription $SubscriptionID  Please hold...."

Write-Host "The following VM's have been discovered in subscription $SubscriptionID"
$azurevms

foreach ($azurevm in $azurevms) {

    Write-Host "Checking for tag $vmtagname on $azurevm"
    $tagRGname = Get-AzVM -Name $azurevm | Select-Object -ExpandProperty ResourceGroupName

    $tags = (Get-AzResource -ResourceGroupName $tagRGname -Name $azurevm).Tags

If ($tags.UpdateWindow){
Write-Host "$azurevm already has the tag $vmtagname."
}
else
{
Write-Host "Creating Tag $vmtagname and Value $tagvalue for $azurevm"
$tags.Add($vmtagname,$tagvalue)

    Set-AzResource -ResourceGroupName $tagRGname -ResourceName $azurevm -ResourceType Microsoft.Compute/virtualMachines -Tag $tags -Force `
   }

}

Write-Host "All tagging is done"

Upvotes: 1

Related Questions