本文提供了对订阅内的 Windows 经典部署虚拟机和资源管理器部署虚拟机执行批量安装并配置 Microsoft Anti-Malware 扩展的 PowerShell 脚本。

关于安装 Windows 虚拟机扩展的先决条件,请参考 适用于 Windows 的虚拟机扩展和功能

准备扩展的配置

请将配置根据格式储存成 .json 文件或 .xml 文件。如需更改配置内容,请参照后文的配置字段说明。

Json 格式样例

复制
{
"AntimalwareEnabled": true,
"RealtimeProtectionEnabled": true,
"ScheduledScanSettings": {
"isEnabled": true,
"day": 7,
"time": 120,
"scanType": "Quick"
},
"Exclusions": {
"Extensions": ".ext1;.ext2",
"Paths": "c:\\excluded-path-1;c:\\excluded-path-2",
"Processes": "excludedproc1.exe;excludedproc2.exe"
}
}

XML 格式样例

复制
<AntimalwareConfig>
<AntimalwareEnabled>true</AntimalwareEnabled>
<RealtimeProtectionEnabled>true</RealtimeProtectionEnabled>
<ScheduledScanSettings isEnabled="true" day="7" time="120" scanType="Quick"/>
<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>
<Monitoring>ON</Monitoring>
<StorageAccountName>contosostorage</StorageAccountName>
</AntimalwareConfig>

配置字段说明

PowerShell 脚本

预定义订阅号、储存 Anti-Malware 扩展日志的存储账号和本地电脑上 .json 配置文件的存放位置。以下信息按实际环境配置。

复制
$subscriptionid = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$diagstorageaccountname = "xxxxxxxxx"
$configPath = "C:\Users\Desktop\antimalware.json"

通过 PowerShell 登陆 Azure

复制
$cred = Get-Credential
Import-Module azure
Import-Module azurerm.profile
Add-AzureAccount -Environment azurechinacloud -Credential $cred
Login-AzureRmAccount -EnvironmentName azurechinacloud -Credential $cred
Get-AzureSubscription -SubscriptionId $subscriptionid | Select-AzureSubscription
Get-AzureRmSubscription -SubscriptionId $subscriptionid | Select-AzureRmSubscription

抓取经典部署 Windows 虚拟机并安装扩展

复制
#Grab all classic VMs from subscription
$classicVM = Get-AzureVM
$classicVMnum = $classicVM.count
#specify storage context to store monitoring events
$StorageContext = New-AzureStorageContext -StorageAccountName $diagstorageaccountname -StorageAccountKey (Get-AzureStorageKey -StorageAccountName $diagstorageaccountname).Primary
#Install antimalware extension for all classic Windows VMs that are in started status
For ($i=0; $i -lt $classicVMnum; $i++) {
if (($classicVM[$i].Status -eq "ReadyRole") -and ($classicvm[$i].VM.OSVirtualHardDisk.OS -eq "Windows")) {
$ifinstalled = $null;
$ifinstalled = Get-AzureVMMicrosoftAntimalwareExtension -vm $classicvm[$i];
if ($ifinstalled -eq $null) {
$result = Set-AzureVMMicrosoftAntimalwareExtension -VM $classicVM[$i] -AntimalwareConfigFile $configPath -Monitoring ON -StorageContext $StorageContext | Update-AzureVM
Write-Host "The installation of anti-malware for VM" $classicVM[$i].InstanceName "in cloud service" $classicVM[$i].ServiceName $result.OperationStatus
} else {
Write-Host "VM" $classicVM[$i].InstanceName "in cloud service" $classicVM[$i].ServiceName "already has anti-malware plugin installed"
}
}
}

抓取资源管理器部署 Windows 虚拟机并安装扩展

复制
#Grab all ARM VMs
$ARMVM = Get-AzureRmVM
$ARMVMnum = $ARMVM.Count
#read configuration string from json file
$settingString = [IO.File]::ReadAllText($configPath); #get latest version from extension info
$allversions = @((Get-AzureRmVMExtensionImage -Location "chinaeast" -PublisherName "Microsoft.Azure.Security" -Type "IaaSAntimalware").Version)
$versionstring = $allVersions[($allVersions.count)-1].Split(".")[0] + "." + $allVersions[($allVersions.count)-1].Split(".")[1] #Install antimalware extension for all ARM Windows VMs that are in started status, except windows2016.
For ($j = 0; $j -lt $ARMVMnum; $j++) {
$VMstatus = Get-azurermvm -Name $ARMVM[$j].Name -ResourceGroupName $ARMVM[$j].ResourceGroupName -status
if (($VMstatus.Statuses[1].Code -eq "PowerState/running") -and ($ARMVM[$j].StorageProfile.OsDisk.OsType -eq "Windows") -and ($ARMVM[$j].StorageProfile.ImageReference.Sku -notlike "2016-*")) {
$ifinstalled = $null;
$ifinstalled = Get-AzureRmVMExtension -ResourceGroupName $ARMVM[$j].ResourceGroupName -VMName $ARMVM[$j].Name -Name "IaaSAntimalware" -ErrorAction SilentlyContinue
if ($ifinstalled -eq $null) {
$result = Set-AzureRmVMExtension -ResourceGroupName $ARMVM[$j].ResourceGroupName -VMName $ARMVM[$j].Name -Location $ARMVM[$j].Location -Name "IaaSAntimalware" -Publisher "Microsoft.Azure.Security" -ExtensionType "IaaSAntimalware" -TypeHandlerVersion $versionstring -SettingString $settingString
Write-Host "The installation of anti-malware for VM" $ARMVM[$j].Name "in resource group" $ARMVM[$j].ResourceGroupName "was" $result.StatusCode "with reason" $result.ReasonPhrase
} else {
Write-Host "VM" $ARMVM[$j].Name "in resource group" $ARMVM[$j].ResourceGroupName "already has anti-malware plugin installed"
}
}
}

此示例脚本的限制

  1. 请用户先在非生产环境中测试此脚本,确认可用且无其他问题后再部署到生产环境中。
  2. Anti-Malware 扩展不兼容 Windows Server 2016,原因是新一代系统自带 Windows Defender,功能与 Anti-Malware 扩展冲突,导致安装失败。
  3. 由于 Azure VM 磁盘属性的限制,经典部署模式的命令无法判别虚拟机的操作系统是否是 Windows Server 2016,所以经典部署下的 Windows Server 2016 执行此脚本时会报错。
  4. 如果虚拟机曾经做过从经典部署模式迁移到资源管理器部署模式的操作,由于迁移后资源管理器虚拟机磁盘属性仍然会继承之前经典部署虚拟机的磁盘属性,资源管理器部署部分的脚本无法判别迁移后虚拟机的磁盘属性。
  5. 经典部署模式的命令支持将 Anti-Malware 扩展扫描时生成的 Windows Event Log 转存到 Azure 存储账户中,而资源管理器部署模式的命令不含此配置,所以无法做到 Event Log 的转存。

查询 Anti-Malware 扩展的安装状态

可以使用以下命令来查询 Anti-Malware 扩展的状态和配置:

  • 经典部署模式

    复制
      Get-AzureVM -ServiceName "ServiceName" -Name "VMname" | Get-AzureVMMicrosoftAntimalwareExtension

    输出:

  • 资源管理器部署模式

    复制
      Get-AzureRmVMExtension -ResourceGroupName "ResourceGroupName" -VMName "VMName" -Name "IaaSAntimalware"

    输出:

System Center Endpoint Protection 错误解决方法

在虚拟机内打开 System Center Endpoint Protection 时会弹出 “你的系统管理员已限制访问此应用” 的错误:

进入指定目录执行以下命令 :

复制
C:\Program Files\Microsoft Security Client> ConfigSecurityPolicy.exe cleanuppolicy.xml            立即访问http://market.azure.cn

为订阅内虚拟机批量安装并配置 Microsoft Anti-Malware 扩展的更多相关文章

  1. VMware 虚拟机图文安装和配置 AlmaLinux OS 8.6 教程

    前言: 这是<VMware 虚拟机图文安装和配置 Rocky Linux 8.5 教程>一文的姐妹篇教程,如果你需要阅读它,请点击这里. 2020 年,CentOS 宣布:计划未来将重心从 ...

  2. 必须使用“角色管理工具”安装或配置Microsoft .NET Framework 3.5 SP1

    在Windows Server 2008下直接安装SQL Server 2008时,会出现如下错误: 必须使用“角色管理工具”安装或配置Microsoft .NET Framework 3.5 SP1 ...

  3. 解决【必须使用“角色管理工具”安装或配置Microsoft .NET Framework 3.5 SP1】的方法

    [摘要:正在Windows Server 2008下间接装置SQL Server 2008时,会涌现以下毛病: 必需应用“脚色治理对象”装置或设置装备摆设Microsoft .NET Framewor ...

  4. windows Server 2008 -必须使用“角色管理工具”安装或配置Microsoft .Net Framework 3.5

    在windows Server 2008上安装 .Net Framework 3.5的时候,报错:必须使用“角色管理工具”安装或配置Microsoft .Net Framework 3.5. Solu ...

  5. 必须使用角色管理工具 安装或配置microsoft.net framework 3.5

    windows server 2008安装sql server 2012后报错,要求安装microsoft.net framework 3.5 sp1 但安装时提示,必须使用角色管理工具 安装或配置m ...

  6. VMware 虚拟机图文安装和配置 Rocky Linux 8.5 教程

    前言 2020 年,CentOS 宣布:计划未来将重心从 CentOS Linux 转移到 CentOS Stream.CentOS 8 的生命周期已于 2021 年 12 月 31 日终止,而 Ce ...

  7. Hadoop集群--linux虚拟机Hadoop安装与配置、克隆虚拟机

    Hadoop集群 第四章 Hadoop安装与配置.克隆虚拟机 一.Hadoop安装与配置 1.将hadoop安装包通过Xftp传输到虚拟机的/software目录下 2.进入/software目录下, ...

  8. 在hyper-v虚拟机中安装并配置linux

    虽然都是自己写的,还是贴个原文链接吧,如果文章里的图片错乱了,可能就是我贴错了,去看原文吧. 多图警告 WSL2真香? WSL2相比于WSL1前者更类似于虚拟机,配合上Windoes Terminal ...

  9. nginx + php + mysql安装、配置、自启动+redis扩展

    用过了apache就想着用用nginx,网上教程其实很多,但是受服务器版本等限制,每个人遇到的问题也不一样,先记录下我的 一.安装依赖 yum -y install gcc zlib zlib-dev ...

随机推荐

  1. c++面试题中经常被面试官面试的小问题总结(二)(本篇偏向指针知识)

    原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/10713204.html 1.利用指针交换两个字符串方法?(这题是我当年读大一的时候看到的,好怀 ...

  2. 10w定时任务,如何高效触发超时

    一.缘起 很多时候,业务有定时任务或者定时超时的需求,当任务量很大时,可能需要维护大量的timer,或者进行低效的扫描. 例如:58到家APP实时消息通道系统,对每个用户会维护一个APP到服务器的TC ...

  3. 通配符证书导致 Outlook Anywhere 的客户端连接问题

    通配符证书导致 Outlook Anywhere 的客户端连接问题 本主题介绍当您使用 Outlook Anywhere 连接到 Microsoft Exchange 及在组织中跨 Exchange ...

  4. PL/SQL:these query result are not updateable,include the ROWID to get updateab -----for update

    these query result are not updateable,include the ROWID to get updateab 原因: 其实,选中一个表后,右键,如果选择“query ...

  5. 航空公司客户价值分析(KMeans聚类)

    PS.图片可能不清楚,代码 数据集都在 https://github.com/xubin97/Data-Mining_exp1 项目介绍: 本案例的目标是客户价值识别,通过航空公司客户数据识别不同价值 ...

  6. 每天一道剑指offer-二叉树的下一个结点

    题目 每天一道剑指offer-二叉树的下一个结点 https://www.nowcoder.com/practice/ef068f602dde4d28aab2b210e859150a?tpId=13& ...

  7. java并发编程(6)显式锁

    显式锁 一.Lock与ReentrantLock Lock提供了一种无条件的.可轮询的.定时的以及可中断的锁获取操作,所有的加锁和解锁方法都是显式的 ReentrantLock实现了Lock:并提供了 ...

  8. jquery实现的让图片在网页的可视区域里四处漂浮的效果

    本文分享给大家一个用jquery开发的图片漂浮效果. jquery图片漂浮效果,常见的就是网页里四处漂来漂去的广告了,漂到边缘时还会反弹和拐弯. 下面来看具体的代码,先看要实现漂亮效果的这个jquer ...

  9. Linux NTP时间服务器

    NTP 时间服务器 ntp也是一种协议 ntp软件(支持ntp协议)  CentOS6自带 CentOS7需要安装 chrony软件(支持ntp协议)   CentOS7自带 安装ntp CentOS ...

  10. js控制表格隔行变色

    只是加载时候隔行变一个颜色,鼠标滑动上去时候没有变化 <table width="800" border="0" cellpadding="0& ...