Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unrequested debugging Info in Powershell console

I'm having a nuisance issue I'd like to eliminate where I get debugging info I am not asking for in a Powershell console and ISE. It's a nuisance in that it gets in the way of my desired debugging info.

I'm not clear if this is specific to the tool I am scripting (WinSCP), or a more generic PowerShell behavior.

In a nutshell, WinSCP.Session.Open events don't write out anything out (good!), but the Close() and Dispose() methods are quite chatty, with the below appearing in my console on each invocation.

At first I thought this might be the consequence of including the statements in a finally block, but moving them to the try block yields the same nuisance behavior.

Based on a similar, but not identical issue, I checked out the "preference variables". My stock values are listed at the very end, the only one I tried to change to no avail was the "Warning Preference" to "Silently COntinue", which had no effect

Run-Example:

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
like image 471
dave_the_dev Avatar asked Apr 01 '26 19:04

dave_the_dev


1 Answers

You are missing brackets in call to .Close and .Dispose. So you are actually not calling the methods at all, but rather taking an address of the methods and not using it. PowerShell prints the "expression" results on a console as a last resort.

The correct syntax is:

$session.Close()
$session.Dispose()
like image 70
Martin Prikryl Avatar answered Apr 03 '26 13:04

Martin Prikryl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!