【发布时间】:2020-08-16 09:05:55
【问题描述】:
重定向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.
关于允许该消息渗透到控制台/std_out 的 PowerShell 重定向/流,我缺少什么?
什么不起作用
根据互联网的智慧,我尝试了以下方法:
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
服务器 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
【问题讨论】:
-
3>$null或*>$null应该可以工作,但我无法在此处复制该特定警告以进行验证。 -
为我工作。
$('hi'; write-warning warning) 3>$null -
我无法复制,因为不推荐使用基本身份验证,但是添加“-WarningAction SilentlyContinue”怎么样?
-
@Brice 感谢您的建议,但这是我提到的“更多方法”之一。我会更新答案以包含它。
-
我在尝试时没有收到该错误,
.\test joe here。我可以像往常一样隐藏其他警告。
标签: powershell