0

I am working on a PowerShell runbook to look at all vms in the azure subscription, find out if guest level monitoring is not enabled and enable it

Following is the command I am using to enable the diags.

Set-AzureRmVMDiagnosticsExtension -ResourceGroupName xxxxxx -VMName xxxxxx -DiagnosticsConfigurationPath $diagnosticsconfig_path -StorageAccountName xxxxxx

I am thinking of storing the diag xml file in a storage blog, how do I point to it with $diagnosticsconfig_path ?

If I use

Get-AzureStorageBlobContent -blob "xxx.json" -Container xxx -Context $storageAccount.Context 

or

Get-AzureStorageFileContent -ShareName 'xxx' -Context $storageAccount.Context -path xxx.json 

The runbook can download the file but how do I refer to this file as the diagpath in Set-AzureRmVMDiagnosticsExtension

Deb
  • 3
  • 2

2 Answers2

1

Try the command below, you are no need to download the file, we can point the path directly to the blob url.

$SAResourceGroupName="<Storage Account ResourceGroupName>"
$StorageAccountName="<StorageAccountName>"

$StorageAccountKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $SAResourceGroupName -AccountName $StorageAccountName).Value[1]
$Context=New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey

$tmpStart = Get-Date
$tmpEnd = $tmpStart.AddHours(2.0)
$SASToken = New-AzureStorageBlobSASToken -Blob "diagnostics_publicconfig.xml" -Container "111" -Context $Context -Permission r -StartTime $tmpStart -ExpiryTime $tmpEnd -FullURI

Set-AzureRmVMDiagnosticsExtension -ResourceGroupName joywebapp -VMName joyVM -DiagnosticsConfigurationPath "$SASToken" -StorageAccountName joystoragev2

enter image description here

In the runbook:

enter image description here

Joy Wang
  • 39,905
  • 3
  • 30
  • 54
  • Thanks Joy for the response, Following is what I am getting when I try the script Set-AzureRmVMDiagnosticsExtension : Illegal characters in path. I tried something similar before by manually created the SAS and fed it to variable $diagnosticsconfig_path and it gave me same error. – Deb Nov 21 '18 at 12:22
  • @Deb I test it in the runbook, it also works, see my update, could you provide your specific command? especially the `-DiagnosticsConfigurationPath` you used. – Joy Wang Nov 22 '18 at 05:47
  • $SASToken = New-AzureStorageBlobSASToken -Blob "GLD.json" -Container "scripts" -Context $Context -Permission r -StartTime $tmpStart -ExpiryTime $tmpEnd -FullURI Set-AzureRmVMDiagnosticsExtension -ResourceGroupName deb_nonprod -VMName AZINF3001 -DiagnosticsConfigurationPath $SASToken -StorageAccountName nonproddiag980 – Deb Nov 22 '18 at 10:24
  • I tried in Azure runbook too and it gave me same error. Does it matter if the storage blob has more than 1 file in it? – Deb Nov 22 '18 at 10:58
  • Tried creating a fresh storage account created blob and placed file there, still didnt work. – Deb Nov 22 '18 at 11:04
  • `$SASToken = https://xxxx.blob.core.windows.net/scripts/GLD.json?sv=2018-03-28&sr=b&sig=xxxxXXXX01010101xxxx1M%3D&st=2018-11-22T11%3A39%3A34Z&se=2018-11-22T13%3A39%3A34Z&sp=r` – Deb Nov 22 '18 at 11:47
  • I tried modifying the link by adding `&` in place of `&` now its throwing a different error `Set-AzureRmVMDiagnosticsExtension : The remote server returned an error: (404) Not Found.` – Deb Nov 22 '18 at 12:46
  • @Deb Your token looks fine, but my file is a `.xml` file, you could have a try. – Joy Wang Nov 23 '18 at 00:58
  • 1
    yes that worked, wonder y it didn't work as .json. However what I did for the xml is copied the sample xml from `https://learn.microsoft.com/en-us/azure/virtual-machines/extensions/diagnostics-windows` changed the resource id and storageaccount and applied it. Applied fine, but now when I try to change the settings in the portal (eg. Sample rate of processor time) it says `Update failed` . Also in this case for every VM I will need a separate xml, thats gona be a pain. – Deb Nov 23 '18 at 12:40
  • @Deb If it works, please mark my reply as answer, there is an option to mark on the left of my reply, thanks. – Joy Wang Nov 23 '18 at 13:43
1

Thanks for great script . Let me define the entire process in steps:

  1. Go to this Microsoft page, copy the XML sample file, paste in powershell and save as XML.

  2. Select one storage account, select a container and upload the XML file.

  3. Now use this script to enable or change the exiting storage account.

    $VMRGName = (Get-AzureRmResource -Name $VMName -ResourceType "Microsoft.Compute/virtualMachines" ).ResourceGroupName
    $StorageAccountName= "storageaccount01"
    $SAResourceGroupName= (Get-AzureRmResource -Name $diagstoragename -ResourceType 'Microsoft.Storage/storageAccounts').ResourceGroupName
    $storagecontainer = "test"
    $StorageAccountKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $SAResourceGroupName -AccountName $StorageAccountName).Value[1]
    $Context= New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
    $tmpStart = Get-Date
    $tmpEnd = $tmpStart.AddHours(2.0)
    $SASToken = New-AzureStorageBlobSASToken -Blob "DiagnosticsPubConfig.xml" -Container $storagecontainer -Context $Context -Permission r -StartTime $tmpStart -ExpiryTime $tmpEnd -FullURI
    #to SET
    Set-AzureRmVMDiagnosticsExtension -ResourceGroupName $VMRGName -VMName $VMName  -DiagnosticsConfigurationPath "$SASToken" -StorageAccountName $StorageAccountName 
    #to GET
    $publicsettings = (Get-AzureRmVMDiagnosticsExtension  -ResourceGroupName $VMRGName -VMName $VMName).PublicSettings
    $encodedconfig = (ConvertFrom-Json -InputObject $publicsettings).StorageAccount
d219
  • 2,707
  • 5
  • 31
  • 36
Subhendu
  • 11
  • 1