PowerShell SQLServer

$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
posted @ 2023-01-31 15:58  talentzemin  阅读(20)  评论(0编辑  收藏  举报