PowerShell: รับสถานะ Windows Defender จากคอมพิวเตอร์ที่เข้าร่วมโดเมนทั้งหมด (Get-AntiMalwareStatus)
คุณใช้ Windows 10 และ Windows Server 2016 หรือสูงกว่าใช่ไหม ต้องการตรวจสอบสถานะของ Windows Defender Anti-Malware จากคอมพิวเตอร์ทุกเครื่องหรือไม่ ถ้าอย่างนั้นคุณก็มาถูกที่แล้ว ฉันจะให้เครื่องมือแก่คุณในการทำเช่นนั้น
เป้าหมาย
ไม่ต้องพูดอะไรมาก นี่คือสิ่งที่เป็นไปได้ด้วยฟังก์ชัน PowerShell ขั้นสูงที่เรียกว่า Get-AntiMalwareStatus
Get-AntiMalwareStatus -Scope AllServer | Format-Table -AutoSize
เจ๋งใช่มั้ย?
หากไม่มี Format-Table ผลลัพธ์จะแสดงในรูปแบบรายการ ต่อไปนี้เป็นตัวอย่างการรับสถานะมัลแวร์จากทุกโดเมนที่เข้าร่วมคอมพิวเตอร์
ค่าที่เป็นไปได้สำหรับพารามิเตอร์ขอบเขตคือ:
- AllServer=โดเมนทั้งหมดเข้าร่วม Windows Server
- AllComputer=โดเมนทั้งหมดเข้าร่วมคอมพิวเตอร์ระบบปฏิบัติการ Windows
หากไม่มีการกำหนดพารามิเตอร์ขอบเขต ฟังก์ชันจะแสดงสถานะ Defender ของคอมพิวเตอร์ในระบบ
สิ่งที่คุณต้องมีคือ โดเมน Active Directory และหากคุณต้องการดึงข้อมูล Windows Defender จากคอมพิวเตอร์ไคลเอนต์ คุณต้อง เปิดใช้งาน WinRm บนระบบปฏิบัติการไคลเอนต์ทั้งหมด ตามที่อธิบายไว้ที่นี่: กลุ่ม นโยบาย: การเปิดใช้งาน WinRM สำหรับระบบปฏิบัติการไคลเอนต์ Windows (Windows 10, Windows 8, Windows 7)
ฟังก์ชัน Get-AntiMalwareStatus
สำหรับการใช้ฟังก์ชันนี้ในเซสชัน PowerShell ของคุณ ให้ย้ายไปยังจุดถัดไป
function Get-AntiMalwareStatus {
# .SYNOPSIS
# Get-AnitMalewareStatus is an advanced Powershell function.
# It gets the Windows Defender Status of the local computer and remote computer.
# .DESCRIPTION
# Uses Invoke-Command and Get-MpComputerStatus.
# .PARAMETER
# Scope
# Define a scope. Possible values:
# AllServer, AllComputer
# Default: localhost
# .EXAMPLE
# Get-AntiMalwareStatus -Scope AllComputer
# .NOTES
# Author: Patrick Gruenauer
# Web: https://sid-500.com
[CmdletBinding()]
param
(
[Parameter(Position=0,Helpmessage = 'Possible Values: AllServer, AllComputer')]
[ValidateSet('AllServer','AllComputer')]
$Scope
)
$result=@()
$ErrorActionPreference="SilentlyContinue"
switch ($Scope) {
$null {
Get-MpComputerStatus | Select-Object -Property Antivirusenabled,AMServiceEnabled,AntispywareEnabled,BehaviorMonitorEnabled,IoavProtectionEnabled,`
NISEnabled,OnAccessProtectionEnabled,RealTimeProtectionEnabled,AntivirusSignatureLastUpdated
}
AllServer {
$server=Get-ADComputer -Filter 'operatingsystem -like "*server*" -and enabled -eq "true"' | Select-Object -ExpandProperty Name
foreach ($s in $server) {
$rs=Invoke-Command -ComputerName $s {Get-MpComputerStatus | Select-Object -Property Antivirusenabled,AMServiceEnabled,AntispywareEnabled,` BehaviorMonitorEnabled,IoavProtectionEnabled,NISEnabled,OnAccessProtectionEnabled,RealTimeProtectionEnabled,AntivirusSignatureLastUpdated}
If ($rs) {
$result+=New-Object -TypeName PSObject -Property ([ordered]@{
'Server'=$rs.PSComputername
'Anti-Virus'=$rs.AntivirusEnabled
'AV Update'=$rs.AntivirusSignatureLastUpdated
'Anti-Malware'=$rs.AMServiceEnabled
'Anti-Spyware'=$rs.AntispywareEnabled
'Behavior Monitor'=$rs.BehaviorMonitorEnabled
'Office-Anti-Virus'=$rs.IoavProtectionEnabled
'NIS'=$rs.NISEnabled
'Access Prot'=$rs.OnAccessProtectionEnabled
'R-T Prot'=$rs.RealTimeProtectionEnabled
})
}
}
}
AllComputer {
$comp=Get-ADComputer -Filter 'enabled -eq "true"' | Select-Object -ExpandProperty Name
foreach ($c in $comp) {
$rs=Invoke-Command -ComputerName $c {Get-MpComputerStatus | Select-Object -Property Antivirusenabled,AMServiceEnabled,AntispywareEnabled,` BehaviorMonitorEnabled,IoavProtectionEnabled,NISEnabled,OnAccessProtectionEnabled,RealTimeProtectionEnabled,AntivirusSignatureLastUpdated}
If ($rs) {
$result+=New-Object -TypeName PSObject -Property ([ordered]@{
'Computer'=$rs.PSComputername
'Anti-Virus'=$rs.AntivirusEnabled
'AV Update'=$rs.AntivirusSignatureLastUpdated
'Anti-Malware'=$rs.AMServiceEnabled
'Anti-Spyware'=$rs.AntispywareEnabled
'Behavior Monitor'=$rs.BehaviorMonitorEnabled
'Office-Anti-Virus'=$rs.IoavProtectionEnabled
'NIS'=$rs.NISEnabled
'Access Prot'=$rs.OnAccessProtectionEnabled
'R-T Prot'=$rs.RealTimeProtectionEnabled
})
}
}
}
}
Write-Output $result
}
วิธีการใช้งาน
คัดลอกโค้ดด้านบนลงใน PowerShell ISE (ise.exe) และเรียกใช้โค้ด จากนั้นพิมพ์คำสั่งและสนุกกับมัน
หากคุณต้องการทำให้ฟังก์ชันพร้อมใช้งานอย่างถาวร เพื่อให้ฟังก์ชันพร้อมใช้งานทุกครั้งที่คุณเริ่ม PowerShell คุณต้องสร้างโฟลเดอร์ใน C:\Program Files\WindowsPowerShell\Modules ตั้งชื่อโฟลเดอร์ Get-AntiMalwareStatus จากนั้นบันทึกโค้ดเป็นไฟล์ .psm1 ในโฟลเดอร์นั้น ภาพหน้าจอด้านล่างจะช่วยคุณได้
พบกันใหม่ครั้งหน้าอีกครั้งกับ PowerShell!



