Powershell 控制台中未经请求的调试信息

Unrequested debugging Info in Powershell console

我遇到了一个令人讨厌的问题,我想消除在 Powershell 控制台和 ISE 中获取我不需要的调试信息的地方。这很麻烦,因为它妨碍了我想要的调试信息。

我不清楚这是特定于我编写脚本的工具 (WinSCP) 还是更通用的 PowerShell 行为。

简而言之,WinSCP.Session.Open 事件不会写出任何东西(很好!),但是 Close()Dispose() 方法非常繁琐,下面出现在每次调用我的控制台。

起初我认为这可能是将语句包含在 finally 块中的结果,但是将它们移动到 try 块会产生相同的麻烦行为。

基于一个类似但不完全相同的问题,我查看了 "preference variables"。我的股票价值列在最后,我唯一尝试更改无济于事的是 "Warning Preference" 到 "Silently COntinue",但没有效果

运行-示例:

PS F:\> getFirewallContents "AAA" 255.255.227.254
Attempting Session.Open  AAA  |  255.255.227.254
Completed  Session.Open  AAA  |  255.255.227.254
/var/db/commits exists, last write: 5/8/2015 11:33:22 AM
Closing Session  AAA

... two stanzas that follow are the undesired output ......    

MemberType          : Method
OverloadDefinitions : {System.Void Close()}
TypeNameOfValue     : System.Management.Automation.PSMethod
Value               : System.Void Close()
Name                : Close
IsInstance          : True

MemberType          : Method
OverloadDefinitions : {System.Void Dispose()}
TypeNameOfValue     : System.Management.Automation.PSMethod
Value               : System.Void Dispose()
Name                : Dispose
IsInstance          : True

Closed  Session  AAA

function getFirewallContents ([string] $fw_name, [string] $fw_ip) {
    $session = New-Object WinSCP.Session
    $sessionOptions = New-Object WinSCP.SessionOptions
    $xferOptions = New-Object WinSCP.TransferOptions

    $sessionOptions.HostName = $fw_ip
    $sessionOptions.UserName = "NOPE"
    $sessionOptions.Password = "NOT TELLING"
    $sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
    $sessionOptions.GiveUpSecurityAndAcceptAnySshHostKey = $TRUE

    $remotefile = "/var/db/commits"

    Try {
        Write-Host  "Attempting Session.Open " $fw_name " | " $sessionOptions.HostName 

        $session.Open($sessionOptions)

        Write-Host  "Completed  Session.Open " $fw_name " | " $sessionOptions.HostName      

        if ($session.FileExists($remotefile)) {
            Write-Host "$remotefile exists, last write:" $session.GetFileInfo($remotefile).LastWriteTime
        } else {
            Write-Host "$remotefile NOT FOUND"
        }
    } Catch {
        Write-Host "Something bad happened"
    } Finally {
        Write-Host "Closing Session  $fw_name "
        $session.Close
        $session.Dispose
        Write-Host "Closed  Session  $fw_name "
    }
}

Name                           Value
----                           -----
ConfirmPreference              High
DebugPreference                SilentlyContinue
ErrorActionPreference          Continue
ProgressPreference             Continue
VerbosePreference              SilentlyContinue
WarningPreference              Continue     (Tried Silently Continue, no effect)
WhatIfPreference               False

您在调用 .Close.Dispose 时缺少括号。所以你实际上 根本没有调用 方法,而是获取方法的地址而不使用它。作为最后的手段,PowerShell 在控制台上打印 "expression" 结果。

正确的语法是:

$session.Close()
$session.Dispose()