背景:
阅读新闻

connect vm to log analytics workspace

  作者: 今日评论: [字体: ]
https://docs.microsoft.com/en-us/azure/virtual-machines/extensions/oms-windows
PowerShell deployment

The Set-AzVMExtension command can be used to deploy the Log Analytics agent virtual machine extension to an existing virtual machine. Before running the command, the public and private configurations need to be stored in a PowerShell hash table.

PowerShell
$PublicSettings = @{"workspaceId" = "myWorkspaceId"}
$ProtectedSettings = @{"workspaceKey" = "myWorkspaceKey"}

Set-AzVMExtension -ExtensionName "MicrosoftMonitoringAgent" `
    -ResourceGroupName "myResourceGroup" `
    -VMName "myVM" `
    -Publisher "Microsoft.EnterpriseCloud.Monitoring" `
    -ExtensionType "MicrosoftMonitoringAgent" `
    -TypeHandlerVersion 1.0 `
    -Settings $PublicSettings `
    -ProtectedSettings $ProtectedSettings `
    -Location WestUS 


https://github.com/Azure/Azure-Security-Center/blob/master/Remediation%20scripts/Install%20monitoring%20agent%20on%20your%20machines/PowerShell/Install-MMA-VMExtension.ps1
#***************************************************************************************************************
# This script will import a CSV file, based on the "checkForMMAExtension" script output file
# and install the MMA extension on all the VMs found in that CSV
#**************************************************************************************************************

$ErrorActionPreference = 'Stop'

#Login-AzAccount

#variables
$VMinputFolder = "c:\temp\"
$VMinputFile = "ASC-inputFile.csv"  # We will install the MMA VM extension based on the VMs in this file


#fill in your workspaceID, workspaceKey, resourcegroup name and location
$publicSettings = @{"workspaceId" = "<YourWorkspaceID>"}
$protectedSettings = @{"workspaceKey" = "<YourWorkspaceKey>"}

# Importing CSV file
Write-Host "*** Trying to import file:" ($VMinputFolder + $VMinputFile) " ***" -ForegroundColor Green
try {
    $targetVMs = Import-Csv -Path ($VMinputFolder + $VMinputFile)
    Write-Host "Your input file contains" $targetVMs.VMname.count "VM's" -ForegroundColor Green
}
catch {Write-Host "Could not open input file.... Please your path and filename." -ForeGroundColor Red}

$answer = Read-Host "Continue installation? (yes/no)"
if($answer -eq "yes"){
    Write-Host "Starting VM extension installation" -ForegroundColor Green
    }
else
{
    Write-Host "Aborting installation" -ForegroundColor Red
    break
}

# install VM extension
foreach($VM in $targetVMs)
{
    Write-Output "`r`n"
    Write-Host "*** Trying to install the MMA VM extension on" $VM.VMname "- Subscription:" $VM.SubScriptionName -ForegroundColor Green
    Write-Host "Changing to subscription:" $VM.SubScriptionName -ForegroundColor Green
    if ($VM.OsType -eq "Windows") {
        try
        {
            Set-AzContext -Subscription $VM.SubScriptionName
            Write-Host "Installing VM Extension of type Windows....please wait..." -ForeGroundColor Green
            try
            {
                # check if the VM extension has already been installed
                Get-AzVMExtension -VMName $VM.VMname -ResourceGroupName $VM.ResourceGroup -Name "MicrosoftMonitoringAgent" | Select-Object VMName, ProvisioningState, ResourceGroupName
                Write-Host "Extension has already been installed, so skipping...." -ForegroundColor Red

            }
            catch
            {
                Set-AzVMExtension -VMName $VM.VMname -ResourceGroupName $VM.ResourceGroup `
                -Name MicrosoftMonitoringAgent `
                -TypeHandlerVersion 1.0 `
                -Publisher Microsoft.EnterpriseCloud.Monitoring  `
                -ExtensionType MicrosoftMonitoringAgent `
                -Settings $publicSettings `
                -ProtectedSettings $protectedSettings `
                -Location $VM.Location
                Write-Host "Done!" -ForeGroundColor Green
            }

        
        }
        catch {Write-Host "Could not set subscription or could not install the VM extension" -ForegroundColor Red}
        
    }
    #VM is of type Linux
    elseif ($VM.OsType -eq "Linux") {
        Set-AzContext -Subscription $VM.SubScriptionName
        Write-Host "Installing VM Extension of type Linux....please wait..." -ForeGroundColor Green
        try
        {
            # check if the VM extension has already been installed
            Get-AzVMExtension -VMName $VM.VMname -ResourceGroupName $VM.ResourceGroup -Name "MicrosoftMonitoringAgent" | Select-Object VMName, ProvisioningState, ResourceGroupName
            Write-Host "Extension has already been installed, so skipping...." -ForegroundColor Red

        }
        catch
        {
            Set-AzVMExtension -VMName $VM.VMname -ResourceGroupName $VM.ResourceGroup `
            -Name MicrosoftMonitoringAgent `
            -TypeHandlerVersion 1.7 `
            -Publisher Microsoft.EnterpriseCloud.Monitoring  `
            -ExtensionType OmsAgentForLinux `
            -Settings $publicSettings `
            -ProtectedSettings $protectedSettings `
            -Location $VM.Location
            Write-Host "Done!" -ForeGroundColor Green
        }
    }
    else {
        Write-Host "No valid OS type found!" -ForegroundColor Red
    }
}


来源:
录入日期:[2020/11/18 9:35:00]
收藏 推荐 打印 | 录入:mikebai | 阅读:
文章评论      
正在加载评论列表...
评论关闭