• 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