Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources: Active Threads
Viewing all articles
Browse latest Browse all 8411

Pass username fro MySQL query into variable with field name meta data

$
0
0

Hi All,

I am new Powershell and MySQL and having some trouble with passing a username, returned by a query to a MySQL server, into a variable without the meta data of the field name being copied also.

The query responds with the following:

Login ----- test1

 

I want "test1" only to pass into variable $test. However I cannot work out the correct way to do this.

I am importing a module that I have created to store the MySQL query so that I can simple call the connection and pass a query to it in a separate script, the module is call mysqlconnection.psm1 and contains the following:

Function Run-MySQLQuery {

    Param(
        [Parameter(
            Mandatory = $true,
            ParameterSetName = '',
            ValueFromPipeline = $true)]
            [string]$query,   
        [Parameter(
            Mandatory = $true,
            ParameterSetName = '',
            ValueFromPipeline = $true)]
            [string]$connectionString
        )
    Begin {
        Write-Verbose "Starting Begin Section"        
    }
    Process {
        Write-Verbose "Starting Process Section"
        try {
            # load MySQL driver and create connection
            Write-Verbose "Create Database Connection"
            # You could also could use a direct Link to the DLL File
            # $mySQLDataDLL = "C:\Program Files (x86)\MySQL\MySQL Connector Net 6.9.3\Assemblies\v4.5\MySQL.Data.dll"
            # [void][system.reflection.Assembly]::LoadFrom($mySQLDataDLL)
            [void][System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
            $connection = New-Object MySql.Data.MySqlClient.MySqlConnection
            $connection.ConnectionString = $ConnectionString
            Write-Verbose "Open Database Connection"
            $connection.Open()
            # Run MySQL Querys
            Write-Verbose "Run MySQL Querys"
            $command = New-Object MySql.Data.MySqlClient.MySqlCommand($query, $connection)
            $dataAdapter = New-Object MySql.Data.MySqlClient.MySqlDataAdapter($command)
            $dataSet = New-Object System.Data.DataSet
            $recordCount = $dataAdapter.Fill($dataSet, "data")
            $dataSet.Tables["data"] | Format-Table
        }        
        catch {
            Write-Host "Could not run MySQL Query" $Error[0]    
        }    
        Finally {
            Write-Verbose "Close Connection"
            $connection.Close()
        }
    }
    End {
        Write-Verbose "Starting End Section"
    }
}

export-modulemember Run-MySQLQuery

This module is called in another script with the following:

$username = "test1"

Import-Module ActiveDirectory
Import-Module C:\Windows\System32\WindowsPowerShell\v1.0\Modules\MySQL\mysqlconnection_test.psm1

$test = run-MySQLQuery -ConnectionString "Server=test;Uid=root;Pwd=Password123;database=test;" -Query "select login from usr_data where usr_id = 226"

 

As state previously $test now contains:

Login
-----
test1

I am sure I am missing something simply, but I am stumped so far.

 

Powershell and MySQL are not part of my standard expertise so any help with this would be greatly appreciated!

Ryan


Viewing all articles
Browse latest Browse all 8411

Trending Articles