0

I'm seeing output issues within the 'Not Found' column, even when DisplayName is not null it's marking mailbox as 'Mailbox Not Found'. Seems ok for the first 5 output lines until until it hits the first 'Cloud' DisplayName then it fills every cell with 'Mailbox Not Found' in the 'Not Found' column.
****if (($mb1 -eq 'No Cloud Mailbox') -and ($mb2 -eq 'No Onprem Mailbox')) { $mb3 = 'Mailbox Not Found' }****

Am I missing something? Any help would be appreciated.

#========
#Get date
#========

$date = Get-Date -format dd-MM-yy

#===========================
#Setting up global variables
#===========================

$allmbadinfo = @()
$mbadinfo = @()
$users = Get-Content D:\import\allrgs.txt

#=================
#Grab mailbox info
#=================


Foreach ($user in $users ) {

      $mb1 = Get-RemoteMailbox $User

          if ($mb1 -ne $null) { $mb1 = $mb1.DisplayName }

                    else {
                          $mb1 = 'No Cloud Mailbox' }


      $mb2 = Get-Mailbox $User

           if ($mb2 -ne $null) { $mb2 = $mb2.DisplayName }

                    else {
                          $mb2 = 'No Onprem Mailbox' }


           if (($mb1 -eq 'No Cloud Mailbox') -and ($mb2 -eq 'No Onprem Mailbox')) { $mb3 = 'Mailbox Not Found' }


       $imputlist = Write-Output $user


#================================================================================================
#Create new array object and populate information from variables, add table column names and data
#================================================================================================

      $mbadinfo = New-Object PSObject
      $mbadinfo | Add-Member -MemberType 'NoteProperty' -Name 'Cloud' -Value $mb1
      $mbadinfo | Add-Member -MemberType 'NoteProperty' -Name 'Onprem' -Value $mb2
      $mbadinfo | Add-Member -MemberType 'NoteProperty' -Name 'Not Found' -Value $mb3
      $mbadinfo | Add-Member -MemberType 'NoteProperty' -Name 'Imput List' -Value $imputlist
      $allmbadinfo += $mbadinfo

      }

      #======================
      # Exporting data to csv
      #======================

      $allmbadinfo | Export-Csv D:\export\remotembxrgs-$date.csv -NoType -NoClobber
1
  • Do $mb1 and $mb2 get successfully set to null when no mailbox is found in all cases? Your condition should actually be $null -ne $mb1. Commented May 18, 2020 at 17:11

1 Answer 1

1

The code will become clearer if you introduce a few more variables so you are not re-using $mb1, $mb2 and $mb3.
The main problem with that is that once your variable $mb3 has been set to 'Mailbox Not Found', it will keep that value forever, since your last if does not state what it should become when the condition is not met.

Try this:

#================================
#Get date and read the users file
#================================

$date  = Get-Date -format dd-MM-yy
$users = Get-Content D:\import\allrgs.txt

#=================
#Grab mailbox info
#=================

$allmbadinfo = foreach ($user in $users ) {
    # test for remote mailbox
    $mb1 = Get-RemoteMailbox $user -ErrorAction SilentlyContinue
    $remote = if ($mb1) { $mb1.DisplayName } else { 'No Cloud Mailbox' }

    # test for on premise mailbox
    $mb2 = Get-Mailbox $user -ErrorAction SilentlyContinue
    $onPrem = if ($mb2) { $mb2.DisplayName } else { 'No Onprem Mailbox' }

    # if both are $null, set this to 'Mailbox Not Found'
    $noMailBox = if (!$mb1 -and !$mb2) { 'Mailbox Not Found' } else { '' }


#================================================================================================
# Create new array object and populate information from variables, add table column names and data
#================================================================================================

    # for Powershell v3.0 and newer:
    # output an object to get collected in variable $allmbadinfo
    [PsCustomObject]@{
        'Cloud'           = $remote
        'Onprem'          = $onPrem
        'Not Found'       = $noMailBox
        'Input List item' = $user
    }

    # for PowerShell versions less than 3.0:
    # $mbadinfo = New-Object PSObject
    # $mbadinfo | Add-Member -MemberType 'NoteProperty' -Name 'Cloud' -Value $remote
    # $mbadinfo | Add-Member -MemberType 'NoteProperty' -Name 'Onprem' -Value $onPrem
    # $mbadinfo | Add-Member -MemberType 'NoteProperty' -Name 'Not Found' -Value $noMailBox
    # $mbadinfo | Add-Member -MemberType 'NoteProperty' -Name 'Input List item' -Value $user

    # output this object to get collected in variable $allmbadinfo
    # $mbadinfo

}

#======================
# Exporting data to csv
#======================

$allmbadinfo | Export-Csv "D:\export\remotembxrgs-$date.csv" -NoType -NoClobber
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the response. I picked up a few tips and tricks from your reply hence why I wanted to put the code together first then post it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.