I have a powershell script that calls the following command:
plink -batch -ssh $defUser@$srv -pw $defPassword -m $executeCommandFile
The problem that $defPassword is not always the same/correct. How can I catch Access denied error from plink?
Something like this:
if (plink -batch -ssh $defUser@$srv -pw $defPassword -ne "Access denied") \\execute -m $executeCommandFile
else \\use different $defPassword(for example $defPassword2) and then executeThink it's like try\catch but with plink used password
1 Answer
"Access is denied" message appears in error stream. You can use the Windows PowerShell redirection operators e.g as follows:
$aux = . plink -batch -ssh $defUser@$srv -pw $defPassword -m $executeCommandFile *>&1
if ( $aux -match '^Access.*denied' ) { ### the specified string found: use different $defPassword
} else { ### success $aux ### show plink results
}Note that 2>&1 could suffice:
0
*>&1sends all output types (*) to the success output stream;2>&1sends errors (2) and success output (1) to the success output stream.