Solved

porblem with variable count

Posted on 2013-05-16
8
195 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 39

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 39

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
 

Author Comment

by:bibi92
ID: 39173376
$STATUS_DBMI = "PROD|PRINCIPAL|SYNCHRONIZED"
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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 39

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
This article will help you understand what HashTables are and how to use them in PowerShell.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now