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 ?