Issue with foreach loop and variable

Copper Contributor

Hi all,

 

I have this script I am having trouble with (below).

 

The script gets all our Exchange 2013 databases and iterates through them to return the server they are active on in our DAG.

 

We have 5 databases all currently active on the same server; SERVER1 .  The problem is that the $WhichServer variable returns "SERVER1 SERVER1 SERVER1 SERVER1 SERVER1 " even on the first iteration of the foreach loop.  Everything else works ok.

 

EDIT: here is the output I get

 

0 MailboxDatabase01 - MailboxDatabase01 Active on SERVER1 SERVER1 SERVER1 SERVER1 SERVER1
0 MailboxDatabase02 - MailboxDatabase02 Active on SERVER1 SERVER1 SERVER1 SERVER1 SERVER1
0 MailboxDatabase03 - MailboxDatabase03 Active on SERVER1 SERVER1 SERVER1 SERVER1 SERVER1
0 MailboxDatabase04 - MailboxDatabase04 Active on SERVER1 SERVER1 SERVER1 SERVER1 SERVER1
0 MailboxDatabase05 - MailboxDatabase05 Active on SERVER1 SERVER1 SERVER1 SERVER1 SERVER1

 

Can someone tell me what I am doing wrong?

 

thanks

 

jc

 

$MailboxDBs = Get-MailboxDatabase
foreach ($DB in $MailboxDBs)
{
  $WhichServer = Get-MailboxDatabaseCopyStatus -identity $DB.alias -active | select -expand ActiveDatabaseCopy
  if ($WhichServer -eq "Server1")
  {
    Write-Host "0 $DB - $DB Active on $WhichServer"
  }
  else
  {
    Write-Host "1 $DB - $DB Active on $WhichServer
  }
}

1 Reply
Think I have it.

I changed
-identity $DB.alias
to
-identity $DB.name

This seems to have resolved the issue.