Why am I getting a get-member error from a query?

Welcome Forums General PowerShell Q&A Why am I getting a get-member error from a query?

This topic contains 2 replies, has 2 voices, and was last updated by

2 months, 2 weeks ago.

  • Author
  • #165235

    Topics: 24
    Replies: 65
    Points: 383
    Rank: Contributor

    I have 2 scripts

    script1 has the following:

    Function Query($Query) {
    $SqlConnection = New-Object System.Data.SqlClient.SqlConnection 
    $SqlConnection.ConnectionString = "Server=$DB_Server;Initial Catalog=$Database;Integrated Security=SSPI" 
    $SqlCmd = New-Object System.Data.SqlClient.SqlCommand 
    $SqlCmd.Connection = $SqlConnection 
    $SqlCmd.CommandText = $Query 
    $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter 
    $SqlAdapter.SelectCommand = $SqlCmd 
    $DataSet = New-Object System.Data.DataSet 
    $DataSet.Tables[0] }
    #create .net array object for csv export
    $exportObject = New-Object System.Collections.ArrayList
    #create ordered dictionary so column names come out in the ordered they were created
    $rowObject = [ordered]@{}
    $connection_string = "Connection Timeout=120;User Id=UID1;Data Source=datasource.com;Password=password12!553"
    $rowObject.'Connection Details' = $connection_string
    #INSERT connection string into Table
    Query "UPDATE [$someTable]
    SET [connection_string] = '$connection_string'
    WHERE [cname] = '$cinput' AND ([pserver] = '$pinput'"
    $exportObject.Add((New-Object PSObject -Property $rowObject)) | Out-Null
    $exportObject | Select-Object

    Now in script2, i call script1 and out-variable the object then convert it to a pscustomobject to use it with an HTML Table function (not relevant to this thread so wont include in code. more info on that here)


    & ".\script1.ps1" -ViewConnection "$cinput" -OutVariable xprtOut | Format-Table -Wrap
    #converting xprtOut from Arraylist to pscustomobject to be used with ConvertTo-HTMLTable 
    $Arr = @()
    Foreach ($Object in $xprtOut) 
        $arrayListCount = -($Object | gm | Where-Object {$_.MemberType -like "noteproperty"}).count
        $customObj = New-Object pscustomobject
        do {
            $customObj | Add-Member -MemberType NoteProperty -Name (($Object | gm)[$($i)].Name) -Value ($Object."$(($Object | gm)[$($i)].Name)")
        } while($i -ge $arrayListCount)
        $Arr += $customObj

    when i run script2, i get the following errors:

    gm : You must specify an object for the Get-Member cmdlet.

    $arrayListCount = -($Object | gm | Where-Object {$_.MemberType -l ...

    Cannot index into a null array.

    ... dd-Member -MemberType NoteProperty -Name (($Object | gm)[$($i)].Name) ...

    After some long debugging, i found the root problem: by removing the Update Query statement from script1, script2 stopped erroring and it started working just fine.

    so the question is, why would the Query statement in script1 be problematic? what does it have to do with the object conversion??

    This post is related to this Post

  • #165253

    Topics: 1
    Replies: 1532
    Points: 2,601
    Helping Hand
    Rank: Community Hero
  • #165256

    Topics: 24
    Replies: 65
    Points: 383
    Rank: Contributor



    Is it really that hard to tell that you already asked this in other forums?

    Guess not, but based on recommendations in the past, I posted a reference in the post already

You must be logged in to reply to this topic.