Solved

porblem with variable count

Posted on 2013-05-16
8
198 Views
Last Modified: 2013-05-18
Hello,

With this script, I check mirroring role

$server="TEST\DBTEST,TEST\DBTEST2"

foreach ($ser in $server) {
$STATUS_DBMI = (&sqlcmd -E -S $server -d master -h -1 -W -b -Q " set nocount on;SELECT DB_NAME(database_id) +'|'+ mirroring_role_desc +'|'+ mirroring_state_desc FROM sys.database_mirroring WHERE mirroring_guid IS NOT NULL;")
echo $STATUS_DBMI

if ($STATUS_DBMI) {
  echo $STATUS_DBMI
  $NB_DBMI=$STATUS_DBMI.count

  for($k = 0; $k -lt $NB_DBMI; $k++) {
  echo $STATUS_DBMI
  $array_str = $STATUS_DBMI[$k].split("|")
  $DB_NAME = $array_str[0].Trim()
  $MIRR_ROLE_DESC = $array_str[1].Trim()
  $MIRR_STATE_DESC = $array_str[2].Trim()

 }

 $STATUS_DBMI is null only on the loop, why?
How can I resolve iit?

Thanks
0
Comment
Question by:bibi92
  • 5
  • 3
8 Comments
 
LVL 40

Expert Comment

by:footech
ID: 39173304
It looks like your sqlcmd should use $ser and not $server.

So you're saying that the first two times that you echo $STATUS_DBMI, everything is output fine, but the third time (in the "for" loop) you get nothing?
0
 

Author Comment

by:bibi92
ID: 39173309
$server is used because echo $STATUS_DBMI is only $null on the loop for($k = 0; $k -lt $NB_DBMI; $k++) {
  echo $STATUS_DBMI
but for a server $STATUS_DBMI is $null because there is not mirroring
0
 
LVL 40

Expert Comment

by:footech
ID: 39173367
Try changing the following lines
$server="TEST\DBTEST","TEST\DBTEST2"

foreach ($ser in $server) {
$STATUS_DBMI = (&sqlcmd -E -S $ser -d master -h -1 -W -b -Q " set nocount on;SELECT DB_NAME(database_id) +'|'+ mirroring_role_desc +'|'+ mirroring_state_desc FROM sys.database_mirroring WHERE mirroring_guid IS NOT NULL;")

Open in new window

Please provide the output of echo $STATUS_DBMI.
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

by:bibi92
ID: 39173376
$STATUS_DBMI = "PROD|PRINCIPAL|SYNCHRONIZED"
0
 

Author Comment

by:bibi92
ID: 39173400
This following command is ok :

$server="TEST\DBTEST","TEST\DBTEST2"

foreach ($ser in $server) {
$STATUS_DBMI = (&sqlcmd -E -S $ser -d master -h -1 -W -b -Q " set nocount on;SELECT DB_NAME(database_id) +'|'+ mirroring_role_desc +'|'+ mirroring_state_desc FROM sys.database_mirroring WHERE mirroring_guid IS NOT NULL;")

but I have to echo the variable from array, and this does not work :

for($k = 0; $k -lt $NB_DBMI; $k++) {
  echo $STATUS_DBMI
  $array_str = $STATUS_DBMI[$k].split("|")
  $DB_NAME = $array_str[0].Trim()
  $MIRR_ROLE_DESC = $array_str[1].Trim()
  $MIRR_STATE_DESC = $array_str[2].Trim()
0
 

Author Comment

by:bibi92
ID: 39173487
I try to modify the script with replace array like this, the following error occurs :
$DB_NAME=$STATUS_DBMI.split <<<< ("|")[0]
+ CategoryInfo          : InvalidOperation: (split:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : InvokeMethodOnNull
0
 
LVL 40

Accepted Solution

by:
footech earned 500 total points
ID: 39173909
If $STATUS_DBMI is just a string like "PROD|PRINCIPAL|SYNCHRONIZED" and not an array, then you can eliminate the for loop.
        $DB_NAME = $STATUS_DBMI.split("|")[0].Trim()
        $MIRR_ROLE_DESC = $STATUS_DBMI.split("|")[1].Trim()
        $MIRR_STATE_DESC = $STATUS_DBMI.split("|")[2].Trim()

Open in new window


or using an intermediate variable
        $array_str = $STATUS_DBMI.split("|")
        $DB_NAME = $array_str[0].Trim()
        $MIRR_ROLE_DESC = $array_str[1].Trim()
        $MIRR_STATE_DESC = $array_str[2].Trim()

Open in new window


I'd like to make sure I understand all the data that is being worked on.  Can you provide the complete console output from running the following?
$server="TEST\DBTEST,TEST\DBTEST2"

foreach ($ser in $server) {
    $STATUS_DBMI = (&sqlcmd -E -S $ser -d master -h -1 -W -b -Q " set nocount on;SELECT DB_NAME(database_id) +'|'+ mirroring_role_desc +'|'+ mirroring_state_desc FROM sys.database_mirroring WHERE mirroring_guid IS NOT NULL;")

    if ($STATUS_DBMI) {
        $STATUS_DBMI
        $STATUS_DBMI.gettype()
        $NB_DBMI=$STATUS_DBMI.count
        $NB_DBMI
  }
}

Open in new window

0
 

Author Closing Comment

by:bibi92
ID: 39176965
Thanks regards
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Set OWA language and time zone in Exchange for individuals, all users or per database.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question