SQL example book Powershell Toolmaking

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Brian Klish Brian Klish 2 years, 8 months ago.

  • Author
    Posts
  • #16394
    Profile photo of Ramon Bruin
    Ramon Bruin
    Participant

    Hi,

    I am reading the book Powershell Toolmaking in a month of lunches. Which I really like!

    But I am running into an issue at chapter 14, Adding database access. It seems when I am querying the database, I get next to the results, also some kind of row count.
    Which is then also used as a value and is then causing an error.

    When I query the database, which contains three computernames, it first displays a 3 and then the three computernames. When I pipe this into the Get-MOLSystemInfo, it is trying to connect to computer 3, before it continues with the actual machines from the database, as you can see in file1.

    I have pinned it down to the Get-MOLDatabaseData function. I have paste into ISE, made some adjustments so that it works as a standalone script, and I get the same behavior. As you can see in file2.

    So my question is, how do I get rid of this total count value?
    Did I miss something somewhere?

    Thanks,

    Ramon Bruin

  • #16400
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    I believe it's the $adapter.Fill... that returns a row count. This is how I handled it in my function:

    # Fill the DataSet with the SQL Adapter info
    $nRecs = $SqlAdapter.Fill($DataSet)
    $nRecs | Out-Null 

    Basically, the .NET function is returning a row count value and you need to just pipe that output to | Out-Null to catch the output and return nothing (null). You can try just doing the | Out-Null on your fill line as well.

  • #16401
    Profile photo of Ramon Bruin
    Ramon Bruin
    Participant

    Hi Rob,

    Thanks man, that fixed it for me!

  • #20056
    Profile photo of Brian Klish
    Brian Klish
    Participant

    That fixed it for me too. The function was returning two different types of objects. First an integer and then a datarow. Don, if you see this you may want to fix the code download on morelunches?

    Now I have to figure out why I can't work the the datarow objects that are coming across. Need to convert to custom objects maybe?

You must be logged in to reply to this topic.