Search code examples
powershellvariablespsobject

PSobject as variable


I'm trying to make the 2 functions below working. The thing below, what I find a little strange is: Why is the first Qf function call working but why not the second?

$global:query_output = @()
$global:query_output_filtered = @()

function Q {
    Param(
        [Parameter(Mandatory=$true)][string]$DBquery,
        $DBip = "IP" ,
        $DBusr = "username" ,
        $DBpas = "password" ,
        $DBname = "dbname"
    )
    Process {
        try {
            $SQLConnection = New-Object System.Data.SQLClient.SQLConnection
            $SQLConnection.ConnectionString ="server=$DBip;database=$DBname; User         ID = $DBusr; Password = $DBpas;"
            $SQLConnection.Open()
        } catch {
            [System.Windows.Forms.MessageBox]::Show("Failed to connect SQL     Server:")
        }

        $SQLCommand = New-Object System.Data.SqlClient.SqlCommand
        $SQLCommand.CommandText = "Use Inventory " + $DBquery
        $SQLCommand.Connection = $SQLConnection

        $SQLAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
        $SqlAdapter.SelectCommand = $SQLCommand
        $SQLDataset = New-Object System.Data.DataSet
        $SqlAdapter.fill($SQLDataset) | Out-Null

        $script:query_output = @()
        foreach ($data in $SQLDataset.Tables[0]) {
            $script:query_output += $data
        }
        $SQLConnection.Close()
        Write-Host ""
        Write-Host "================= query_output =======================" -ForegroundColor Green -BackgroundColor Red
        $script:query_output | Format-Table -AutoSize
        Write-Host "========================================" -ForegroundColor Green -BackgroundColor Red
    }
}

function Qf {
    Param(
        $objectsearch = "*02",
        $objectcolom = "company"
    )
    Process {
        $script:query_output_filtered = $script:query_output | Where-Object {
            $_.$objectcolom -like $objectsearch
        }
        Write-Host ""
        Write-Host "================= query_output_filtered=======================" -ForegroundColor Green -BackgroundColor Red
        $script:query_output_filtered | Format-Table -AutoSize
        Write-Host "========================================" -ForegroundColor Green -BackgroundColor Red
    }
}

Q("SELECT * FROM machine WHERE ID LIKE '%111'")

Qf("*DE")
Qf("*POS02","systemname")

Solution

  • Arguments to PowerShell functions/cmdlets must be passed whitespace-separated, not comma-separated. The latter is only for passing arguments to object methods.

    A statement Qf("*DE") first evaluates the grouping expression ("*DE") to the string "*DE", then passes that string as the first argument to the function Qf.

    A statement Qf("*POS02","systemname") again first evaluates the grouping expression ("*POS02","systemname") to the string array "*POS02","systemname", then passes that array as the first argument to the function Qf. Because of that the parameter $objectsearch has the value "*POS02","systemname" and the parameter $objectcolom has the (default) value "company".

    Change this:

    Q("SELECT * FROM machine WHERE ID LIKE '%111'")
    
    Qf("*DE")
    Qf("*POS02","systemname")
    

    into this:

    Q "SELECT * FROM machine WHERE ID LIKE '%111'"
    
    Qf "*DE"
    Qf "*POS02" "systemname"
    

    and the problem will disappear.