install OMS Log Analytics Agent and OMS Dependency Agent to all Windows VMs in the subscription using PowerShell


Save the following code as myscript.ps1 by replacing the ????? with the OMS workspaceid and OMS workspace key. Share the file using Storage Account Shared Access key as https://

function installOMS {

param(
[parameter(Mandatory=$true, HelpMessage="OMS Log Analytics Workspace ID")]
[ValidateNotNullOrEmpty()]
[string]$WorkSpaceID,

[parameter(Mandatory=$true, HelpMessage="OMS Log Analytics Workspace Key")]
[ValidateNotNullOrEmpty()]
[string]$WorkSpaceKey
)

$verboseactionoutput = "continue"
# Set the parameters
$FileName = "MMASetup-AMD64.exe"
$SMFileName = "InstallDependencyAgent-Windows.exe"
$OMSFolder = 'C:\OMSinstallSource'
$MMAFile = $OMSFolder + "\" + $FileName
$SMFile = $OMSFolder + "\" + $SMFileName

# Check if folder exists, if not, create it
Write-Verbose "Create folder $OMSFolder..."
New-Item $OMSFolder -type Directory -Force | Out-Null
Write-Verbose "created!"

# Change the location to the specified folder
Set-Location $OMSFolder

try
{

# Download OMS Log Analytics monitoring agent
Write-Verbose "downloading monitoring agent..."
$URL = "https://go.microsoft.com/fwlink/?LinkId=828603"
Invoke-WebRequest -Uri $URl -OutFile $MMAFile | Out-Null
Write-Verbose "downloaded!"

# Download OMS Log Analytics dependency agent
Write-Verbose "downloading dependency agent..."
$URL = "https://aka.ms/dependencyagentwindows"
Invoke-WebRequest -Uri $URl -OutFile $SMFile | Out-Null
Write-Verbose "downloaded!"

# Install the Microsoft Monitoring Agent
Write-Verbose "Installing Microsoft Monitoring Agent.."
$ArgumentList = '/C:"setup.exe /qn ADD_OPINSIGHTS_WORKSPACE=1 '+ "OPINSIGHTS_WORKSPACE_ID=$WorkspaceID " + "OPINSIGHTS_WORKSPACE_KEY=$WorkSpaceKey " +'AcceptEndUserLicenseAgreement=1"'
Start-Process $FileName -ArgumentList $ArgumentList -ErrorAction Stop -Wait | Out-Null
Write-Verbose "installed!"

# Install the Service Map Agent
Write-Verbose "Installing dependency Agent.."
$ArgumentList = '/C:"InstallDependencyAgent-Windows.exe /S /AcceptEndUserLicenseAgreement:1"'
Start-Process $SMFileName -ArgumentList $ArgumentList -ErrorAction Stop -Wait | Out-Null
Write-Verbose "installed!"

# Change the location to C: to remove the created folder
Set-Location -Path "C:\"

# Remove the folder with the agent files
Write-Verbose "Removing the folder $OMSFolder ..."
Remove-Item $OMSFolder -Force -Recurse | Out-Null
Write-Verbose "removed!"
}
catch
{
Write-Error -Message $_.Exception
throw $_.Exception
}

}

installOMS -WorkSpaceID "?????????????" -WorkSpaceKey "???????????"

Run the following code on any management client to inject the agents installation using Custom Script Extension for the Windows VMs in the subscription. Change the ??????? withe URL of the shared ps1 file.

$cred = Get-Credential
Add-AzureRmAccount -Credential $cred
Get-AzureRmSubscription
Set-AzureRmContext -SubscriptionId "????????"

$allvms = Get-AzureRmVM | where {$allvms[0].OSProfile.LinuxConfiguration -eq $null}

foreach ($vm in $allvms)
{
# Remove the script extention
Remove-AzureRmVMCustomScriptExtension -ResourceGroupName $vm.ResourceGroupName `
-VMName $vm.Name -Name "myCustomScript"

# Deploy the script extenion
Set-AzureRmVMCustomScriptExtension -ResourceGroupName $vm.ResourceGroupName `
-VMName $vm.Name -Name "myCustomScript" `
-FileUri "https://?????????/myscript.ps1.ps1" `
-Run "myscript.ps1" -Location "West EU"
}


Comments (2)

  1. you have to to run antivirus remove the error.

Skip to main content