[英]Powershell Write-Error does not populate -ErrorVariable
我正在运行Powershell 4,并尝试通过在调用中使用-ErrorVariable参数获取错误变量以在函数中进行填充,并尝试在函数本身内写入错误。 但是,该变量永远不会填充。
这是我的脚本:
$SqlCmd = "C:\Program Files\Microsoft SQL Server\110\Tools\Binn\SQLCMD.EXE"
myfunc -ErrorVariable myfuncerr
if ($myfuncerr -and $myfuncerr.Count -ne 0) {
$worked = 1
} else {
$worked = 0
}
function myfunc
{
$output = & $SqlCmd -S localhost -d integration -q 'alter table tablethatdoesntexist add xt int' -b | out-string
if ($LASTEXITCODE = 1)
{
write-error $output
}
}
因为它是-ErrorVariable,所以我期望写错误用$ output的内容填充变量$ myfuncerr,但这不会发生($ myfuncerr保持空白)。 我正在Powershell ISE中进行调试,因此可以看到实际上已调用Write-Error。
我也尝试过用throw($ output)引发异常,并使用-ErrorAction SilentlyContinue运行myfunc,但仍未填充$ myfuncerr,即
$SqlCmd = "C:\Program Files\Microsoft SQL Server\110\Tools\Binn\SQLCMD.EXE"
myfunc -ErrorVariable myfuncerr -ErrorAction SilentlyContinue
if ($myfuncerr -and $myfuncerr.Count -ne 0) {
$worked = 1
} else {
$worked = 0
}
function myfunc
{
$output = & $SqlCmd -S localhost -d integration -q 'alter table tablethatdoesntexist add xt int' -b | out-string
if ($LASTEXITCODE = 1)
{
throw $output
}
}
我是否正确使用-ErrorVariable参数?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.