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

เปลี่ยนที่อยู่นามแฝงเป็นที่อยู่ SMTP หลักด้วย PowerShell


คุณมีที่อยู่ SMTP หลักและที่อยู่นามแฝงสำหรับชุดกล่องจดหมายแต่ละชุดอยู่แล้ว อย่างไรก็ตาม งานคือการสลับที่อยู่นามแฝงกับที่อยู่ SMTP หลัก วิธีที่ดีที่สุดในการทำเช่นนี้คือใช้ PowerShell ในบทความนี้ คุณจะได้เรียนรู้วิธีเปลี่ยนที่อยู่นามแฝงเป็นที่อยู่ SMTP หลักใน Exchange Server ด้วย PowerShell

การแนะนำ

สคริปต์ PowerShell Set-PrimaryAddress.ps1 ใช้งานได้กับ:

  • แลกเปลี่ยนในสถานที่
  • แลกเปลี่ยนไฮบริด

หากคุณต้องการทำเช่นเดียวกันสำหรับสภาพแวดล้อม Exchange Online (Microsoft 365) ให้อ่านบทความ เปลี่ยนที่อยู่นามแฝง Microsoft 365 เป็นที่อยู่ SMTP หลักด้วย PowerShell

หมายเหตุ: ที่อยู่ smtp ของนามแฝงจะกลายเป็นที่อยู่ SMTP หลัก และที่อยู่ SMTP หลักจะกลายเป็นที่อยู่นามแฝง ดังนั้นที่อยู่ SMTP หลักจะไม่ถูกลบออก มันเพิ่งถูกสลับกับที่อยู่ smtp ของนามแฝง

ก่อนที่คุณจะเริ่ม

จะเกิดอะไรขึ้นหากไม่มีที่อยู่นามแฝงที่กำหนดไว้สำหรับกล่องจดหมายทุกกล่อง? วิธีที่ดีที่สุดคืออ่านบทความเพิ่มที่อยู่ SMTP รองจำนวนมากด้วย PowerShell

เมื่อคุณตรวจสอบแล้วว่าที่อยู่นามแฝงถูกตั้งค่าสำหรับกล่องจดหมายทั้งหมดแล้ว ให้เรียกใช้สคริปต์ PowerShell Set-PrimaryAddress.ps1 จากบทความนี้เพื่อสลับที่อยู่นามแฝงกับที่อยู่ SMTP หลัก

สมมติว่าคุณทำเสร็จแล้วและต้องการลบที่อยู่ SMTP หลักเก่าที่ตอนนี้เป็นที่อยู่นามแฝง คุณสามารถอ่านบทความการลบที่อยู่ SMTP รองจำนวนมากด้วย PowerShell

การทำเช่นนี้เป็นขั้นตอนย่อมดีกว่าการใช้สคริปต์ตัวเดียวที่ทำทุกอย่างทันที เพื่อให้คุณมั่นใจว่าทุกอย่างถูกต้องตั้งค่าไว้

เตรียมสคริปต์ PowerShell ที่อยู่ SMTP หลัก

ดาวน์โหลดสคริปต์ Set-PrimaryAddress.ps1 PowerShell หรือคัดลอกและวางโค้ดด้านล่างลงใน Notepad ตั้งชื่อให้ว่า Set-PrimaryAddress.ps1 และวางไว้ใน โฟลเดอร์ C:\scripts สร้างโฟลเดอร์ สคริปต์ หากคุณยังไม่มี

<#
    .SYNOPSIS
    Set-PrimaryAddress.ps1

    .DESCRIPTION
    Get the secondary (alias) address from a specified domain and set it as the primary SMTP address
    for all mailbox users in Exchange Server and Exchange Hybrid. If there is no alias address
    set with the specified domain, it will skip the user and display a message. The primary SMTP address
    will become an alias address and all the secondary email address will remain.

    .LINK
    www.alitajran.com/change-alias-address-to-primary-smtp-address/

    .NOTES
    Written by: ALI TAJRAN
    Website:    alitajran.com
    X:          x.com/alitajran
    LinkedIn:   linkedin.com/in/alitajran

    .CHANGELOG
    V1.00, 06/04/2024 - Initial version
    V1.10, 09/03/2024 - Added parameters
#>

param (
    [Parameter(Mandatory = $true)]
    [string]$DomainName,
    [switch]$WhatIf,
    [switch]$Remote
)

# Output will be added to C:\temp folder. Open the log with a text editor.
Start-Transcript -Path C:\temp\Set-Primary-SMTP-Address.log -Append

# Ensure the domain starts with '@'
$PrimarySMTPDomain = "@" + $DomainName

# Choose the appropriate cmdlets based on the switch
if ($Remote) {
    $GetMailboxCmdlet = "Get-RemoteMailbox"
    $SetMailboxCmdlet = "Set-RemoteMailbox"
}
else {
    $GetMailboxCmdlet = "Get-Mailbox"
    $SetMailboxCmdlet = "Set-Mailbox"
}

# Get all mailbox users
$users = Invoke-Expression "$GetMailboxCmdlet -ResultSize Unlimited"

foreach ($user in $users) {
    $currentPrimarySMTP = $user.PrimarySmtpAddress.ToString()
    $aliasAddresses = $user.EmailAddresses | Where-Object { $_ -clike "smtp*" -and $_ -clike "*$PrimarySMTPDomain" }

    # Check if the current primary SMTP address ends with the specified domain
    if ($currentPrimarySMTP -like "*$PrimarySMTPDomain") {
        Write-Host "Skipping $($user.DisplayName) - Primary SMTP already ends with $PrimarySMTPDomain" -ForegroundColor Yellow
    }
    elseif ($aliasAddresses.Count -eq 1) {
        $newPrimarySMTP = $aliasAddresses -replace "smtp:", ""
        Write-Host "Updating primary SMTP for $($user.DisplayName) to $newPrimarySMTP" -ForegroundColor Green

        # Set the new primary SMTP address
        if ($WhatIf) {
            Invoke-Expression "$SetMailboxCmdlet -Identity '$($user.Identity)' -PrimarySmtpAddress '$newPrimarySMTP' -EmailAddressPolicyEnabled `$false -WhatIf"
        }
        else {
            Invoke-Expression "$SetMailboxCmdlet -Identity '$($user.Identity)' -PrimarySmtpAddress '$newPrimarySMTP' -EmailAddressPolicyEnabled `$false"
        }
    }
    elseif ($aliasAddresses.Count -eq 0) {
        Write-Host "No alias address found for $($user.DisplayName) - Primary SMTP not updated" -ForegroundColor Cyan
    }
    else {
        Write-Host "Multiple alias addresses found for $($user.DisplayName) - Primary SMTP not updated" -ForegroundColor Red
    }
}

Stop-Transcript

ตั้งค่าสคริปต์ PowerShell ที่อยู่ SMTP หลักจำนวนมาก

สคริปต์นี้ใช้งานได้กับ Exchange on-premises และสภาพแวดล้อม Exchange Hybrid จำเป็นอย่างยิ่งที่คุณจะต้องเรียกใช้ Exchange Management Shell ในฐานะผู้ดูแลระบบ

ไปที่เส้นทางสคริปต์และเรียกใช้สคริปต์ Set-PrimaryAddress.ps1 สคริปต์จะผ่านกล่องจดหมายทั้งหมดในองค์กร Exchange

C:\scripts\.\Set-PrimaryAddress.ps1 -DomainName "exoip.nl" -WhatIf

สมมติว่าคุณต้องการกำหนดเป้าหมายกล่องจดหมายระบบคลาวด์ในสภาพแวดล้อม Exchange Hybrid คุณต้องเพิ่มพารามิเตอร์ -Remote สิ่งนี้จะเปลี่ยนจาก Get-Mailbox เป็น Get-RemoteMailbox เพื่อดึงกล่องจดหมายคลาวด์ทั้งหมด และ Set-Mailbox จะถูกเปลี่ยนเป็น ตั้งค่า-RemoteMailbox เพื่อใช้การเปลี่ยนแปลงกับกล่องจดหมายระบบคลาวด์

C:\scripts\.\Set-PrimaryAddress.ps1 -DomainName "exoip.nl" -Remote -WhatIf

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

C:\scripts\.\Set-PrimaryAddress.ps1 -DomainName "exoip.nl"

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

ในตัวอย่างนี้ ที่อยู่นามแฝงที่มีอยู่ซึ่งมีโดเมน @exoip.nl จะถูกตั้งค่าเป็นกลุ่มเป็นที่อยู่ SMTP หลัก

Transcript started, output file is C:\temp\Set-Primary-SMTP-Address.log
Multiple alias addresses found for Alysia Maverick - Primary SMTP not updated
No alias address found for Boris Campbell - Primary SMTP not updated
Updating primary SMTP for  Christopher Payne to Christopher.Payne@exoip.nl
No alias address found for Discovery Search Mailbox - Primary SMTP not updated
Updating primary SMTP for  James Paterson to James.Paterson@exoip.nl
No alias address found for Fraser, Max - Primary SMTP not updated
Updating primary SMTP for Nicholas Murray to Nicholas.Murray@exoip.nl
No alias address found for Richard Hunter - Primary SMTP not updated
No alias address found for sharedmailboxonprem - Primary SMTP not updated
Transcript stopped, output file is C:\temp\Set-Primary-SMTP-Address.log

ขณะนี้ที่อยู่นามแฝง smtp ที่มีอยู่เป็นที่อยู่ SMTP หลักสำหรับผู้ใช้ทั้งหมด

อ่านเพิ่มเติม: ส่งจากนามแฝงใน Office 365 »

บทสรุป

คุณได้เรียนรู้วิธีตั้งค่าที่อยู่นามแฝงเป็นกลุ่มเป็นที่อยู่ SMTP หลักด้วย PowerShell ขั้นแรก ให้ดาวน์โหลดสคริปต์ Set-PrimaryAddress PowerShell จากนั้น เพิ่มโดเมนในคำสั่งที่คุณต้องการให้ค้นหาในที่อยู่นามแฝงของกล่องจดหมาย ในที่สุดให้รันสคริปต์ อย่าลืมทดสอบก่อนด้วยพารามิเตอร์ -WhatIf

คุณสนุกกับบทความนี้หรือไม่? คุณอาจต้องการสร้างกล่องจดหมายที่ใช้ร่วมกันด้วยนามแฝงเดียวกัน อย่าลืมติดตามเราและแบ่งปันบทความนี้

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