How do I capture the password expire prompt within WinSCP session logging?
I have try/catch blocks in my script and within the catch block I write to eventviewer to log any exceptions/errors thrown. However, for this one sftp the password was expired and the session log shows the prompt from the server saying password is expired, but the exception thrown doesn't show that - it just says authentication failed. I know its basically the same thing, but i would prefer to accurately catch the server response so when im alerted in eventviewer I can easily see the issue.
You can see how under the "NESTED CATCH ERROR" that the exception doesn't clearly show that the password is expired. Anyone know how I can capture that server response from the session instead of searching the log for "password expire"? Here is my code within the catch if that helps
. 2020-08-21 10:20:50.606 Prompt (keyboard interactive, "SSH server: Password Authentication", "Enter password for USERNAME", "Password:") . 2020-08-21 10:20:50.607 Using stored password. . 2020-08-21 10:20:50.753 Prompt (keyboard interactive, "SSH server: Password Expired", "Your password has expired. Enter new password for USERNAME", "New Password:"1 more) . 2020-08-21 10:20:50.754 User aborted during keyboard-interactive authentication . 2020-08-21 10:20:50.755 Attempt to close connection due to fatal exception: * 2020-08-21 10:20:50.755 **Connection has been unexpectedly closed.** Server sent command exit status 0. . 2020-08-21 10:20:50.756 Closing connection. ***NESTED CATCH ERROR**** Exception calling "Open" with "1" argument(s): "Connection has been unexpectedly closed. Server sent command exit status 0. Authentication log (see session log for details): Using username "USERNAME". Welcome to ********* Authentication failed." End Error
catch{ # Write the error message to the console for visual, but also write to the eventlog for monitoring - note this also pulls errors from .check() in the transfer Write-Output "Error: $($_.Exception.Message)" $logMessage = $_.Exception.Message if ($errorWhitelist -notcontains $logMessage){ Write-EventLog -LogName Application -Source "External_SFTP_Script" -EventID 101 -EntryType Warning -Message $logMessage } Add-Content -Path $logFilePath "***NESTED CATCH ERROR****`n $logMessage`nEnd Error" Add-Content -Path $logFilePath $Error $Error.Clear() }