Exporting data with header using Powershell

  • Comments posted to this topic are about the item Exporting data with header using Powershell

    ----------------------------------------------------------------------------------------------
    Microsoft Certified Solution Master: Data Platform, Microsoft Certified Trainer
    Email: Louis.Li@rrlminc.com | Blog[/url] | LinkedIn[/url]

  • I really wish I understood what this script is doing. To me, the article assumes the reader already knows a lot of what is being referred to.

    I can write queries, but I have never used Powershell, and I can't make heads or tails out of these directions.

    I do have a query with results that I would like to export to a file (although I want it tab-delimited, not comma separated), but I would need the directions to be more explanatory than this.

  • swebster (4/28/2014)


    I really wish I understood what this script is doing. To me, the article assumes the reader already knows a lot of what is being referred to.

    I can write queries, but I have never used Powershell, and I can't make heads or tails out of these directions.

    I do have a query with results that I would like to export to a file (although I want it tab-delimited, not comma separated), but I would need the directions to be more explanatory than this.

    Hi swebster, take a look at this:

    http://rrlmblog.wordpress.com/2014/04/29/step-by-step-extracting-data-from-sql-server-using-powershell/

    ----------------------------------------------------------------------------------------------
    Microsoft Certified Solution Master: Data Platform, Microsoft Certified Trainer
    Email: Louis.Li@rrlminc.com | Blog[/url] | LinkedIn[/url]

  • swebster (4/28/2014)


    I really wish I understood what this script is doing. To me, the article assumes the reader already knows a lot of what is being referred to.

    I can write queries, but I have never used Powershell, and I can't make heads or tails out of these directions.

    I do have a query with results that I would like to export to a file (although I want it tab-delimited, not comma separated), but I would need the directions to be more explanatory than this.

    There are much easier methods of getting information out of SQL Server with SQL Server PowerShell. Most folks tend to go towards using SMO or .NET data reader objects.

    A basic example of how I generally pull out information to manipulate in PowerShell

    #need to import the module used by SQL Server 2012 in order to access the Invoke-Sqlcmd commandlet

    Import-Module SQLPS

    #if working with SQL Server 2008 - SQL Server 2008 R2 use:

    # Add-PSSnapin *SQL*

    #This is a here-string and supports variables that contain multi-line strings

    $q = @"

    SELECT name, database_id

    FROM sys.databases

    GO

    "@

    #capturing the data into a variable

    $results = Invoke-Sqlcmd -ServerInstance ORKO -Database master -Query $q

    # export the results straight to your tab delimited CSV file

    $results | Export-Csv -Path 'C:\Temp\TestInfo.cvs' -Delimiter "`t" -NoTypeInformation

    Shawn Melton
    Twitter: @wsmelton
    Blog: wsmelton.github.com
    Github: wsmelton

  • Louis and Shawn,

    Thanks!

    Scott

  • When i am trying to run the script through sql server agent it is showing given error. Could any body can suggest how i can run this by using sql server agent .

    Executed as user: HOMEPC\SYSTEM. A job step received an error at line 2 in a PowerShell script. The corresponding line is 'write-host $args.Count'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'Cannot invoke this function because the current host does not implement it. '. Process Exit Code -1. The step failed.

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply