try { $serverAddress = Get-Content -Path ".\DefaultServer.txt" $credential = Import-Clixml -Path ".\AdminCredential.xml" $session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://$serverAddress/PowerShell/ -Authentication Kerberos -Credential $credential Import-PSSession $session # Parameters from batch script $mailboxName = $args[0] $mailboxAlias = $args[1] # Create the shared mailbox New-Mailbox -Shared -Name $mailboxName -Alias $mailboxAlias -PrimarySmtpAddress "$mailboxAlias@windows.local" -OrganizationalUnit "OU=RealUsers,DC=windows,DC=local" Write-Host "Shared mailbox created successfully!" -ForegroundColor Green # Ask if the user wants to add an additional email address $addEmail = Read-Host "Do you want to set up an additional email address? (yes/no)" if ($addEmail -eq "yes") { $additionalEmail = Read-Host "Enter the additional email address" $makeReplyAddress = Read-Host "Do you want this to be the reply address? (yes/no)" # Add the additional email address Set-Mailbox -Identity $mailboxAlias -EmailAddresses @{add="$additionalEmail"} Write-Host "Additional email address added successfully!" -ForegroundColor Green # Set the additional email as the primary SMTP address if required if ($makeReplyAddress -eq "yes") { Set-Mailbox -Identity $mailboxAlias -PrimarySmtpAddress $additionalEmail Write-Host "The additional email address has been set as the reply address." -ForegroundColor Green } # Retrieve mailbox details to display email addresses $mailboxDetails = Get-Mailbox -Identity $mailboxAlias # Display the primary SMTP address from the PrimarySmtpAddress property Write-Host "Here are the current email addresses for this mailbox:" -ForegroundColor Yellow Write-Host "Primary SMTP Address:" -ForegroundColor Green Write-Host $mailboxDetails.PrimarySmtpAddress -ForegroundColor Cyan # Filter out the primary SMTP address from the EmailAddresses property $otherAliases = $mailboxDetails.EmailAddresses | Where-Object { $_ -notlike "SMTP:$($mailboxDetails.PrimarySmtpAddress)" } # Display other aliases if ($otherAliases -and $otherAliases.Count -gt 0) { Write-Host "Other Aliases:" -ForegroundColor Green foreach ($alias in $otherAliases) { Write-Host $alias -ForegroundColor Cyan } } else { Write-Host "No additional aliases found." -ForegroundColor Cyan } } Remove-PSSession $session } catch { Write-Host "Failed to create shared mailbox. Error:" -ForegroundColor Red Write-Host $_ }