SetAzVMextension to Connect Vm to Log Analytics

With the following code I want to connect my VMs to my log analytics workspace.

$ResourceGroup = "stackoverflow"
$WorkspaceName = "dontberudepls"


$AllVMs = Get-AzVM -ResourceGroupName $ResourceGroup

for ($i=0; $i -lt $AllVMs.length; $i++){

$vWorkspace = Get-AzResource -Name $WorkspaceName
If (-not $WorkspaceName) {Write-Host -ForegroundColor Yellow "Workspace " $WorkspaceName " wasn’t found in the current subscription."; return}
$vWorkSpace = Get-AzOperationalInsightsWorkspace -Name $vWorkspace.Name -ResourceGroupName $vWorkspace.ResourceGroupName
$vWorkspaceID = $vWorkspace.CustomerID
$vworkspaceKey = (Get-AzOperationalInsightsWorkspaceSharedKeys -ResourceGroupName $vworkspace.ResourceGroupName -Name $vworkspace.Name).PrimarySharedKey
Set-AzVMExtension -ResourceGroupName $AllVMs[$i].ResourceGroupName -VMName $AllVMs[$i].Name -Name ‘MicrosoftMonitoringAgent’ -Publisher ‘Microsoft.EnterpriseCloud.Monitoring’ -ExtensionType ‘MicrosoftMonitoringAgent’ -TypeHandlerVersion ‘1.0’ -Location $AllVMs[$i].Location -SettingString "{‘workspaceId’: ‘$vWorkspaceID’}" -ProtectedSettingString "{‘workspaceKey’: ‘$vworkspaceKey’}"

}

And I got the following error code when trying to deploy this code:

Set-AzVMExtension : Invalid property identifier character: ‘. Path '', line 1, position 1.

I am writing a script that is creating a log analytics workspace and adding all logs from the VMs from a specific resource group inside my workspace.

I followed mostly this tutorial. My script adds more resources than just this function so I wanted to do this in Powershell. But I can't seem to notice where the error comes from. I dont use identifier character: ‘. Path '' Path anywhere ?

I came to this SO question that have the same functionality. but just connects everything to the log analytics which is not exactly what I need. I hope that someone can help me out what I am doing wrong or missing or not seeing clearly ?

2

1 Answer

When you copied pieces of code, it used the wrong quotes.

‘MicrosoftMonitoringAgent’ should be 'MicrosoftMonitoringAgent'

Same for the others, otherwise they won't be seen as strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest