0

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 ?

achahbar
  • 901
  • 3
  • 21
  • 47
  • 1
    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. – Michael B. Apr 24 '19 at 11:58
  • Thanks man. That did the trick. Totally overlooked over this small detail. If you copy your answer in a answer I will accept the answer. – achahbar Apr 24 '19 at 12:18

1 Answers1

1

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.

Michael B.
  • 558
  • 3
  • 11