locked
How to get / compare values in PS RRS feed

  • Question

  • Hi all;

    I'm trying to run a PS script like this:

    $ResultSet = Sqlcmd -x -I -b -V 1 -S meuserver -Q "Select Name from sys.databases"
    foreach ($objItem in $ResultSet)
    {
    	if ($objItem.Name -eq $null)
    	{
    	Write-Host $objItem.Name "Não funcionou"
    	}
    	else
    	{
    	Write-Host $objItem.Name "OK"
    	}
    }

    But the result is:

    Não funcionou
    Não funcionou
    Não funcionou
    Não funcionou
    Não funcionou
    Não funcionou
    Não funcionou
    Não funcionou
    Não funcionou
    Não funcionou
    Não funcionou
    

    How can I correct compare value in $objItem.Name ?

    Thanks.

    Ricardo


    <a href="http://br.linkedin.com/in/ricardomuramatsu"><img src="http://www.linkedin.com/img/webpromo/btn_liprofile_blue_80x15.png" width="80" height="15" border="0" alt="View Ricardo Muramatsu's profile on LinkedIn"> </a>

    Wednesday, May 28, 2014 5:17 PM

Answers

  • You are asking how to parse string output. Here is an example:


    $resultSet = @"
    Falha
    -----------
              1
              1
              1
              1
              1
    "@ -split "`r`n"
    
    for ( $n = 2; $n -lt $resultSet.Count; $n++ ) {
      $result = $resultSet[$n].Trim()
      if ( $result -eq "1" ) {
        "Result was 1"
      }
      else {
        "Result was not 1"
      }
    }
    


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by jrv Wednesday, May 28, 2014 7:23 PM
    • Marked as answer by Ricardo Muramatsu Wednesday, May 28, 2014 7:34 PM
    Wednesday, May 28, 2014 7:01 PM
  • This is an array of strings:

    Falha
    -----------
              1
              1
              1
              1
              1

    Just access the array directly.

    for($i=2; $i -lt $resultSet.Count' $i++){
        If (([int]$resultSet[$i].Falha) -eq 1) {
            Write-Output 'Do something here, 1 was returned'
    	write-host $_.Falha "<<<"
        } Else {
            Write-Output 'No action required, 1 was NOT returned'
    	write-host $_.Falha "<<<"
        }
    }

    Same as Mike's code but you have to walk the array after skipping the first two rows.

    Bills code is correct for the "here" string but will be odd for the returned array. It might work as the array lines might ignore the split.

    The idea is the same.


    ¯\_(ツ)_/¯



    • Edited by jrv Wednesday, May 28, 2014 7:26 PM
    • Marked as answer by Ricardo Muramatsu Wednesday, May 28, 2014 7:35 PM
    Wednesday, May 28, 2014 7:22 PM

All replies

  • Sqlcmd is going to output String objects. So you need to remove .Name from your if comparison, because String objects do not have a Name property.

    -- Bill Stewart [Bill_Stewart]

    Wednesday, May 28, 2014 5:53 PM
  • Ok, I'llrephrasemy question...

    I've this...

    PS> $ResultSet = Sqlcmd -x -I -b -V 1 -S Myserver -i "c:\temp\Get_Sembackup.sql"
    PS> $resultset
    Falha
    -----------
              1
              1
              1
              1
              1

    I need to compare the value of the "Falha" field and if 1 takes an action, if different from 1 does not need to do anything.
    The fact is that I am unable to make the comparison of the value of the "Falha" field.
    How can do this?

    Thanks again and sorry about my bad english.

    Ricardo

     

    <a href="http://br.linkedin.com/in/ricardomuramatsu"><img src="http://www.linkedin.com/img/webpromo/btn_liprofile_blue_80x15.png" width="80" height="15" border="0" alt="View Ricardo Muramatsu's profile on LinkedIn"> </a>

    Wednesday, May 28, 2014 6:48 PM
  • EDIT: Nothing to see here, take a look at Bill's response below.


    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    Wednesday, May 28, 2014 6:56 PM
  • You are asking how to parse string output. Here is an example:


    $resultSet = @"
    Falha
    -----------
              1
              1
              1
              1
              1
    "@ -split "`r`n"
    
    for ( $n = 2; $n -lt $resultSet.Count; $n++ ) {
      $result = $resultSet[$n].Trim()
      if ( $result -eq "1" ) {
        "Result was 1"
      }
      else {
        "Result was not 1"
      }
    }
    


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by jrv Wednesday, May 28, 2014 7:23 PM
    • Marked as answer by Ricardo Muramatsu Wednesday, May 28, 2014 7:34 PM
    Wednesday, May 28, 2014 7:01 PM
  • Hi,

    This is a complete guess, as I don't have anything to test with:

    $ResultSet = Sqlcmd -x -I -b -V 1 -S Myserver -i "c:\temp\Get_Sembackup.sql"
    
    $ResultSet | ForEach {
    
        If ($_.Falha -eq 1) {
    
            Write-Output 'Do something here, 1 was returned'
    
        } Else {
    
            Write-Output 'No action required, 1 was NOT returned'
    
        }
    
    }

    You may need to use '1' (instead of 1) in the if statement, depending on what's in your results.


    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    Hi Mike,

    This is a problem...

    The script:

    $ResultSet = Sqlcmd -x -I -b -V 1 -S myserver -i "c:\temp\Get_Sembackup.sql"
    $ResultSet | ForEach {
        If ($_.Falha -eq 1) {
            Write-Output 'Do something here, 1 was returned'
    		write-host $_.Falha "<<<"
        } Else {
            Write-Output 'No action required, 1 was NOT returned'
    		write-host $_.Falha "<<<"
        }
    }

    Result here appear to have a $NULL in $_.Falha

    No action required, 1 was NOT returned
    <<<
    No action required, 1 was NOT returned
    <<<
    No action required, 1 was NOT returned
    <<<
    No action required, 1 was NOT returned
    <<<
    No action required, 1 was NOT returned
    <<<
    No action required, 1 was NOT returned
    <<<
    No action required, 1 was NOT returned
    <<<
    No action required, 1 was NOT returned
    <<<
    No action required, 1 was NOT returned
    <<<

    But this is not True, the value is 1:

    PS> $ResultSet
    Falha      
    -----------
              1
              1
              1
              1
              1

    Att.

    Ricardo


    <a href="http://br.linkedin.com/in/ricardomuramatsu"><img src="http://www.linkedin.com/img/webpromo/btn_liprofile_blue_80x15.png" width="80" height="15" border="0" alt="View Ricardo Muramatsu's profile on LinkedIn"> </a>

    Wednesday, May 28, 2014 7:14 PM
  • Hi Mike,

    This is a problem...

    The script:

    <snip>

    Yeah, I was assuming that you were having objects returned instead of a large string. Take a look at Bill's post.

    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    Wednesday, May 28, 2014 7:16 PM
  • This is an array of strings:

    Falha
    -----------
              1
              1
              1
              1
              1

    Just access the array directly.

    for($i=2; $i -lt $resultSet.Count' $i++){
        If (([int]$resultSet[$i].Falha) -eq 1) {
            Write-Output 'Do something here, 1 was returned'
    	write-host $_.Falha "<<<"
        } Else {
            Write-Output 'No action required, 1 was NOT returned'
    	write-host $_.Falha "<<<"
        }
    }

    Same as Mike's code but you have to walk the array after skipping the first two rows.

    Bills code is correct for the "here" string but will be odd for the returned array. It might work as the array lines might ignore the split.

    The idea is the same.


    ¯\_(ツ)_/¯



    • Edited by jrv Wednesday, May 28, 2014 7:26 PM
    • Marked as answer by Ricardo Muramatsu Wednesday, May 28, 2014 7:35 PM
    Wednesday, May 28, 2014 7:22 PM
  • I still think we will hear another shoe drop.


    ¯\_(ツ)_/¯

    Wednesday, May 28, 2014 7:26 PM
  • Thanks All.

    Ricardo


    <a href="http://br.linkedin.com/in/ricardomuramatsu"><img src="http://www.linkedin.com/img/webpromo/btn_liprofile_blue_80x15.png" width="80" height="15" border="0" alt="View Ricardo Muramatsu's profile on LinkedIn"> </a>

    Wednesday, May 28, 2014 7:35 PM