Solved

porblem with variable count

Posted on 2013-05-16
8
196 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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 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

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 …
A procedure for exporting installed hotfix details of remote computers using powershell
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

777 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