ค้นหาเว็บไซต์

วิธีค้นหาอีเมลด้วย PowerShell


วิธีที่ยอดเยี่ยมในการค้นหาข้อความในกล่องจดหมาย Exchange Online คือการใช้ PowerShell คุณสามารถปรับแต่งคำสั่งตามที่คุณต้องการและรับผลลัพธ์ที่คุณต้องการ นอกจากนี้ การส่งออกผลลัพธ์เป็นไฟล์ CSV ก็ทำได้ดีเยี่ยม ดังนั้นคุณจึงสามารถกรองเข้าไปได้ ในบทความนี้ คุณจะได้เรียนรู้วิธีค้นหาอีเมลด้วย PowerShell

ติดตั้ง Microsoft Graph PowerShell

เรียกใช้ Windows PowerShell ในฐานะผู้ดูแลระบบและติดตั้ง Microsoft Graph PowerShell

Install-Module Microsoft.Graph -Force
Install-Module Microsoft.Graph.Beta -AllowClobber -Force

ข้อสำคัญ: ติดตั้งโมดูล Microsoft Graph PowerShell และ Microsoft Graph Beta PowerShell เสมอ นั่นเป็นเพราะบาง cmdlet ยังไม่พร้อมใช้งานในเวอร์ชันสุดท้าย และจะไม่ทำงาน อัปเดตทั้งสองโมดูลเป็นเวอร์ชันล่าสุดก่อนที่คุณจะเรียกใช้ cmdlet หรือสคริปต์เพื่อป้องกันข้อผิดพลาดและผลลัพธ์ที่ไม่ถูกต้อง

กำหนดค่าการลงทะเบียนแอปใน Microsoft Entra

ก่อนที่คุณจะสามารถรันคำสั่งหรือสคริปต์ได้ คุณต้องสร้างการลงทะเบียนแอปด้วยสิทธิ์ที่ถูกต้องและความลับของไคลเอนต์เพื่อตรวจสอบสิทธิ์กับ Microsoft Graph PowerShell ก่อน

1. ลงทะเบียนใบสมัครใหม่

ลงทะเบียนแอปพลิเคชันใหม่ใน Microsoft Entra ID:

  1. ลงชื่อเข้าใช้ศูนย์การจัดการ Microsoft Entra
  2. คลิก ข้อมูลประจำตัว > แอปพลิเคชัน > การลงทะเบียนแอป
  3. คลิกการลงทะเบียนใหม่

  1. ตั้งชื่อแอปพลิเคชันว่า Search-Mail
  2. เลือก บัญชีในไดเรกทอรีองค์กรนี้เท่านั้น (EXOIP เท่านั้น - ผู้เช่ารายเดียว)
  3. คลิก ลงทะเบียน

สร้างแอปพลิเคชัน Search-Mail สำเร็จแล้ว คัดลอกค่าด้านล่างและวางลงใน Notepad เนื่องจากคุณจะต้องใช้ในภายหลังเมื่อเชื่อมต่อกับ Microsoft Graph PowerShell

  1. คัดลอกรหัสแอปพลิเคชัน (ไคลเอนต์)
  2. คัดลอกรหัสไดเรกทอรี (ผู้เช่า)

2. กำหนดสิทธิ์ API

กำหนดสิทธิ์ API สำหรับแอปพลิเคชันที่คุณลงทะเบียนในขั้นตอนก่อนหน้า:

  1. คลิกสิทธิ์ API
  2. คลิกเพิ่มสิทธิ์

  1. คลิก Microsoft API
  2. คลิก Microsoft Graph

  1. คลิกสิทธิ์ของแอปพลิเคชัน
  2. ค้นหา User.Read.All
  3. เลือก ผู้ใช้ > User.Read.All

  1. ค้นหา Mail.Read
  2. เลือก เมล > Mail.Read
  3. คลิก เพิ่มการอนุญาต

เพิ่มสิทธิ์ API เรียบร้อยแล้ว ขั้นตอนต่อไปคือการให้ความยินยอมจากผู้ดูแลระบบ

3. ให้ความเห็นชอบจากผู้ดูแลระบบ

คุณต้องให้ความยินยอมของผู้ดูแลระบบสำหรับสิทธิ์ที่คุณเลือกในขั้นตอนก่อนหน้า:

  1. คลิก ให้สิทธิ์ผู้ดูแลระบบสำหรับ EXOIP
  2. คลิกใช่

  1. เครื่องหมายถูกสีเขียวแสดงว่าคุณได้รับความยินยอมจากผู้ดูแลระบบเรียบร้อยแล้ว

4. สร้างความลับของลูกค้า

หลังจากที่คุณลงทะเบียนแอปพลิเคชันใหม่ใน Microsoft Entra กำหนดสิทธิ์ API และให้ความยินยอมของผู้ดูแลระบบ คุณจะต้องสร้างความลับไคลเอ็นต์

หากต้องการสร้างรหัสลับไคลเอ็นต์สำหรับแอปพลิเคชันของคุณใน Microsoft Entra ID ให้ทำตามขั้นตอนเหล่านี้:

  1. คลิกใบรับรองและข้อมูลลับ
  2. คลิก รหัสลับไคลเอ็นต์ > รหัสลับไคลเอ็นต์ใหม่
  3. พิมพ์ คำอธิบาย
  4. เลือกวันหมดอายุ
  5. คลิกเพิ่ม

หมายเหตุ: วันหมดอายุของรหัสลับลูกค้ามีระยะเวลาสูงสุด 24 เดือน (2 ปี) คุณสามารถต่ออายุความลับไคลเอ็นต์ใน Microsoft Entra ID ด้วย PowerShell ได้ตลอดเวลา

  1. คัดลอกค่ารหัสลับไคลเอ็นต์และบันทึกไว้ใน 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 อย่าลืมติดตามเราและแบ่งปันบทความนี้

บทความที่เกี่ยวข้อง