0

Below is the PowerShell function I created to return a table.

         function GetJOINConstraints
        {
        [CmdletBinding()]
            param(
            [Parameter( Mandatory=$true)] $ConnectionString,
            [Parameter( Mandatory=$true)] $tableName
                ) 

         $Conn = new-object System.Data.SqlClient.SQLConnection
         $Conn.connectionString=$ConnectionString
         $Conn.Open()
         $Cmd = New-Object System.Data.SqlClient.SqlCommand
         $Cmd.Connection = $Conn
         $cmd.CommandText = "Exec TemplateTransport.usp_Get_JOINConstraints 'Action'"  
         #$Cmd.CommandType = [System.Data.CommandType]'StoredProcedure'
         #$Cmd.Parameters.Add("@TargetTableName", $tableName)
         $SQLAdapter = New-Object System.Data.Sqlclient.SqlDataAdapter
         $SqlAdapter.SelectCommand = $Cmd
         $rs = New-Object System.Data.DataSet
         $SqlAdapter.Fill($rs)
         Return  $rs.Tables[0] | Format-Table
         $Con

    n.Close()

 }

When I try to Run this function it returns the result like below

fkeyid   : Action
fkeycol  : ActionTypeID
fkeynull : False
rkeyid   : ActionType
rkeycol  : ID
nkcol    : Name
alias    : 
nullable : False
fkOrder  : 1

fkeyid   : Action
fkeycol  : DefaultTimeTrackingTaskID
fkeynull : True
rkeyid   : TimeTrackingTask
rkeycol  : ID
nkcol    : Name
alias    : 
nullable : True
fkOrder  : 1

I'm looking for a result like

fkeyid   fkeycol  fkeynull rkeyid   rkeycol  nkcol    alias    nullable fkOrder
------   -------  -------- ------   -------  -----    -----    -------- -------
Action   Actio...    False Actio... ID       Name                 False       1
Action   Defau...     True TimeT... ID       Name                  True       1
Action   Proce...    False Process  ID       Proce... Proce...    False       1
Action   TimeT...     True TimeT... ID       Name                  True       1

I can achieve that using Format-Table . but cannot save the resultset to an Array. I want to be able to save it to an Array and work on it. Any help is much appreciated.

Thanks.

1 Answer 1

5

You should avoid formatting text for output as a return from a function. What would be better use for you is to remove that line entirely, store the output into a variable, and then pipe that variable to format-table. So, remove the |Format-Table from your function, and then do something like this:

$TableJoins = GetJOINConstraints -ConnectionString $ConString -TableName $tblName
$TableJoins | Format-Table

Alternatively you can use Tee-Object to store the output into a variable, and still pipe it to Format-Table:

GetJOINConstraints -ConnectionString $ConString -TableName $tblName | Tee-Object -Variable TableJoins | Format-Table
Sign up to request clarification or add additional context in comments.

3 Comments

I want to save the result set to an Array and loop through the array. so I tried [Array]$dt = GetJOINConstraints -ConnectionString $ConnString -tableName $tableName1 But when I try to get $dt[1][2] it says unable to index into an object of type system.Int32
Are you expecting an array of arrays? Wouldn't you want to do something like $dt[1].fkeynull?
I'm new to powershell. I was expecting a 2D Array. A similar function with a different Sproc returned a 2d array. But I'm sure I can work with $dt[1].fkeynull. thanks. I'll keep you posted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.