Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

SQL stored procedure print output to text file;

$
0
0

I'm trying to get the output of a storedprocedure to a file.  The SP creates a print view of the data and when I try to out-file it just gives me the -1.  Here is my script:

$server = "server"
#$dbToBackup = "database" 
$query = "master.dbo.usp_logins"
$SecurityFile = "C:\outfile.txt"

$connString = "Server=$server;Database=$dbToBackup;Integrated Security=SSPI;" 
$conn = new-object System.Data.SqlClient.SqlConnection $connString 
$handler = [System.Data.SqlClient.SqlInfoMessageEventHandler] {param($sender, $event) Write-Host $event.Message };
$conn.add_InfoMessage($handler)
$conn.FireInfoMessageEventOnUserErrors = $true
$conn.Open() 
$cmd = new-object System.Data.SqlClient.SqlCommand("$query",$conn) 
$cmd.CommandType = [System.Data.CommandType]"StoredProcedure" 
$cmd.ExecuteNonQuery() 
$conn.Close() 
I can get the results in the PS window but I need to get this to a file for later use.



Viewing all articles
Browse latest Browse all 21975

Trending Articles