$Conectionstring ="Server=.;Database=master;Trusted_Connection=True;"
$sqlconnection = New-Object System.Data.SqlClient.SqlConnection $Conectionstring
$sqlconnection.Open()
$cmd = New-Object System.Data.SqlClient.SqlCommand
$cmd.Connection = $sqlconnection
$cmd.CommandTimeout = 0
$cmd.CommandText = "print 'TestOutputMessage1';WAITFOR DELAY '00:00:010';print 'TestOutputMessage2'"
#$message=""
$handler = [System.Data.SqlClient.SqlInfoMessageEventHandler] {param($sender, $event)
[System.IO.File]::AppendAllText("E:\output.log",$event.Message)
#$global:message += $event.Message
}
$sqlconnection.add_InfoMessage($handler);
$cmd.ExecuteNonQuery()
#$message |Write-Warning
标签:cmd,SQLServer,System,event,sqlconnection,message,Data,PowerShell
From: https://www.cnblogs.com/talentzemin/p/17079491.html