我尝试过在我的powershell中使用catch语句来处理错误异常,但是下面的语句没有将sql消息输出到.log文件中。
Try{
Invoke-SqlCmd -InputFile "D:\DropBoxFolder\Dropbox\Public\WIKI_POWERSHELL\AdminTask\ExecutePowerShell_From_BAT\Map_AU.sql"
}
Catch{
$ErrorMessage = $_.Exception.Message
$Time=Get-Date
"This script failed at $Time and error message was $ErrorMessage" | out-file D:\DropBoxFolder\Dropbox\Public\WIKI_POWERSHELL\AdminTask\ExecutePowerShell_From_BAT\Inovke_SQLScript.log -append
}
Finally{
$Time=Get-Date
"This script made a read attempt at $Time" | out-file D:\DropBoxFolder\Dropbox\Public\WIKI_POWERSHELL\AdminTask\ExecutePowerShell_From_BAT\Inovke_SQLScript.log -append
$con.Close()
}在这里,从powershell返回的错误,我想将错误重定向到.log
Invoke-Sqlcmd :数据库中已经有一个名为“Map_AU234”的对象。在D:\DropBoxFolder\Dropbox\Public\WIKI_POWERSHELL\AdminTask\ExecutePowerShell_ From_BAT\Inovke_SQLScript.ps1:49 char:14 + Invoke-SqlCmd <<<< -InputFile "D:\DropBoxFolder\Dropbox\Public\WIKI_POWERSHE LL\AdminTask\ExecutePowerShell_From_BAT\Map_AU.sql“+ CategoryInfo : InvalidOperation:(:) Invoke-Sqlcmd,SqlPowerS hellSqlExecutionException + FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShe ll.GetScriptCommand
发布于 2016-01-10 08:42:28
在Invoke-SqlCmd的末尾添加-ErrorAction Stop之后,它就可以工作了。
https://stackoverflow.com/questions/34702221
复制相似问题