Need help to disable Exchange 2013 Mailbox using Primarysmtpaddress

This topic contains 5 replies, has 2 voices, and was last updated by Profile photo of Shashank Shashank 2 days, 20 hours ago.

  • Author
    Posts
  • #73156
    Profile photo of Shashank
    Shashank
    Participant

    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

  • #73220
    Profile photo of Wilm Reiche
    Wilm Reiche
    Participant

    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

    • #73258
      Profile photo of Shashank
      Shashank
      Participant

      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

  • #73297
    Profile photo of Wilm Reiche
    Wilm Reiche
    Participant

    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.

    • #73310
      Profile photo of Shashank
      Shashank
      Participant

      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.

    • #73345
      Profile photo of Shashank
      Shashank
      Participant

      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

You must be logged in to reply to this topic.