Sampel kode untuk mengaktifkan dan mengonfigurasi Microsoft Antimalware untuk Azure

Artikel ini menyediakan sampel kode PowerShell untuk mengaktifkan dan mengonfigurasi Microsoft Antimalware untuk berbagai layanan Azure termasuk:

  • Mesin Virtual Azure Resource Manager
  • kluster Azure Service Fabric
  • Azure Cloud Services (dukungan diperluas)
  • server dengan dukungan Azure Arc

Gunakan contoh-contoh ini untuk menerapkan dan mengonfigurasi ekstensi Microsoft Antimalware di seluruh lingkungan Azure Anda.

Menyebarkan Microsoft Antimalware pada VM Azure Resource Manager

Nota

Sebelum Anda menjalankan contoh kode ini, hapus komentar variabel dan berikan nilai yang sesuai.

Peringatan

Menyebarkan atau memperbarui ekstensi ini menggantikan pengaturan Antivirus Microsoft Defender yang ada, termasuk pengecualian. Untuk mempertahankan pengaturan Anda, tentukan pengaturan tersebut dalam konfigurasi ekstensi. Untuk informasi selengkapnya, lihat Konfigurasi Antimalware Default dan Kustom.

# Script to add Microsoft Antimalware extension to Azure Resource Manager VMs
# Specify your subscription ID
$subscriptionId= " SUBSCRIPTION ID HERE "
# Specify location, resource group, and VM for the extension
$location = " LOCATION HERE " # For example, "Southeast Asia" or "Central US"
$resourceGroupName = " RESOURCE GROUP NAME HERE "
$vmName = " VM NAME HERE "

# Enable Antimalware with default policies
$settingString = '{"AntimalwareEnabled": true}'
# Enable Antimalware with custom policies
# $settingString = '{
# "AntimalwareEnabled": true,
# "RealtimeProtectionEnabled": true,
# "ScheduledScanSettings": {
#                             "isEnabled": true,
#                             "day": 0,
#                             "time": 120,
#                             "scanType": "Quick"
#                             },
# "Exclusions": {
#            "Extensions": ".ext1,.ext2",
#                  "Paths":"",
#                  "Processes":"sampl1e1.exe, sample2.exe"
#             },
# "SignatureUpdates": {
#                               "FileSharesSources": "",
#                               "FallbackOrder": "",
#                               "ScheduleDay": 0,
#                               "UpdateInterval": 0,
#                       },
# "CloudProtection": true
#
# }'
# Sign in to Azure and select the subscription to use
Connect-AzAccount

Set-AzContext -SubscriptionId $subscriptionId
# Retrieve the most recent version number of the extension
$allVersions = (Get-AzVMExtensionImage -Location $location -PublisherName "Microsoft.Azure.Security" -Type "IaaSAntimalware").Version
$versionString = $allVersions[($allVersions.Count)-1].Split(".")[0] + "." + $allVersions[($allVersions.Count)-1].Split(".")[1]
# Set the extension by using prepared values
Set-AzVMExtension -ResourceGroupName $resourceGroupName -Location $location -VMName $vmName -Name "IaaSAntimalware" -Publisher "Microsoft.Azure.Security" -ExtensionType "IaaSAntimalware" -TypeHandlerVersion $versionString -SettingString $settingString

Tambahkan Microsoft Antimalware ke klaster Azure Service Fabric

Azure Service Fabric menggunakan set skala mesin virtual Azure untuk membuat klaster Service Fabric. Templat kumpulan skala komputer virtual yang membuat kluster Service Fabric tidak dikonfigurasi dengan ekstensi Antimalware. Aktifkan Antimalware secara terpisah pada kumpulan skala. Saat Anda mengaktifkannya pada set skala mesin virtual, semua node yang dibuat dalam set skala mesin virtual tersebut akan mewarisi dan mendapatkan ekstensi tersebut secara otomatis.

Contoh kode berikut menunjukkan cara mengaktifkan ekstensi IaaS Antimalware dengan menggunakan cmdlet Az.Compute PowerShell.

Nota

Sebelum Anda menjalankan contoh kode ini, hapus komentar variabel dan berikan nilai yang sesuai.

Peringatan

Menyebarkan atau memperbarui ekstensi ini menggantikan pengaturan Antivirus Microsoft Defender yang ada, termasuk pengecualian. Untuk mempertahankan pengaturan Anda, tentukan pengaturan tersebut dalam konfigurasi ekstensi. Untuk informasi selengkapnya, lihat Konfigurasi Antimalware Default dan Kustom.

# Script to add Microsoft Antimalware extension to a virtual machine scale set (VMSS) and Service Fabric cluster
# Sign in to Azure and select the subscription to use
Connect-AzAccount
# Specify your subscription ID
$subscriptionId="SUBSCRIPTION ID HERE"
Set-AzContext -SubscriptionId $subscriptionId
# Specify location, resource group, and VMSS for the extension
$location = "LOCATION HERE" # For example, "West US", "Southeast Asia", or "Central US"
$resourceGroupName = "RESOURCE GROUP NAME HERE"
$vmScaleSetName = "YOUR VM SCALE SET NAME"

# Customize the configuration.json configuration file according to the documentation: https://msdn.microsoft.com/library/dn771716.aspx
$settingString = '{"AntimalwareEnabled": true}'
# Enable Antimalware with custom policies
# $settingString = '{
# "AntimalwareEnabled": true,
# "RealtimeProtectionEnabled": true,
# "ScheduledScanSettings": {
#                             "isEnabled": true,
#                             "day": 0,
#                             "time": 120,
#                             "scanType": "Quick"
#                             },
# "Exclusions": {
#            "Extensions": ".ext1,.ext2",
#                  "Paths":"",
#                  "Processes":"sampl1e1.exe, sample2.exe"
#             } ,
# "SignatureUpdates": {
#                               "FileSharesSources": "",
#                               "FallbackOrder": "",
#                               "ScheduleDay": 0,
#                               "UpdateInterval": 0,
#                       },
# "CloudProtection": true
# }'

# Retrieve the most recent version number of the extension
$allVersions = (Get-AzVMExtensionImage -Location $location -PublisherName "Microsoft.Azure.Security" -Type "IaaSAntimalware").Version
$versionString = $allVersions[($allVersions.Count)-1].Split(".")[0] + "." + $allVersions[($allVersions.Count)-1].Split(".")[1]
$vmss = Get-AzVmss -ResourceGroupName $resourceGroupName -VMScaleSetName $vmScaleSetName
Add-AzVmssExtension -VirtualMachineScaleSet $vmss -Name "IaaSAntimalware" -Publisher "Microsoft.Azure.Security" -Type "IaaSAntimalware" -TypeHandlerVersion $versionString -Setting $settingString
Update-AzVmss -ResourceGroupName $resourceGroupName -VMScaleSetName $vmScaleSetName -VirtualMachineScaleSet $vmss

Tambahkan Microsoft Antimalware ke Azure Cloud Services dengan menggunakan dukungan tambahan

Contoh kode berikut menunjukkan cara menambahkan atau mengonfigurasi Microsoft Antimalware ke Azure Cloud Services dengan menggunakan dukungan tambahan melalui cmdlet PowerShell.

Nota

Sebelum Anda menjalankan contoh kode ini, hapus komentar variabel dan berikan nilai yang sesuai.

Peringatan

Menyebarkan atau memperbarui ekstensi ini menggantikan pengaturan Antivirus Microsoft Defender yang ada, termasuk pengecualian. Untuk mempertahankan pengaturan Anda, tentukan pengaturan tersebut dalam konfigurasi ekstensi. Untuk informasi selengkapnya, lihat Konfigurasi Antimalware Default dan Kustom.

# Create an Antimalware extension object, where file is AntimalwareSettings
$xmlconfig = [IO.File]::ReadAllText("C:\path\to\file.xml")
$extension = New-AzCloudServiceExtensionObject -Name "AntimalwareExtension" -Type "PaaSAntimalware" -Publisher "Microsoft.Azure.Security" -Setting $xmlconfig -TypeHandlerVersion "1.5" -AutoUpgradeMinorVersion $true

# Get existing Cloud Service
$cloudService = Get-AzCloudService -ResourceGroup "ContosOrg" -CloudServiceName "ContosoCS"

# Add Antimalware extension to existing Cloud Service extension object
$cloudService.ExtensionProfile.Extension = $cloudService.ExtensionProfile.Extension + $extension

# Update Cloud Service
$cloudService | Update-AzCloudService

Berikut contoh file XML konfigurasi privat:

<?xml version="1.0" encoding="utf-8"?>
<AntimalwareConfig
    xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <AntimalwareEnabled>true</AntimalwareEnabled>
    <RealtimeProtectionEnabled>true</RealtimeProtectionEnabled>
    <ScheduledScanSettings isEnabled="true" day="1" time="120" scanType="Full" />
    <Exclusions>
        <Extensions>
            <Extension>.ext1</Extension>
            <Extension>.ext2</Extension>
        </Extensions>
        <Paths>
            <Path>c:\excluded-path-1</Path>
            <Path>c:\excluded-path-2</Path>
        </Paths>
        <Processes>
            <Process>excludedproc1.exe</Process>
            <Process>excludedproc2.exe</Process>
        </Processes>
    </Exclusions>
</AntimalwareConfig>

Menambahkan Microsoft Antimalware untuk server yang mendukung Azure Arc

Contoh kode berikut menunjukkan cara menambahkan Microsoft Antimalware untuk server yang mendukung Azure Arc melalui cmdlet PowerShell.

Nota

Sebelum Anda menjalankan contoh kode ini, hapus komentar variabel dan berikan nilai yang sesuai.

# Before you use Azure PowerShell to manage VM extensions on your hybrid server managed by Azure Arc-enabled servers, install the Az.ConnectedMachine module. Run the following command on your Azure Arc-enabled server:
# If Az.ConnectedMachine is installed, ensure the version is at least 0.4.0
Install-Module -Name Az.ConnectedMachine
Import-Module -Name Az.ConnectedMachine

# Specify location, resource group, and machine for the extension
$subscriptionid =" SUBSCRIPTION ID HERE "
$location = " LOCATION HERE " # For example, "Southeast Asia" or "Central US"
$resourceGroupName = " RESOURCE GROUP NAME HERE "
$machineName = "MACHINE NAME HERE "

# Enable Antimalware with default policies
$setting = @{"AntimalwareEnabled"=$true}
# Enable Antimalware with custom policies
$setting2 = @{
"AntimalwareEnabled"=$true;
"RealtimeProtectionEnabled"=$true;
"ScheduledScanSettings"= @{
                            "isEnabled"=$true;
                            "day"=0;
                            "time"=120;
                            "scanType"="Quick"
                            };
"Exclusions"= @{
           "Extensions"=".ext1, .ext2";
                 "Paths"="";
                 "Processes"="sampl1e1.exe, sample2.exe"
            };
"SignatureUpdates"= @{
                              "FileSharesSources"="";
                              "FallbackOrder"="";
                              "ScheduleDay"=0;
                              "UpdateInterval"=0;
                      };
"CloudProtection"=$true
}
# Sign in to Azure
Connect-AzAccount
# Enable Antimalware with the policies
New-AzConnectedMachineExtension -Name "IaaSAntimalware" -ResourceGroupName $resourceGroupName -MachineName $machineName -Location $location -SubscriptionId $subscriptionid -Publisher "Microsoft.Azure.Security" -Settings $setting -ExtensionType "IaaSAntimalware"

Langkah selanjutnya