PowerShell 重定向并不总是有效

Ric*_*unn 9 powershell

重定向警告流New-PSSession,因为我相信它应该不起作用。无论我尝试抑制/重定向它做什么,我总是收到以下(黄色)警告消息到控制台:

WARNING: Using New-PSSession with Basic Authentication is going to be deprecated soon, checkout https://aka.ms/exops-docs for using Exchange Online V2 Module which uses Modern Authentication.
Run Code Online (Sandbox Code Playgroud)

PowerShell 重定向/流允许该消息渗透到控制台/std_out,我错过了什么?

什么不起作用

根据互联网的智慧,我尝试了以下方法:

New-PSSession *>$null ...
Run Code Online (Sandbox Code Playgroud)
New-PSSession ... | Out-Null
Run Code Online (Sandbox Code Playgroud)
New-PSSession *>$null ... -OutVariable x -ErrorVariable y -WarningVariable z
Run Code Online (Sandbox Code Playgroud)
New-PSSession *>$null ... -WarningAction SilentlyContinue
Run Code Online (Sandbox Code Playgroud)
$WarningPreference = 'SilentlyContinue'
New-PSSession ...
Run Code Online (Sandbox Code Playgroud)

我什至尝试临时控制std_out

$std_out = [System.Console]::Out
$out_writer = New-Object IO.StringWriter
[System.Console]::SetOut($out_writer)

$std_err = [System.Console]::Error
$err_writer = New-Object IO.StringWriter
[System.Console]::SetOut($err_writer)

$sess = New-PSSession ...

[System.Console]::SetOut($std_out)
[System.Console]::SetError($std_err)
Run Code Online (Sandbox Code Playgroud)

除了上面我能想到的每一个组合,还有我忘记的更多方法。

什么应该工作

Invoke-Command正如预期的那样,使用适用于其他警告的作品来测试这些技术中的每一种:


$std_out = [System.Console]::Out
$out_writer = New-Object IO.StringWriter
[System.Console]::SetOut($out_writer)

$std_err = [System.Console]::Error
$err_writer = New-Object IO.StringWriter
[System.Console]::SetOut($err_writer)

Invoke-Command -ScriptBlock {

    Write-Error "My Error"
    Write-Warning "My Warning"
    [console]::WriteLine('Directly to std_out!')

} *>$null -OutVariable x -ErrorVariable y -WarningVariable z

[System.Console]::SetOut($std_out)
[System.Console]::SetError($std_err)

Run Code Online (Sandbox Code Playgroud)

但是我尝试过的任何事情都不会抑制或重定向来自New-PSSession.

繁殖

param (
    $username,
    $password
)

$secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential ($username, $secpasswd)
$URL = "https://ps.outlook.com/powershell"
New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $URL -Credential $creds -Authentication Basic -AllowRedirection
Run Code Online (Sandbox Code Playgroud)

测试环境

视窗 10:

Name                           Value
----                           -----
PSVersion                      7.1.0-preview.2
PSEdition                      Core
GitCommitId                    7.1.0-preview.2
OS                             Microsoft Windows 10.0.18363
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
Run Code Online (Sandbox Code Playgroud)

视窗 10:

Name                           Value
----                           -----
PSVersion                      5.1.18362.752
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.18362.752
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
Run Code Online (Sandbox Code Playgroud)

服务器 2019:

Name                           Value
----                           -----
PSVersion                      5.1.17763.1007
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.17763.1007
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
Run Code Online (Sandbox Code Playgroud)

WinRM(适用于所有系统):

Config
    MaxEnvelopeSizekb = 500
    MaxTimeoutms = 60000
    MaxBatchItems = 32000
    MaxProviderRequests = 4294967295
    Client
        NetworkDelayms = 5000
        URLPrefix = wsman
        AllowUnencrypted = false
        Auth
            Basic = true
            Digest = true
            Kerberos = true
            Negotiate = true
            Certificate = true
            CredSSP = false
        DefaultPorts
            HTTP = 5985
            HTTPS = 5986
        TrustedHosts
    Service
        RootSDDL = O:NSG:BAD:P(A;;GA;;;BA)(A;;GR;;;IU)S:P(AU;FA;GA;;;WD)(AU;SA;GXGW;;;WD)
        MaxConcurrentOperations = 4294967295
        MaxConcurrentOperationsPerUser = 1500
        EnumerationTimeoutms = 240000
        MaxConnections = 300
        MaxPacketRetrievalTimeSeconds = 120
        AllowUnencrypted = false
        Auth
            Basic = false
            Kerberos = true
            Negotiate = true
            Certificate = false
            CredSSP = false
            CbtHardeningLevel = Relaxed
        DefaultPorts
            HTTP = 5985
            HTTPS = 5986
        IPv4Filter = *
        IPv6Filter = *
        EnableCompatibilityHttpListener = false
        EnableCompatibilityHttpsListener = false
        CertificateThumbprint
        AllowRemoteAccess = true
    Winrs
        AllowRemoteShellAccess = true
        IdleTimeout = 7200000
        MaxConcurrentUsers = 2147483647
        MaxShellRunTime = 2147483647
        MaxProcessesPerShell = 2147483647
        MaxMemoryPerShellMB = 2147483647
        MaxShellsPerUser = 2147483647
Run Code Online (Sandbox Code Playgroud)

sta*_*tor 4

通常,重定向输出就像您已经尝试过的那样。更具体:

New-PSSession -... 3> $null
Run Code Online (Sandbox Code Playgroud)

将重定向您不需要的警告,如3PowerShell 3.0 以来的警告流所述(请参阅文档)。

事实上,它在这种特殊情况下不起作用似乎是一个错误,并且几天前已经在这里提出了一个问题。


这篇文章中可能有一个可能的解释:

发生这种情况是因为Write-Host 未写入任何流。它被发送到主机程序,主机程序决定如何处理它。Windows PowerShell 控制台和 Windows PowerShell ISE 在控制台上显示主机消息。

好吧,它不再正确了(自 PowerShell 5.0 起),因为Write-Host现在它是信息流的包装器Write-Information,因此写入信息流 (6)。但有趣的是,有些主机消息没有写入任何流。这里可能就是这种情况,您无法通过重定向所有流来抑制控制台输出。因此,“警告”可能是通过主机消息转发的,而不是使用任何流。

我看到了几次抑制这些主机消息的尝试(此处此处),但据我所知,对您的情况没有任何帮助。

引用的文章进一步指出:

[...] 发送给主机程序的消息(无法抑制或重定向)。