none
Changing the output of a PowerShell script RRS feed

  • Question

  • I created a PowerShell script that looks at Exchange mailbox database health and returns a 1 for healthy/mounted and a 0 for suspended.  The script works well; trouble is some of our Exchange server have many databases so the output can be up to 8 number 1's.
    I would like to return a single number 1 if ANY mailbox database is not healthy/mounted on a server.
    I'm stuck.

    Script:

    Any suggestions would be great!


    #Get PS version
    #$PSVersion = $Host.Version.Major
      Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
    . $env:ExchangeInstallPath\bin\RemoteExchange.ps1
    Connect-ExchangeServer -auto



    Get-MailboxDatabaseCopyStatus | Foreach-Object {

          If ($_.Status.ToString() -eq "Mounted" -or $_.ContentIndexState.ToString() -eq "Healthy")

          {

                # Write-Host "$($_.Name) - $($_.Status) - $($_.ContentIndexState) - 1" -ForegroundColor Green
                # Write-Output "$($_.Name) - $($_.Status) - $($_.ContentIndexState) - 1"

    # $status = 1 is Mounted and Healthy
    $status = 1
       
          }

          Else

          {

              # Write-Host "$($_.Name) - $($_.Status) - $($_.ContentIndexState) - 0" -ForegroundColor red
              # Write-Output "$($_.Name) - $($_.Status) - $($_.ContentIndexState) - 0"

    # $Status = 0 is not Mounted and Suspended
    # $status = 0
          }

    Monday, March 23, 2020 6:59 PM

All replies

  • The first thing you should do is to use the formatting tool on the website when posting your script. The "Insert Code Block" icon. 

    I would like to return a single number 1 if ANY mailbox database is not healthy/mounted

    This code is not returning anything. It is only setting a variable name $status to 1 if "mounted OR healthy" are true. That is the opposite of what you asked for in your question. The "not healthy/mounted" part. 

    I'm not an Exchange person, but I'm a bit confused about mounted and healthy. The IF statement and comments do not match. You test for OR but comment that you see AND.

    ($_.Status.ToString() -eq "Mounted" -or $_.ContentIndexState.ToString() -eq "Healthy")
    # $status = 1 is Mounted and Healthy

    An unhealthy mounted DB will have the same status as a healthy mounted DB. Is that what you want?  

    The statement that sets $status to 0 is commented out. You don't include the code that shows how the $status variable is initialized or what you do with it after this code runs.

    Maybe did you really want to analyze all mounted DB's and test to see if their indexes were healthy?

    Something like this?

    $status = 0                     # our default status that all DB's are ok 
    
    Get-MailboxDatabaseCopyStatus | Foreach-Object {
    
          If ($_.Status.ToString() -eq "Mounted")  
          {
                if ($_.ContentIndexState.ToString() -eq "Healthy") 
                {
                     # We have a healthy index on a mounted DB. 
                }
                else 
                { 
                    $status = 1        # we have a mounted DB that is not healthy
                }
          }
          Else
          {
                # add code to analyze un-mounted DB's here
          }
    }
    return $status          # return what we found 



    • Edited by MotoX80 Monday, March 23, 2020 9:25 PM
    Monday, March 23, 2020 7:59 PM
  • Hi,

    Do you mean you just want to know which mailbox database is dismounted and unhealthy?

    If so, I made some modification on your script, and you can use this:

    Get-MailboxDatabaseCopyStatus | Foreach-Object { If ($_.Status.ToString() -eq "Mounted" -or $_.ContentIndexState.ToString() -eq "Healthy") { #Write-Host "$($_.Name) - $($_.Status) - $($_.ContentIndexState) - 1" -ForegroundColor Green #Write-Output "$($_.Name) - $($_.Status) - $($_.ContentIndexState) - 1" # $status = 1 is Mounted and Healthy # $status = 1 } Else { Write-Host "$($_.Name) - $($_.Status) - $($_.ContentIndexState) - 0" -ForegroundColor red #Write-Output "$($_.Name) - $($_.Status) - $($_.ContentIndexState) - 0" # $Status = 0 is not Mounted and Suspended # $status = 0 }}

    I use it in my EMS, here is the test result:

    You also can use the following command to find the unhealthy or dismounted database:

    Get-MailboxDatabaseCopyStatus|where {$_.Status -ne "Mounted" -or $_.ContentIndexState -ne "Healthy"}

    Regards,

    Lydia Zhou


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.

    Tuesday, March 24, 2020 3:00 AM
    Moderator
  • Just checking in to see if above information was helpful. If you have any questions or need further help on this issue, please feel free to post back. 

    Regards,

    Lydia Zhou


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.

    Friday, March 27, 2020 8:50 AM
    Moderator
  • Any updates so far? If you have solved your problem, could you share with us? Maybe it will help more people with similar problems. 

    Regards,

    Lydia Zhou


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.

    Thursday, April 2, 2020 1:03 AM
    Moderator