$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