Want to connect VMs to Log Analytics? Use this PowerShell script

There are a couple of scripts out there to connect a VM to a Log Analytics workspace, but most of them require tons of parameters such as VM, Workspace, Workspace ID, Resource Group, and so forth.

Here, I summarize the process and create a simple PowerShell script that will retrieve all the information that is not required during the input and connect any given VM to a workspace.

The script is simple to use. Just run it and provide the two required parameters, which are WorkspaceName and VM, as depicted in the image below.

Log Analytics

The result is the VM is connected to the workspace. To get to this page, click on the desired Log Analytics, then click on Virtual Machines located in the Workspace Data Sources section.

Log Analytics

Here is the PowerShell script:

Param(
[parameter(mandatory=$true)]$WorkspaceName,
[parameter(mandatory=$true)]$VM
)
#Checking Azure connection..
Import-Module AzureRM -ErrorAction SilentlyContinue
If (-not (Get-Module AzureRM)) {Write-Host;Write-Host -ForegroundColor Yellow “AzureRM module wasn’t found on the current server, the installation will start (wait for the completion, some confirmation will be required)”; Install-Module AzureRM}
if (Get-AzureRMContext) {write-host;write-host “You are connect to the following Microsoft Azure subscription: ” (Get-AzureRmContext).SubscriptionName} Else { Write-host -ForegroundColor Yellow “You are not connected, please connect.”; Connect-AzureRmAccount}
#Parameter validation…
If (-not $VM) {Write-Host -ForegroundColor Yellow “You must specific a VM in the -VM parameter”; return}
If (-not $WorkspaceName) {Write-Host -ForegroundColor Yellow “You must specific a Workspace in the -VM parameter”; return}
#Parameter data validation…
$vVM = Get-AzureRmResource -Name $VM -ErrorAction:SilentlyContinue
If (-not $vVM) {Write-Host -ForegroundColor Yellow “The ” $VM ” wasn’t found in the current subscription.”; return}
$vWorkspace = Get-AzureRmResource -Name $WorkspaceName
If (-not $WorkspaceName) {Write-Host -ForegroundColor Yellow “Workspace ” $WorkspaceName ” wasn’t found in the current subscription.”; return}
$vWorkSpace = Get-AzureRmOperationalInsightsWorkspace -Name $vWorkspace.Name -ResourceGroupName $vWorkspace.ResourceGroupName
$vWorkspaceID = $vWorkspace.CustomerID
$vworkspaceKey = (Get-AzureRmOperationalInsightsWorkspaceSharedKeys -ResourceGroupName $vworkspace.ResourceGroupName -Name $vworkspace.Name).PrimarySharedKey
Set-AzureRmVMExtension -ResourceGroupName $vVM.ResourceGroupName -VMName $vVM.Name -Name ‘MicrosoftMonitoringAgent’ -Publisher ‘Microsoft.EnterpriseCloud.Monitoring’ -ExtensionType ‘MicrosoftMonitoringAgent’ -TypeHandlerVersion ‘1.0’ -Location $vVM.Location -SettingString “{‘workspaceId’: ‘$vWorkspaceID’}” -ProtectedSettingString “{‘workspaceKey’: ‘$vworkspaceKey’}”


That’s it. You’re all set.

Featured image: Shutterstock

About The Author

1 thought on “Want to connect VMs to Log Analytics? Use this PowerShell script”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top