Thursday, March 1, 2018

PowerShell Script for export CSV File

9 - Sample PowerShell Script for export CSV File


####################################################################################################################################
 <#  Author :  Pawan Aryal
   Create Date:   02/07/2018
   Description:   Export SQL Data into CSV File using Powershell -
   Source Data :  Database.Schema.Table
                         
                             
   Params:  $serverName      = Instance of the SQL server
                  $databaseName  = name of the database
                  $extractFile        = name of the csv file
#>         
####################################################################################################################################
param
 (
    $SQLServer      = "SQLServerDatawarehouse",
    $Database         = "Database",
    $FilePath           = "C:\Scripts"
     
 )

trap {
        "Error trapped"; break;
}

write-host  (Get-Date) "   Beginning script..."
write-host  (Get-Date) "   Populating dataset in memory..."


####################################################################################################################################
#SQL

  $SqlQuery  = "SELECT * FROM Database.Schema.Table";


####################################################################################################################################
#Delcare Connection Variables

    $SQLConnection = New-Object System.Data.SqlClient.SqlConnection;
$SQLConnection.ConnectionString = `
"Server = $SQLServer; Database = $Database; Integrated Security = True";

$SQLCommand = New-Object System.Data.SqlClient.SqlCommand;
$SQLCommand.CommandText = $SqlQuery;
$SQLCommand.Connection  = $SQLConnection;
 

####################################################################################################################################
#Load up the Tables in a dataset
 
    $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
    $SqlAdapter.SelectCommand = $SQLCommand
    $DataSet = New-Object System.Data.DataSet
    $SqlAdapter.Fill($DataSet)
    $DataSetTable = $DataSet.Tables["Table"];
    $SQLConnection.Close()

####################################################################################################################################
# Grab Last row cell data from DataSet of  Eom_Snapshot_Date column to print filename with Date.

    $lastrows = $DataSet.Tables["Table"] | Select-Object -Last 1 |  foreach  {$_.Eom_Snapshot_Date}

$LastRowMonthEndDate  = $lastrows | Get-Date
    $LastRowMonthEndDate  = $LastRowMonthEndDate.ToString("yyyyMMdd") 
   


write-host  (Get-Date) "   Workout MonthEnd date   = " $LastRowMonthEndDate

####################################################################################################################################
# Generate csv file.

$Export_MWS_File  = "$FilePath"+"Workout_Summary_$LastRowMonthEndDate.csv"
    $DataSet.Tables[0]  | Export-Csv $Export_MWS_File -NoTypeInformation
 

write-host  (Get-Date) ("   Monthly Workout Summary file saved  at :- " + $Export_MWS_File)
write-host  (Get-Date) ("   Process completed !")

 
####################################################################################################################################

No comments:

Post a Comment