วิธีค้นหาอีเมลด้วย PowerShell
วิธีที่ยอดเยี่ยมในการค้นหาข้อความในกล่องจดหมาย Exchange Online คือการใช้ PowerShell คุณสามารถปรับแต่งคำสั่งตามที่คุณต้องการและรับผลลัพธ์ที่คุณต้องการ นอกจากนี้ การส่งออกผลลัพธ์เป็นไฟล์ CSV ก็ทำได้ดีเยี่ยม ดังนั้นคุณจึงสามารถกรองเข้าไปได้ ในบทความนี้ คุณจะได้เรียนรู้วิธีค้นหาอีเมลด้วย PowerShell
ติดตั้ง Microsoft Graph PowerShell
เรียกใช้ Windows PowerShell ในฐานะผู้ดูแลระบบและติดตั้ง Microsoft Graph PowerShell
Install-Module Microsoft.Graph -ForceInstall-Module Microsoft.Graph.Beta -AllowClobber -Forceข้อสำคัญ: ติดตั้งโมดูล Microsoft Graph PowerShell และ Microsoft Graph Beta PowerShell เสมอ นั่นเป็นเพราะบาง cmdlet ยังไม่พร้อมใช้งานในเวอร์ชันสุดท้าย และจะไม่ทำงาน อัปเดตทั้งสองโมดูลเป็นเวอร์ชันล่าสุดก่อนที่คุณจะเรียกใช้ cmdlet หรือสคริปต์เพื่อป้องกันข้อผิดพลาดและผลลัพธ์ที่ไม่ถูกต้อง
กำหนดค่าการลงทะเบียนแอปใน Microsoft Entra
ก่อนที่คุณจะสามารถรันคำสั่งหรือสคริปต์ได้ คุณต้องสร้างการลงทะเบียนแอปด้วยสิทธิ์ที่ถูกต้องและความลับของไคลเอนต์เพื่อตรวจสอบสิทธิ์กับ Microsoft Graph PowerShell ก่อน
1. ลงทะเบียนใบสมัครใหม่
ลงทะเบียนแอปพลิเคชันใหม่ใน Microsoft Entra ID:
- ลงชื่อเข้าใช้ศูนย์การจัดการ Microsoft Entra
- คลิก ข้อมูลประจำตัว > แอปพลิเคชัน > การลงทะเบียนแอป
- คลิกการลงทะเบียนใหม่
- ตั้งชื่อแอปพลิเคชันว่า Search-Mail
- เลือก บัญชีในไดเรกทอรีองค์กรนี้เท่านั้น (EXOIP เท่านั้น - ผู้เช่ารายเดียว)
- คลิก ลงทะเบียน
สร้างแอปพลิเคชัน Search-Mail สำเร็จแล้ว คัดลอกค่าด้านล่างและวางลงใน Notepad เนื่องจากคุณจะต้องใช้ในภายหลังเมื่อเชื่อมต่อกับ Microsoft Graph PowerShell
- คัดลอกรหัสแอปพลิเคชัน (ไคลเอนต์)
- คัดลอกรหัสไดเรกทอรี (ผู้เช่า)
2. กำหนดสิทธิ์ API
กำหนดสิทธิ์ API สำหรับแอปพลิเคชันที่คุณลงทะเบียนในขั้นตอนก่อนหน้า:
- คลิกสิทธิ์ API
- คลิกเพิ่มสิทธิ์
- คลิก Microsoft API
- คลิก Microsoft Graph
- คลิกสิทธิ์ของแอปพลิเคชัน
- ค้นหา User.Read.All
- เลือก ผู้ใช้ > User.Read.All
- ค้นหา Mail.Read
- เลือก เมล > Mail.Read
- คลิก เพิ่มการอนุญาต
เพิ่มสิทธิ์ API เรียบร้อยแล้ว ขั้นตอนต่อไปคือการให้ความยินยอมจากผู้ดูแลระบบ
3. ให้ความเห็นชอบจากผู้ดูแลระบบ
คุณต้องให้ความยินยอมของผู้ดูแลระบบสำหรับสิทธิ์ที่คุณเลือกในขั้นตอนก่อนหน้า:
- คลิก ให้สิทธิ์ผู้ดูแลระบบสำหรับ EXOIP
- คลิกใช่
- เครื่องหมายถูกสีเขียวแสดงว่าคุณได้รับความยินยอมจากผู้ดูแลระบบเรียบร้อยแล้ว
4. สร้างความลับของลูกค้า
หลังจากที่คุณลงทะเบียนแอปพลิเคชันใหม่ใน Microsoft Entra กำหนดสิทธิ์ API และให้ความยินยอมของผู้ดูแลระบบ คุณจะต้องสร้างความลับไคลเอ็นต์
หากต้องการสร้างรหัสลับไคลเอ็นต์สำหรับแอปพลิเคชันของคุณใน Microsoft Entra ID ให้ทำตามขั้นตอนเหล่านี้:
- คลิกใบรับรองและข้อมูลลับ
- คลิก รหัสลับไคลเอ็นต์ > รหัสลับไคลเอ็นต์ใหม่
- พิมพ์ คำอธิบาย
- เลือกวันหมดอายุ
- คลิกเพิ่ม
หมายเหตุ: วันหมดอายุของรหัสลับลูกค้ามีระยะเวลาสูงสุด 24 เดือน (2 ปี) คุณสามารถต่ออายุความลับไคลเอ็นต์ใน Microsoft Entra ID ด้วย PowerShell ได้ตลอดเวลา
- คัดลอกค่ารหัสลับไคลเอ็นต์และบันทึกไว้ใน Notepad
เชื่อมต่อกับ Microsoft Graph PowerShell ด้วยรหัสลับไคลเอ็นต์
คุณต้องเปลี่ยนค่าพารามิเตอร์ด้านล่างเพื่อเชื่อมต่อกับ MS Graph PowerShell ด้วย Client Secret:
- พิมพ์ค่า Application Client ID ใน บรรทัด 2
- พิมพ์ค่า รหัสผู้เช่าไดเรกทอรี ใน บรรทัด 3
- พิมพ์ค่า ค่าความลับไคลเอ็นต์ ใน บรรทัด 4
เรียกใช้สคริปต์ PowerShell ด้านล่าง
# Configuration
$ClientId = "c6787ff5-08ec-4e96-b4d2-1d1782303310"
$TenantId = "eb403171-a4ec-4d98-a08f-1876318c9deb"
$ClientSecret = "6_q8Q~X9e-fjwWhVVP_WWx~ua4JMI.BWkI7Q7b7B"
# Convert the client secret to a secure string
$ClientSecretPass = ConvertTo-SecureString -String $ClientSecret -AsPlainText -Force
# Create a credential object using the client ID and secure string
$ClientSecretCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $ClientId, $ClientSecretPass
# Connect to Microsoft Graph with Client Secret
Connect-MgGraph -TenantId $tenantId -ClientSecretCredential $ClientSecretCredentialค้นหาอีเมลของผู้ใช้ที่เฉพาะเจาะจง
รับข้อความทั้งหมดจากผู้ใช้เฉพาะ
$User = "Amanda.Morgan@exoip.com"
Get-MgUserMessage -All -UserId "$User" |
Select-Object Subject, InternetMessageId, ReceivedDateTime,
@{Name = "Sender"; Expression = { $_.Sender.EmailAddress.Address } },
@{Name = "Recipients"; Expression = { $_.ToRecipients.EmailAddress.Address -join ', ' } } |
Out-GridView
รับข้อความทั้งหมดจากผู้ใช้เฉพาะที่อ่านแล้ว
$User = "Amanda.Morgan@exoip.com"
Get-MgUserMessage -All -UserId "$User" -Filter "IsRead eq true" |
Select-Object Subject, InternetMessageId, ReceivedDateTime,
@{Name = "Sender"; Expression = { $_.Sender.EmailAddress.Address } },
@{Name = "Recipients"; Expression = { $_.ToRecipients.EmailAddress.Address -join ', ' } } |
Out-GridViewรับข้อความทั้งหมดจากผู้ใช้เฉพาะที่ยังไม่ได้อ่าน
$User = "Amanda.Morgan@exoip.com"
Get-MgUserMessage -All -UserId "$User" -Filter "IsRead eq false" |
Select-Object Subject, InternetMessageId, ReceivedDateTime,
@{Name = "Sender"; Expression = { $_.Sender.EmailAddress.Address } },
@{Name = "Recipients"; Expression = { $_.ToRecipients.EmailAddress.Address -join ', ' } } |
Out-GridViewค้นหาอีเมลในวันที่
ค้นหาอีเมลจากผู้ใช้รายใดรายหนึ่งหลังจากวันที่ระบุ
$User = "Amanda.Morgan@exoip.com"
$Date = "2024-01-01"
Get-MgUserMessage -All -UserId "$User" -Filter "ReceivedDateTime gt $Date" |
Select-Object Subject, InternetMessageId, ReceivedDateTime,
@{Name = "Sender"; Expression = { $_.Sender.EmailAddress.Address } },
@{Name = "Recipients"; Expression = { $_.ToRecipients.EmailAddress.Address -join ', ' } } |
Out-GridViewค้นหาอีเมลในเรื่อง
ค้นหาอีเมลที่ขึ้นต้นด้วยหัวเรื่องเฉพาะ
$Subject = "The subject"
$user = "Amanda.Morgan@exoip.com"
Get-MgUserMessage -All -UserId "$User" -Filter "startswith(Subject,'$Subject')" |
Select-Object Subject, InternetMessageId, ReceivedDateTime,
@{Name = "Sender"; Expression = { $_.Sender.EmailAddress.Address } },
@{Name = "Recipients"; Expression = { $_.ToRecipients.EmailAddress.Address -join ', ' } } |
Out-GridViewค้นหาอีเมลที่เท่ากับหัวเรื่อง
$Subject = "The specific subject"
$user = "Amanda.Morgan@exoip.com"
Get-MgUserMessage -All -UserId "$user" -Filter "Subject eq '$Subject'" |
Select-Object Subject, InternetMessageId, ReceivedDateTime,
@{Name = "Sender"; Expression = { $_.Sender.EmailAddress.Address } },
@{Name = "Recipients"; Expression = { $_.ToRecipients.EmailAddress.Address -join ', ' } } |
Out-GridViewค้นหาอีเมลจาก 30 วันที่ผ่านมา
มาค้นหาอีเมลทั้งหมดในองค์กรในช่วง 30 วันที่ผ่านมา ทั้งผู้รับและผู้ส่ง ผลลัพธ์จะปรากฏใน Out-GridView และส่งออกเป็นไฟล์ CSV
ค้นหาอีเมลทั้งหมดในองค์กรจาก 30 วันที่ผ่านมา
# Get all users
$users = Get-MgUser -All
# Create an empty array to store the data
$Data = @()
# Determine the date 30 days prior to today
$startDate = (Get-Date).AddDays(-30).ToString("yyyy-MM-dd")
# Loop through each user
foreach ($user in $users) {
# Ensure the user has an associated email address
if ($user.Mail) {
$messages = Get-MgUserMessage -UserId $user.Id -All -Filter "ReceivedDateTime ge $startDate" -ErrorAction SilentlyContinue
foreach ($message in $messages) {
$Data += [PSCustomObject]@{
ReceivedDateTime = $message.ReceivedDateTime
Subject = $message.Subject
Sender = $message.Sender.EmailAddress.Address -join ','
Recipient = $message.ToRecipients.EmailAddress.Address -join ','
InternetMessageId = $message.InternetMessageId
}
}
}
}
# Display and export the data
$Data | Out-GridView
$Data | Export-Csv -Path "C:\temp\All_emails.csv" -NoTypeInformation -Encoding utf8ค้นหาอีเมลทั้งหมดในองค์กรที่ส่งถึงผู้รับที่ระบุในช่วง 30 วันที่ผ่านมา
# Email address of the recipient you want to filter
$recipientEmailAddress = "amanda.morgan@exoip.com"
# Get all users
$users = Get-MgUser -All
# Create an empty array to store the data
$Data = @()
# Determine the date 30 days prior to today
$startDate = (Get-Date).AddDays(-30).ToString("yyyy-MM-dd")
# Loop through each user
foreach ($user in $users) {
# Ensure the user has an associated email address
if ($user.Mail) {
$messages = Get-MgUserMessage -UserId $user.Id -All -Filter "ReceivedDateTime ge $startDate" -ErrorAction SilentlyContinue
foreach ($message in $messages) {
# Check if the recipient matches the desired email address
if ($message.ToRecipients.EmailAddress.Address -eq $recipientEmailAddress) {
$Data += [PSCustomObject]@{
ReceivedDateTime = $message.ReceivedDateTime
Subject = $message.Subject
Sender = $message.Sender.EmailAddress.Address
Recipient = $message.ToRecipients.EmailAddress.Address
InternetMessageId = $message.InternetMessageId
}
}
}
}
}
# Display and export the data
$Data | Out-GridView
$Data | Export-Csv -Path "C:\temp\Recipient_emails.csv" -NoTypeInformation -Encoding utf8ค้นหาอีเมลทั้งหมดในองค์กรที่ส่งจากผู้ส่งรายใดรายหนึ่งในช่วง 30 วันที่ผ่านมา
# Email address of the sender you want to filter
$senderEmailAddress = "john.doe@gmail.com"
# Get all users
$users = Get-MgUser -All
# Create an empty array to store the data
$Data = @()
# Determine the date 30 days prior to today
$startDate = (Get-Date).AddDays(-30).ToString("yyyy-MM-dd")
# Loop through each user
foreach ($user in $users) {
# Ensure the user has an associated email address
if ($user.Mail) {
$messages = Get-MgUserMessage -UserId $user.Id -All -Filter "ReceivedDateTime ge $startDate" -ErrorAction SilentlyContinue
foreach ($message in $messages) {
if ($message.Sender.EmailAddress.Address -eq $senderEmailAddress) {
$Data += [PSCustomObject]@{
ReceivedDateTime = $message.ReceivedDateTime
Subject = $message.Subject
Sender = $message.Sender.EmailAddress.Address
Recipient = ($message.ToRecipients.EmailAddress.Address -join ',')
InternetMessageId = $message.InternetMessageId
}
}
}
}
}
# Display and export the data
$Data | Out-GridView
$Data | Export-Csv -Path "C:\temp\Sender_emails.csv" -NoTypeInformation -Encoding utf8แค่นั้นแหละ!
อ่านเพิ่มเติม: ส่งออกใบรับรองการลงทะเบียนแอป Entra ID และรายงานการหมดอายุของความลับ »
บทสรุป
คุณได้เรียนรู้วิธีค้นหาอีเมลด้วย PowerShell ขั้นแรก ให้ตั้งค่าแอปพลิเคชันใน Microsoft Entra ด้วยสิทธิ์ที่ถูกต้อง หลังจากนั้นให้เชื่อมต่อกับ Microsoft Graph PowerShell เข้ากับแอปพลิเคชัน สุดท้ายให้รันคำสั่งเพื่อค้นหาอีเมลด้วย PowerShell
คุณสนุกกับบทความนี้หรือไม่? คุณอาจต้องการวิธีบล็อกโดเมนระดับบนสุด (TLD) ใน Microsoft 365 อย่าลืมติดตามเราและแบ่งปันบทความนี้










