本文提供 PowerShell 程式碼範例,以啟用並設定 Microsoft 反惡意軟體,適用於不同的 Azure 服務,包括:
- Azure Resource Manager VMs
- Azure Service Fabric 叢集
- Azure 雲端服務 (外延支援)
- 已啟用 Azure Arc 的伺服器
利用這些範例部署並設定 Microsoft 防惡意軟體擴充套件,跨越您的 Azure 環境。
將 Microsoft 防毒軟體部署到 Azure Resource Manager 虛擬機器
備註
在執行這個程式碼範例之前,先取消註解變數並提供適當的值。
警告
部署或更新此擴充功能會取代現有的 Microsoft Defender 防毒設定,包括排除功能。 為了保留你的設定,請在擴充功能設定中指定它們。 欲了解更多資訊,請參閱 預設與自訂防惡意軟體設定。
# 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
將 Microsoft Antimalware 新增至 Azure Service Fabric 叢集
Azure Service Fabric 使用 Azure 虛擬機擴展集來建立 Service Fabric 叢集。 建立 Service Fabric 叢集的虛擬機縮放集範本並未啟用防惡意軟體擴充功能。 在體重計組上分別啟用防惡意軟體。 當你在縮放集啟用時,所有在虛擬機縮放集下建立的節點都會繼承並自動獲得擴充功能。
以下程式碼範例說明如何透過 Az.Compute PowerShell 指令列啟用 IaaS 反惡意軟體擴充功能。
備註
在執行這個程式碼範例之前,先取消註解變數並提供適當的值。
警告
部署或更新此擴充功能會取代現有的 Microsoft Defender 防毒設定,包括排除功能。 為了保留你的設定,請在擴充功能設定中指定它們。 欲了解更多資訊,請參閱 預設與自訂防惡意軟體設定。
# 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
使用延伸支援將 Microsoft Antimalware 新增至 Azure 雲端服務
以下程式碼範例說明如何透過 PowerShell cmdlet 使用擴展支援,將 Microsoft 反惡意軟體加入或設定至 Azure 雲端服務。
備註
在執行這個程式碼範例之前,先取消註解變數並提供適當的值。
警告
部署或更新此擴充功能會取代現有的 Microsoft Defender 防毒設定,包括排除功能。 為了保留你的設定,請在擴充功能設定中指定它們。 欲了解更多資訊,請參閱 預設與自訂防惡意軟體設定。
# 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
以下是私人設定 XML 檔案的範例:
<?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>
為 Azure Arc 啟用的伺服器添加 Microsoft Antimalware
以下程式碼範例說明如何透過 PowerShell 指令執行程式,為支援 Azure Arc 的伺服器新增 Microsoft 反惡意軟體。
備註
在執行這個程式碼範例之前,先取消註解變數並提供適當的值。
# 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"