简体   繁体   中英

PowerShell Function multiple SQL queries output as CSV

I'm having problems getting PowerShell to run multiple SQL queries and export the results as CSV.

I'm trying to accomplish this using a Function but the problem occurs in the Process block when I expect two queries to run and output two CSV files.

I tried creating one function to run the query and a second function to create the CSV files but that didn't even run the SQL queries. I'm doing this without SQL being installed where this powershell script is executed from. -thanks!

Function Run-Query {
 param([string[]]$queries,[string[]]$sheetnames)
Begin{
 $SQLServer = 'ServerName'
 $Database = 'DataBase'
 $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
 $SqlConnection.ConnectionString = "Server = $SQLServer; Database = $Database; Integrated Security = True"
}#End Begin
Process{
 $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
 $SqlCmd.CommandText = $queries
 $SqlCmd.Connection = $SqlConnection
 $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
 $SqlAdapter.SelectCommand = $SqlCmd
 $DataSet = New-Object System.Data.DataSet
 $SqlAdapter.Fill($DataSet)
 $DataSet.Tables[0] | Export-Csv -NoTypeInformation -Path "C:\Scripts\$sheetnames.csv"
}#End Process
End{
 $SqlConnection.Close()
}
}#End function run-query.
$queries = @()
 $queries += @'
Select * from something
'@
 $queries += @'
Select * from something2
'@
$sheetnames = @()
$sheetnames += 'Cert'
$sheetnames += 'Prod'
Run-Query -queries $queries

I'm not sure if SQL processes multiple queries separately so while you might be passing two different queries the SQL server might be interpreting them as one query (Not 100% sure this is happening, just a guess really)

You've put your queries in an array so we can easily loop through the array, run each query by itself and put the results into a CSV.

Here's how i'd modify your code to start with:

Function Run-Query
{
    param([string[]]$queries,[string[]]$sheetnames)
    Begin
    {
        $SQLServer = 'ServerName'
        $Database = 'DataBase'
        $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
        $SqlConnection.ConnectionString = "Server = $SQLServer; Database = $Database; Integrated Security = True"
    }#End Begin
    Process
    {
        # Loop through each query
        For($i = 0; $i -lt $queries.count; $i++)
        {
            $SqlCmd = New-Object System.Data.SqlClient.SqlCommand

            # Use the current index ($i) to get the query
            $SqlCmd.CommandText = $queries[$i]

            $SqlCmd.Connection = $SqlConnection
            $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
            $SqlAdapter.SelectCommand = $SqlCmd
            $DataSet = New-Object System.Data.DataSet
            $SqlAdapter.Fill($DataSet)

            # Use the current index ($i) to get the sheetname for the CSV
            $DataSet.Tables[0] | Export-Csv -NoTypeInformation -Path "C:\Scripts\$($sheetnames[$i]).csv"
        }
    }#End Process
    End
    {
        $SqlConnection.Close()
    }
}#End function run-query.

$queries = @()

$queries += @'
Select * from something
'@
$queries += @'
Select * from something2
'@

$sheetnames = @()
$sheetnames += 'Cert'
$sheetnames += 'Prod'

Run-Query -queries $queries -sheetnames $sheetnames

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM