Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell将std错误重定向到变量_Powershell_Powershell 3.0 - Fatal编程技术网

Powershell将std错误重定向到变量

Powershell将std错误重定向到变量,powershell,powershell-3.0,Powershell,Powershell 3.0,我想调用任意表达式并将std error重定向到变量,而不重定向到文件 例如,在Powershell中,可以重定向标准错误。使用临时文件,我可以轻松获得我想要的: #$expr = "1/0" # with std error #$expr = "2+2" # without stderror #$expr = "Get-Service invalidsvc" # with stder

我想调用任意表达式并将std error重定向到变量,而不重定向到文件

例如,在Powershell中,可以重定向标准错误。使用临时文件,我可以轻松获得我想要的:

#$expr = "1/0"                          # with std error
#$expr = "2+2"                          # without stderror
#$expr = "Get-Service invalidsvc"       # with stderror
$expr = "try { throw '111' } catch { }" # without std error
$ans = Invoke-Expression $expr -ErrorVariable ev 2> C:\log\stderr.txt
if(cat C:\log\stderr){
    Write-Host "Error $ev"    
}
我如何做同样的事情,但不创建临时输出文件

错误的解决方案:

  • 使用-error变量开关。反例:

    调用表达式$expr-ErrorVariable ev 2>C:\aaa\file1.txt $expr=“try{throw'111}catch{}” 写入主机“错误$ev”#将输出错误,但std错误为空

  • $LASTEXITCODE和$?检查。反例:

    获取服务无效服务

  • $lastexitcode等于0,$?等于True,但std error不为空

    想法很简单:将Powershell控制台中的“红色”(std错误)文本保存在变量中。我收到的命令是任意字符串。 示例:

  • 当我在Powershell控制台中写入“try{throw'111'}catch{}”时,PS控制台中不会出现红色(错误)文本(尽管$error不是空的)。因此,如果我在代码中调用该表达式,则不会在某个变量中保存任何错误

  • 当我写入“Get Service NoteExistingService”或“1/0”或“write Error 111”时,将出现红色(错误)文本和非空$Error。所以,如果我在代码中调用该表达式,我希望将错误保存在某个变量中


  • 执行此操作的方法是使用
    -errorvariable
    公共参数。您的反例是有效的(我只是犹豫地使用这个词),因为您使用
    Try/Catch
    显式地为它编码,以避免输出错误,并且在Catch中不包含任何编码。您基本上是在抱怨您告诉PowerShell将错误案例发送到
    Catch
    scriptblock,在那里您没有输出任何内容,然后在没有输出任何内容时出现问题。错误仍然会发生,它会按您所说的那样记录在errorvariable中,并存储在
    $error
    中,但由于您没有在
    Catch
    块中输出任何内容,因此StdErr重定向无法执行任何操作

    如果您希望$ev不包含错误,因为您纠正了
    Catch
    块中的问题,那么还需要清除Catch块中的变量

    $expr = 'try{ throw "1111"}catch{Remove-Variable ev}'
    
    或者,如果希望StdErr包含错误文本,请确保在
    Catch
    块中包含该输出:

    $expr = 'try{ throw "1111"}catch{Write-Error $_.Exception.Message}'
    

    我知道这是一个很老的问题,但我也有同样的问题,我想和大家分享我最终的结果

            $error.clear()
        $List = New-Object PSObject
        Invoke-Command -ComputerName $server -ScriptBlock {
            $smbv1 = (Get-SmbServerConfiguration | Select EnableSMB1Protocol)
            $smbv1 | Select-Object EnableSMB1Protocol} -OutVariable List    
    
        foreach($item in $list.EnableSMB1Protocol){
            if($error -ne $null){
                $item = "unknown"
                $msg = $error.Exception.Message
                ac .\smb1errors.txt "$Server, $msg"
            }
    

    由于$error变量是一个.NET对象,为了清除它,我需要传递它参数()。然后执行代码,在本例中检查SMBv1服务,并测试$error是否仍然为$null。如果$error变量包含内容,我会将其捕获到变量中$msg=$error.Exception.Message

    将标准输出和标准错误保存到单独的变量中。如果没有美元符号,它将无法工作(来自Windows Powershell的运行)


    我更新了这篇文章的内容,让事情变得更全面。当然,您的代码可以工作,但不能用于任意表达式,也就是说,我没有关于我执行的表达式的信息。换句话说,我如何区分“try{throw 111;}catch{}”和简单的“throw 111”?它们都有非空的$error变量。我唯一可以使用的是$?,但它不适用于“Get service aaaa”。
    $err = $( $output = get-childitem foo3 ) 2>&1