Termination Script

I have a termination script that I am revising and need a little advice.

Once all the mailbox exports have finished I need to disable the mailboxes where the export was completed successfully. There may be two exports for a single user if they have an online archive and both have to have the status of completed. This is what I have so far.

The collection is $Terminations and contains the following information

Sip (msRTCSIP-PrimaryUserAddress)
Mailbox (email address)
Sam (samaccountname)
identity (mailbox identity)

Mailbox export requests return this information:

Name = MailboxExport or MailboxExport1 and so on
Mailbox: fqdn/xxxx/xxx/last name, first name
Status: Completed, Failed

Here is what I have so far. But I am not sure if I have done the conditions right. What I need: If MailboxExport1 exists for the user, mailboxexport and mailboxexport1 must have the status ‘completed’ If there is no mailboxexport1, then it would go to the else statement.

foreach ($i in $Terminations)
{

if (MailboxExportRequest -Name MailBoxExport1)
{where MailboxExportRequest -Name MailboxExport -Status Completed -and MailboxExportRequest -Name MailboxExport1 -Status Completed -and $Mailbox -eq $Terminations.identity

disable-mailbox $Terminations.mailbox

else

where MailboxExportRequest -Name MailboxExport -Status Completed -and $Mailbox -eq $Terminations.identity

disable-mailbox $Terminations.mailbox

}
}

Thanks in advance for all suggestions

I’ve been staring at this for a bit now and I’m not following your logic. Like, I don’t see what $Mailbox is even set to. I don’t see where you’re using $i. So I’m not sure what’s supposed to be happening.

foreach ($i in $Terminations)

$Terminations should include more than one thing. Within the ForEach loop, you should be referring to $i to refer to an individual thing. I don’t see you doing that; you’re still referring to $Terminations, which could contain multiple items.

The syntax you’re using for Where-Object won’t work. That’s the “simplified” syntax, and it doesn’t support Boolean operators like -and. Also, you seem to have the same condition listed twice. I’d suggest turning this whole thing into a more logical If construct:

if (  (condition-1)  -and  (condition-2)  ) {
  Disable-Mailbox $i.mailbox
}

I’m assuming you meant to refer to just $1 mailbox, here. “$Terminations.mailbox” will either return nothing, or will return ALL of the mailboxes, which is not what I think you want.

Does that help get you moving in the right direction?

Apologies. This is a piece to be added to a larger script. I am still very new to powershell and just really starting to get into more complex things. I currently have a termination script that imports a csv file into $Mailboxes. It brings in the sAMAccountname for the individuals being terminated and column headers for mail, sip and identity. An initial foreach loop in the script populates $Mailboxes with the information for mail, sip and identity.

The piece I am working on is the logical conclusion to the script and that is to disable the mailboxes for the items that have been successfully exported. This is the piece that I put into the question.

The Get-MailboxExportRequest returns all mailboxexports. If the individual has an online archive you get a mailboxexport and a mailboxexport1 for that individual, I am sure you know all of this already, but you don’t know that I know it ;). What I need to be able to do is take the return from Get-MailboxExportRequest and capture those requests where the Mailboxexport and Mailboxexport1 (if it exists for that user) have both completed successfully. Then I can filter that down to just show a single line for each individual using your syntax above.

Get-MailboxExportRequest returns the name of the Export (mailboxexport and mailboxexport1 or 2 etc), The identity of the mailbox and the status. It does not contain any value that can be passed to the disable-mailbox command. What I am trying to do is use a foreach loop to compare each identity returned by the Get-MailboxExportRequest to the identity in the $Mailboxes and retrieve either the email or samaccount name for that identity and pass it to disable-mailbox. This way it will loop through and disable the mailboxes for those entries where the export was successful.

I know that I am not quite getting what the logic and syntax should be to accomplish this. I have tried several different ways, the first being what I posted above. I also want to end up with this being a script or tool that anyone can use where the file for the import is not specified in the script itself but specified as part of the command to run it. This way it can be used regardless of where the csv file actually resides as it won’t be hardcoded in the script.

I think the tips I wrote above should help. You need to remember that if a variable contains more than one thing, you’ll usually have to enumerate (ForEach) it so that you can get one at a time.

I am still struggling even with your suggestions to figure out how to iterate through one collection and compare a value from there to a list of values in a second collection and when a match is found, get an associated value.

Maybe something like this, understanding that $Mailboxes has already been created and is in the mix.

$Terminations = get-mailboxexportrequest -status completed | select-object -ExpandProperty Mailbox (This gives me just the mailbox (identity value) instead of the other pieces of data)

foreach ($entry in $Terminations)

{
foreach ($user in $Mailboxes)

      $item = $user.mail | Where {$User.identity -eq $entry | disable-mailbox $item



  }

}

Sort of.

You’re using $user properly - $user.mail will be the mail of the current mailbox. I don’t know what $Terminations contains, specifically. If it’s an object, then $entry will be the entire object. If $entry is just a string - and is the user’s identity - then you logic should work.

There are probably some other ways. For example…

# $Terminations is a collection of strings
foreach ($mailbox in $mailboxes) {
  if ($mailbox.identity -in $terminations) {
    # the value in $mailbox.identity is also present in $terminations
  }
}

The -in and -contains operators can check to see if one object exists in a collection of other objects. Under the hood, it’s more or less enumerating that $Terminations collection for you - just faster and with less coding on your part.

if ($terminations -contains $mailbox.identity)

Would accomplish the same thing, just approaching the logic from the opposite direction.

Thank you, I think I have it now. I appreciate all of your help.