Solved

porblem with variable count

Posted on 2013-05-16
8
194 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
This article will help you understand what HashTables are and how to use them in PowerShell.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

759 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

24 Experts available now in Live!

Get 1:1 Help Now