Hi,
I am trying to create a script which will disable mailbox in Exchange 2013 based on Primarysmtpaddress.
I can disable users with the below script:
import-csv “D:\Script\Disable Mailbox\MBXDisable.csv” | foreach {Disable-Mailbox $_.Mailbox -confirm:$false}
But the problem with the above script is if we add an alias to another mailbox , it would disable the other mailbox too.
For Example:
User 1 has domain ID : ABC and Email ID : ABC@test.com
User 2 has domain ID: XYZ and Email ID: XYZ@test.com
ABC@test.com Exchange Mailbox gets disabled
Alias ABC@test.com is added to XYZ@test.com as we want mails marked to ABC@test.com to get delivered to XYZ@test.com
Somehow if the script has an input of an email address for ABC@test.com ,it would disable the mailbox for XYZ@test.com also which is an alias mailbox.
I have narrowed down the below script werein I can get the output based on paramter “IsPrimaryAddress”
Get-Mailbox -Identity XYZ@test.com | select -ExpandProperty emailaddresses | ?{$.IsPrimaryAddress -eq “True” -and $.PrefixString -eq “SMTP”}
The output of the above command would be as follows:
SmtpAddress : XYZ@test.com
AddressString : XYZ@test.com
ProxyAddressString : SMTP:XYZ@test.com
Prefix : SMTP
IsPrimaryAddress : True
PrefixString : SMTP
The script should confirm the conditions {$.IsPrimaryAddress -eq “True” -and $.PrefixString -eq “SMTP”} before disabling the mailbox.
Regards,
Shashank
Hi Shashank,
my guess is that $_.Mailbox contains the SMTP address:
Option 1
$UserList= import-csv “D:\Script\Disable Mailbox\MBXDisable.csv”
foreach($user in $UserList){
if(Get-Mailbox $user.Mailbox | where { $_.primarysmtpaddress -eq $user.Mailbox }){
Disable-Mailbox $user.Mailbox -confirm:$false
}
}
Option 2 (normally the WindowsEmailAddress is equal to the PrimarySmtpAddress)
$UserList= import-csv “D:\Script\Disable Mailbox\MBXDisable.csv”
foreach($user in $UserList){
if(Get-Mailbox -Filter “windowsemailaddress -eq $user.Mailbox”){
Disable-Mailbox $user.Mailbox -confirm:$false
}
}
Option 3 (one liner)
import-csv “D:\Script\Disable Mailbox\MBXDisable.csv” | % { Get-Mailbox $user.Mailbox | ? primarysmtpaddress -eq $user.Mailbox | disable-mailbox }
Wilm
Hi Wilm,
Thank you for taking some time out and replying to my query.I have checked all the three options .
I added “test01@test.com” as an alias to one of the user accounts.
CSV file contained the below ID’s:
mstest5@test.com
test01@test.com
mstest4@test.com
Options 1 and 3 executed but the mailboxes didnt get disabled.
Option 2 gave the below error:
Cannot bind parameter ‘Filter’ to the target. Exception setting “Filter”: “Invalid filter syntax. For a description of
the filter parameter syntax see the command help.
“windowsemailaddress -eq @{mstest5@test.com=test01@test.com}.Mailbox” at position 27.”
+ CategoryInfo : WriteError: (
[Get-Mailbox], ParameterBindingException
+ FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.Exchange.Management.RecipientTasks.GetMailbox
+ PSComputerName : server.country.test.com
Cannot bind parameter ‘Filter’ to the target. Exception setting “Filter”: “Invalid filter syntax. For a description of
the filter parameter syntax see the command help.
“windowsemailaddress -eq @{mstest5@test.com=mstest4@test.com}.Mailbox” at position 27.”
+ CategoryInfo : WriteError: (
[Get-Mailbox], ParameterBindingException
+ FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.Exchange.Management.RecipientTasks.GetMailbox
+ PSComputerName : server.country.test.com
Hi Shashank,
I don’t quite know what happened to the data there. Is your CSV just a simple list of SMTP addesses? Then you might consider using get-content and change $user.Mailbox to $user in the script.
Hi Wilm,
I would try the below then:
Option 2
$UserList= get-content “D:\Script\Disable Mailbox\MBXDisable.csv”
foreach($user in $UserList){
if(Get-Mailbox -Filter “windowsemailaddress -eq $user”){
Disable-Mailbox $user -confirm:$false
}
}
I will share the results with you in an hour.
Hi Wilm,
Got the below error after running the script:
Cannot bind parameter ‘Filter’ to the target. Exception setting “Filter”: “Invalid filter syntax. For a description of
the filter parameter syntax see the command help.
“windowsemailaddress -eq mstest5@test.com” at position 25.”
+ CategoryInfo : WriteError: (
[Get-Mailbox], ParameterBindingException
+ FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.Exchange.Management.RecipientTasks.GetMailbox
+ PSComputerName : server.country.test.com
Can someone try to help me with script?
Ah, it needs quoted around the email address. Try:
$UserList= get-content “D:\Script\Disable Mailbox\MBXDisable.csv”
foreach($user in $UserList){
if(Get-Mailbox -Filter “windowsemailaddress -eq ‘$user’”){
Disable-Mailbox $user -confirm:$false
}
}
Hi Wilm,
Thank you for the script.
I would try it tomorrow and share the results.
Thanks Wilm,
Script worked fine.
Just an update , I have modified a bit to get the alias email ID or Email ID that doesnt exists in a text file:
$UserList= get-content “D:\Script\New_DisableMBX\Disable_Mailbox.csv”
foreach($user in $UserList)
{
if(Get-Mailbox -Filter “windowsemailaddress -eq ‘$user’”)
{
Disable-Mailbox $user -confirm:$false
}
else
{
echo $user > “D:\Script\New_DisableMBX\alias.txt”
}
}