新域Exchange服务器邮箱别名自动化创建及变量提取技术求助
Got it, let's tackle this problem—you're trying to automate creating linked mailboxes in your new Exchange environment, but need a reliable way to pull the required user attributes ($Alias, $FullName, $UserAccount, $UserEmailAddress) from your existing domain. Here's a practical, PowerShell-based solution that ties together Active Directory (AD) and Exchange cmdlets:
1. Prerequisites First
Before diving in, make sure:
- You have read access to your source domain's AD.
- The RSAT-AD-PowerShell module is installed on the machine running the script (install via
Install-WindowsFeature RSAT-AD-PowerShellif needed). - You can connect to your new Exchange server's PowerShell endpoint, and have permissions to create mailboxes there.
2. Pulling User Attributes from the Source Domain
We'll use Get-ADUser to fetch the necessary user data. You can run this on a machine joined to the source domain, or specify a remote domain controller and credentials if needed.
Single User Test (For Validation)
Start with a single user to test your variable mapping:
# Import the AD module Import-Module ActiveDirectory # If running outside the source domain, specify DC and credentials $sourceCred = Get-Credential -Message "Enter source domain admin credentials" $sourceDC = "source-dc.yourdomain.com" # Fetch the source user (use SamAccountName or UPN for -Identity) $sourceUser = Get-ADUser -Identity "j.smith" -Server $sourceDC -Credential $sourceCred -Properties GivenName, Surname, UserPrincipalName, SamAccountName, EmailAddress # Map to your required variables (adjust rules to match your company's standards) $Alias = $sourceUser.SamAccountName # Or custom rule, e.g., "$($sourceUser.GivenName[0].ToLower())$($sourceUser.Surname.ToLower())" $FullName = "$($sourceUser.GivenName) $($sourceUser.Surname)" $UserAccount = "$($sourceUser.SamAccountName)@your-source-domain.com" # UPN format works best for linked accounts $UserEmailAddress = $sourceUser.EmailAddress # Or reuse the source UPN if preferred
Batch Processing (For Production)
To automate multiple users, pull all users from a specific OU in the source domain:
# Define your source OU (replace with your actual OU path) $sourceOU = "OU=CorporateUsers,DC=your-source-domain,DC=com" # Fetch all users from the OU $sourceUsers = Get-ADUser -Filter * -SearchBase $sourceOU -Server $sourceDC -Credential $sourceCred -Properties GivenName, Surname, UserPrincipalName, SamAccountName, EmailAddress # Connect to your new Exchange server first (run this once) $exchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://new-exchange.yournewdomain.com/PowerShell/ -Authentication Kerberos Import-PSSession $exchangeSession -DisableNameChecking # Loop through each user to create the linked mailbox foreach ($user in $sourceUsers) { # Apply your alias rule (customize as needed) $Alias = "$($user.GivenName[0].ToLower())$($user.Surname.ToLower())" $FullName = "$($user.GivenName) $($user.Surname)" $UserAccount = "$($user.SamAccountName)@your-source-domain.com" $UserEmailAddress = $user.EmailAddress # Run your New-Mailbox command try { New-Mailbox -alias $Alias -name "$FullName" -database "CompanyDatabase" -org "CompanyMailboxes" -LinkedMasterAccount $UserAccount -LinkedDomainController domain.example.com -UserPrincipalName $UserEmailAddress -ErrorAction Stop Write-Host "✅ Successfully created mailbox for $($user.SamAccountName)" -ForegroundColor Green } catch { Write-Host "❌ Failed to create mailbox for $($user.SamAccountName): $($_.Exception.Message)" -ForegroundColor Red # Log errors to a file for later review Add-Content -Path ".\MailboxCreationErrors.log" -Value "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - $($user.SamAccountName) : $($_.Exception.Message)" } } # Clean up the Exchange session when done Remove-PSSession $exchangeSession
3. Key Notes to Avoid Issues
- Linked Master Account Format: The
-LinkedMasterAccountparameter accepts either the source user's UPN (user@source-domain.com) or the NT format (SOURCE-DOMAIN\user). UPN is more reliable across domains. - Alias Uniqueness: If your alias rule might create duplicates, add a suffix (e.g.,
$Alias$i) and check for existing aliases withGet-Mailbox -Identity $Aliasbefore creating. - Exchange Connection: Make sure your Exchange PowerShell session stays active—if it times out, re-establish it before continuing.
内容的提问来源于stack exchange,提问作者RHSkinner

