Error during while running Azure Runbook

Bombbe 1,611 Reputation points
2021-02-19T14:14:59.617+00:00

Hi,
this is part of bigger Runbook but I somehow keep getting errors in this part

 if ($vm.StorageProfile.OsDisk.OsType -eq 'Windows' -and $vmStatus -match 'VM running')  
        {  
                    
            $extensions = Get-AzureRmVMExtension -ResourceGroupName $vm.ResourceGroupName -VMName $vm.Name | where {$_.ExtensionType -eq "CustomScriptExtension"} -ErrorAction SilentlyContinue  

            # these are for debug reason   
            Write-Output "$extensions.Name"  
            Write-Output "Extensions = $extensions"  
      
            #Make sure the extension is not already installed before attempting to install it  
            if (-not $extensions)  
            {  
              
             Write-Output "Adding CustomScriptExtension to configure VM: $($vm.Name)"  
             $result = Set-AzureRmVMCustomScriptExtension -ResourceGroupName $vm.ResourceGroupName `  
                -Location $vm.Location `  
                -VMName $vm.Name `  
                -Name CustomScriptExtension `  
                -TypeHandlerVersion "1.1" `  
                                          
            }  
            else  
            {     
                Write-Output "Removing existing CustomScriptExtension on VM: $($vm.Name) named $extensions.Name"  
      
                $extensionsName = Get-AzureRmVMExtension -ResourceGroupName $vm.ResourceGroupName -VMName $vm.Name | where {$_.ExtensionType -eq "CustomScriptExtension"}  
      
                #Remove existing custom script extension  
                Remove-AzureRmVMExtension -ResourceGroupName $vm.ResourceGroupName -VMName $vm.Name -name $extensionsName.Name  
                  
                Write-Output "Adding CustomScriptExtension to VM: $($vm.Name)"  
                #Adding custom script extension to server  
                $result = Set-AzureRmVMCustomScriptExtension -ResourceGroupName $vm.ResourceGroupName `  
                -Location $vm.Location `  
                -VMName $vm.Name `  
                -Name CustomScriptExtension `  
                -TypeHandlerVersion "1.1" `  
                                     
                  
              
                  }  
        }  

Errors during run. All errors are same: can't find parameter "Name".

70125-errori.png

More detailed error
70131-errori2.png

I I think have managed to pretty much narrowed error to this line " $extensionsName = Get-AzureRmVMExtension -ResourceGroupName $vm.ResourceGroupName -VMName $vm.Name | where {$_.ExtensionType -eq "CustomScriptExtension"}". I have tried to run following command on Powershell and Cloudshell and I get the results that I want but somehow I can't manage to get it work inside my Runbook. $extensions.Name should print out of name of Custom Script Extension. Do I maybe need to add new module or modules in Automation Account? Any tips?

   $extensionsName = Get-AzureRmVMExtension -ResourceGroupName $vm.ResourceGroupName -VMName $vm.Name | where {$_.ExtensionType -eq "CustomScriptExtension"}  
Write-Output "$extensions.Name"  
Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,159 questions
0 comments No comments
{count} votes

Accepted answer
  1. tbgangav-MSFT 10,406 Reputation points
    2021-02-20T11:42:53.11+00:00

    Hi @Bombbe ,

    Most probably it's because -Name parameter of Get-AzureRMVMExtension cmdlet is always required when you use that cmdlet.

    For reference, check below screenshot.

    70274-get-azurermvmextension.png

    So, please add -Name parameter and a value to it and see if it resolves the issue.


0 additional answers

Sort by: Most helpful