Issue with foreach loop and variable

%3CLINGO-SUB%20id%3D%22lingo-sub-746524%22%20slang%3D%22en-US%22%3EIssue%20with%20foreach%20loop%20and%20variable%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-746524%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20all%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20this%20script%20I%20am%20having%20trouble%20with%20(below).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20script%20gets%20all%20our%20Exchange%202013%20databases%20and%20iterates%20through%20them%20to%20return%20the%20server%20they%20are%20active%20on%20in%20our%20DAG.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWe%20have%205%20databases%20all%20currently%20active%20on%20the%20same%20server%3B%20%3CFONT%3ESERVER1%20%3C%2FFONT%3E.%26nbsp%3B%20The%20problem%20is%20that%20the%20%24WhichServer%20variable%20returns%20%22%3CFONT%3ESERVER1%20%3C%2FFONT%3E%3CFONT%3ESERVER1%20%3C%2FFONT%3E%3CFONT%3ESERVER1%20%3C%2FFONT%3E%3CFONT%3ESERVER1%20%3C%2FFONT%3E%3CFONT%3ESERVER1%20%3C%2FFONT%3E%22%20even%20on%20the%20first%20iteration%20of%20the%20foreach%20loop.%26nbsp%3B%20Everything%20else%20works%20ok.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EEDIT%3A%20here%20is%20the%20output%20I%20get%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%3E0%20MailboxDatabase01%20-%20MailboxDatabase01%20Active%20on%20SERVER1%20SERVER1%20SERVER1%20SERVER1%20SERVER1%3CBR%20%2F%3E0%20MailboxDatabase02%20-%20MailboxDatabase02%20Active%20on%20SERVER1%20SERVER1%20SERVER1%20SERVER1%20SERVER1%3CBR%20%2F%3E0%20MailboxDatabase03%20-%20MailboxDatabase03%20Active%20on%20SERVER1%20SERVER1%20SERVER1%20SERVER1%20SERVER1%3CBR%20%2F%3E0%20MailboxDatabase04%20-%20MailboxDatabase04%20Active%20on%20SERVER1%20SERVER1%20SERVER1%20SERVER1%20SERVER1%3CBR%20%2F%3E0%20MailboxDatabase05%20-%20MailboxDatabase05%20Active%20on%20SERVER1%20SERVER1%20SERVER1%20SERVER1%20SERVER1%3CBR%20%2F%3E%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECan%20someone%20tell%20me%20what%20I%20am%20doing%20wrong%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ethanks%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ejc%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%3E%24MailboxDBs%20%3D%20Get-MailboxDatabase%3CBR%20%2F%3Eforeach%20(%24DB%20in%20%24MailboxDBs)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%26nbsp%3B%20%24WhichServer%20%3D%20Get-MailboxDatabaseCopyStatus%20-identity%20%24DB.alias%20-active%20%7C%20select%20-expand%20ActiveDatabaseCopy%3CBR%20%2F%3E%26nbsp%3B%20if%20(%24WhichServer%20-eq%20%22Server1%22)%3CBR%20%2F%3E%26nbsp%3B%20%7B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Write-Host%20%220%20%24DB%20-%20%24DB%20Active%20on%20%24WhichServer%22%3CBR%20%2F%3E%26nbsp%3B%20%7D%3CBR%20%2F%3E%26nbsp%3B%20else%3CBR%20%2F%3E%26nbsp%3B%20%7B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Write-Host%20%221%20%24DB%20-%20%24DB%20Active%20on%20%24WhichServer%3CBR%20%2F%3E%26nbsp%3B%20%7D%3CBR%20%2F%3E%7D%3C%2FFONT%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-746524%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-746604%22%20slang%3D%22en-US%22%3ERe%3A%20Issue%20with%20foreach%20loop%20and%20variable%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-746604%22%20slang%3D%22en-US%22%3EThink%20I%20have%20it.%3CBR%20%2F%3E%3CBR%20%2F%3EI%20changed%3CBR%20%2F%3E-identity%20%24DB.alias%3CBR%20%2F%3Eto%3CBR%20%2F%3E-identity%20%24DB.name%3CBR%20%2F%3E%3CBR%20%2F%3EThis%20seems%20to%20have%20resolved%20the%20issue.%3C%2FLINGO-BODY%3E
Highlighted
New 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
Highlighted
Think I have it.

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

This seems to have resolved the issue.