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

 
Participant
1 week, 5 days ago.

  • Author
    Posts
  • #165235

    Participant
    Topics: 24
    Replies: 64
    Points: 378
    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 
    $a=$SqlAdapter.Fill($DataSet)
    $SqlConnection.Close() 
    $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)

    script2:

    & ".\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) 
    {
        $i=-1
        $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)")
            $i--
        } 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

    Participant
    Topics: 1
    Replies: 1430
    Points: 2,131
    Helping Hand
    Rank: Community Hero
  • #165256

    Participant
    Topics: 24
    Replies: 64
    Points: 378
    Rank: Contributor

    https://social.technet.microsoft.com/Forums/en-US/0e171c40-073e-43ed-b9ae-67e479791aa7/what-exactly-is-the-null-part-in-this-error?forum=winserverpowershell

    https://stackoverflow.com/questions/56979513/what-does-a-query-have-to-do-with-get-member

    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.