gpt4 book ai didi

PowerShell重定向并非始终有效

转载 作者:行者123 更新时间:2023-12-02 22:10:19 25 4
gpt4 key购买 nike

重定向警告流以获取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.

我缺少关于PowerShell重定向/流的信息,使该消息渗透到控制台/std_out吗?

什么不起作用

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

New-PSSession *>$null ...

New-PSSession ... | Out-Null

New-PSSession *>$null ... -OutVariable x -ErrorVariable y -WarningVariable z

New-PSSession *>$null ... -WarningAction SilentlyContinue

$WarningPreference = 'SilentlyContinue'
New-PSSession ...

我什至尝试对 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)

除了上述所有我能想到的组合之外,还有我遗忘的其他几种方法。

什么应该工作

如预期的那样,使用 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)


但是我尝试过的任何方法都不会抑制或重定向 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

经过测试的环境

Windows 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

Windows 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

Server 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

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

最佳答案

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

New-PSSession -... 3> $null

会重定向您不需要的警告,因为 3描述了自PowerShell 3.0( see docs以来)的警告流。

在特定情况下不起作用的事实似乎是一个错误,几天前就已经存在一个问题 here

this article中可能有一个可能的解释:

This happens because Write-Host is not written to any stream. It's sent to the host program, and the host program decides what to do with it. The Windows PowerShell console and Windows PowerShell ISE display host messages on the console.



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

我看到了几种抑制这些主机消息的尝试( herehere),但是据我所知,在您的情况下没有任何帮助。

引用的文章进一步指出:

[...] messages to the host program (which can't be suppressed or redirected).

关于PowerShell重定向并非始终有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61549872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com