รับ-ADComputer - ตัวกรอง | ตัวอย่างไวยากรณ์
คำสั่ง PowerShell Get-ADComputer เป็นส่วนหนึ่งของโมดูล Active Directory PowerShell ไปที่บทความนี้หากคุณต้องการทราบวิธีการติดตั้ง
หากต้องการ ค้นหาอย่างมีประสิทธิภาพ สำหรับคอมพิวเตอร์ใน Active Directory ของคุณ คุณควร ใช้สวิตช์ตัวกรอง อย่าแยกคอมพิวเตอร์ทุกเครื่องแล้วค้นหาชุดผลลัพธ์
ใช้ Get-ADComputer -Filter เพื่อค้นหาโดยตรงเพื่อประสิทธิภาพที่ดีขึ้น
ทำความเข้าใจกับพารามิเตอร์ตัวกรอง Get-ADComputer
ด้วยตัวเลือกการกรองที่หลากหลาย (การกรอง LDAP, การกรอง oData v3.0 ฯลฯ) สวิตช์ตัวกรองมักจะทำให้เกิดความสับสน
สวิตช์ตัวกรองที่ใช้ในคำสั่ง Get-ADComputer ใช้ภาษานิพจน์ PowerShell ในสตริงการสืบค้น
สิ่งนี้แตกต่างจากเช่น เมื่อใช้คำสั่ง Get-AzureADUser (ซึ่งใช้การกรอง oData v3.0)!
ตัวดำเนินการต่อไปนี้ควรครอบคลุมความต้องการส่วนใหญ่ของคุณ:
| Operator | Meaning | Sample expression |
|---|---|---|
| -eq | Equal to | Name -eq “WKS-2021” |
| -ne | Not equal to | OperatingSystemVersion -ne “10.0 (14393)” |
| -gt | Greater than | LastLogonTimestamp -gt $date |
| -ge | Greater than or equal | Created -ge “01-01-2018” |
| -lt | Less than | Modified -lt “01-02-2021” |
| -le | Less than or equal | Created -le “01-01-2018” |
| -like | Wildcard search | OperatingSystem -like “*2008*” |
| -notlike | Wildcard search with negation | OperatingSystem -notlike “*2016*” |
| -and | And | OperatingSystem -like “*Server*” -and operatingSystemServicePack -eq “Service Pack 1” |
| -or | Or | OperatingSystem -like “*2008*” -or OperatingSystem -like “*2016*” |
โปรดสังเกตสิ่งต่อไปนี้ในตัวอย่างข้างต้น:
- ใส่ค่าที่เปรียบเทียบไว้ในเครื่องหมายอะพอสทรอฟี
- ใช้ -like '*' และ -notlike '*' เพื่อค้นหาค่าว่างหรือไม่ว่าง
- กรองรูปแบบวันที่/เวลา "ลับๆ" โดยตรงด้วยตัวแปร DateTime (เช่น เช่น แอตทริบิวต์ LastLogonTimeStamp ซึ่งใช้รูปแบบ FileTime)
ด้านล่างนี้คุณมีข้อมูลโค้ดบางส่วนที่ควรชี้คุณไปในทิศทางที่ถูกต้องเกี่ยวกับวิธีจัดโครงสร้างตัวกรองของคุณสำหรับคำค้นหา Get-ADComputer:
Get-ADComputer -Filter 'Name -eq "WKS-2021"'Get-ADComputer -Filter 'OperatingSystemVersion -ne "10.0 (14393)"'$Date = (Get-Date).AddDays(-60)
Get-ADComputer -Filter 'lastLogonTimestamp -gt $date'Get-ADComputer -Filter 'Modified -ge "06-04-2021 12:00:00"'$Date = (Get-Date).AddDays(-60)
Get-ADComputer -Filter 'lastLogonTimestamp -lt $date'Get-ADComputer -Filter 'Created -le "01-01-2020"'Get-ADComputer -Filter 'OperatingSystem -like "*2008*"'Get-ADComputer -Filter 'OperatingSystem -notlike "*2016*"'Get-ADComputer -Filter 'OperatingSystem -like "*Server*" -and operatingSystemServicePack -eq "Service Pack 1"'Get-ADComputer -Filter 'OperatingSystem -like "*2008*" -or OperatingSystem -like "*2016*"'