The remote server returned an error: (403) Forbidden when installin extension to Azure VM

Bombbe 1,611 Reputation points
2021-03-19T07:18:55.247+00:00

Hi,
I'm using following line in my Azure Runbook to enable diagnostic settings for Azure vm

if ($vm.StorageProfile.OsDisk.OsType -eq 'Windows' -and $vmStatus -match 'VM running')
    {

        $extensions = Get-AzVMExtension -ResourceGroupName $vm.ResourceGroupName -VMName $vm.Name -Name 'Microsoft.Insights.VMDiagnosticsSettings' -ErrorAction SilentlyContinue
        #Make sure the extension is not already installed before attempting to install it
        if (-not $extensions)
        {

         Write-Output "Adding Windows Azure Diagnostics extension to configure VM: $($vm.Name)"
         $result = set-AzVMDiagnosticsExtension -ResourceGroupName $vm.ResourceGroupName `
            -VMName $vm.Name `
            -DiagnosticsConfigurationPath "$sastoken" `
            -StorageAccountName "nameofstoageaccount" `
            -StorageAccountKey "$StorageAccountKey"

                   }
        else
        {
            Write-Output  "Skipping VM - Azure Diagnostics Extension already installed"


              }

$StorageAccountKey, $sastoken and $vm are configured top of script and those are working.

When running the following lines I will get Error "The remote server returned an error: (403) Forbidden."

More detailed error:

 set-AzVMDiagnosticsExtension : The remote server returned an error: (403) Forbidden. At line:377 char:20 + ... $result = set-AzVMDiagnosticsExtension -ResourceGroupName $vm.Resou ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : CloseError: (:) [Set-AzVMDiagnosticsExtension], WebException + FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.SetAzureRmVMDiagnosticsExtensionCommand

I have enabled in nsg access to storage with Service tags and in storage account I have enabled "allow access for all networks". I tested from vm side that i was able to test-netconnection to storage account urls.

It seems somehow that Runbook would not able do to enable that extension but really can't find out why and what should I try next. After I CTRL C + V script to Powershell and was able to get it working so there should not neither be syntax or variable errors.

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,111 questions
{count} votes

0 additional answers

Sort by: Most helpful