Solved

porblem with variable count

Posted on 2013-05-16
8
200 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

This article will help you understand what HashTables are and how to use them in PowerShell.
A brief introduction to what I consider to be the best editor for PowerShell.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the adminiā€¦
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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