在PowerShell中从远程会话重定向详细流

时间:2019-07-09 17:22:53

标签: powershell

我想将远程会话的详细输出保存到变量或文件中

仅普通输出可以重定向,而冗长的输出不能重定向。

$VerbosePreference = "Continue"

$s = New-PSSession localhost
$str = (Invoke-Command -Session $s -Verbose -Script {
    $VerbosePreference = "Continue"
    "1"
    Write-Verbose "2"
} *>&1)

"str=$str"

预期输出:

str=1 2

实际输出:

VERBOSE: 2
str=1

1 个答案:

答案 0 :(得分:0)

在脚本块内重定向详细输出 。要么使用

$s = New-PSSession -ComputerName localhost
$str = Invoke-Command -Verbose -Session $s -ScriptBlock {
    "1"
    Write-Verbose "2" -Verbose 4>&1
}
"str=$str"

甚至

$s = New-PSSession -ComputerName localhost
$str = Invoke-Command -Verbose -Session $s -ScriptBlock {
    $(
        "1"
        Write-Verbose "2" -Verbose
    ) *>&1
}
"str=$str"

以上代码段均未按预期使用 default 详细首选项(即$VerbosePreference = 'SilentlyContinue')工作。

相关问题