<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Try / Catch not working as expected for Invoke-SSHStreamExpectSecureAction in VMware PowerCLI Discussions</title>
    <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Try-Catch-not-working-as-expected-for-Invoke/m-p/2848012#M101550</link>
    <description>&lt;P&gt;Great idea but, there's no Out-Variable option for&amp;nbsp;&lt;SPAN&gt;$sshStream.Read()&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I may have "workable" solution.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Assuming successful authentication for privileged mode, i can run a privileged command and capture the result of that command to a variable. That variable can be written to the results array.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;If authentication to privileged mode fails, the command will error. The error can be written to the results array.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Examining the errors will identify the incorrect privileged credentials.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;(it's not the best solution, but the end result will be good enough)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if ($sshSession.Connected -like "True") {&lt;BR /&gt;$resultsProperty.Add("Username",$sshUsername)&lt;BR /&gt;$resultsProperty.Add("Password",$password)&lt;BR /&gt;&lt;BR /&gt;Write-Host "SSh session to $($item.VIP_FQDN) is connected, checking $($rootUser) credentials now"&lt;BR /&gt;$sshStream = New-SSHShellStream -index $sshSession.SessionId&lt;BR /&gt;Invoke-SSHStreamExpectSecureAction -ShellStream $SSHStream -Command 'st en' -ExpectString "Password:" -SecureAction ($sshRootPass) -Verbose&lt;BR /&gt;$sshStream.read()&amp;nbsp;&lt;BR /&gt;Start-Sleep -Seconds 3&lt;BR /&gt;$sshStream.WriteLine("pwd")&lt;BR /&gt;Start-Sleep -Seconds 3&lt;BR /&gt;$sshStream.Read()&lt;BR /&gt;&lt;BR /&gt;# Verify it's possible to run an elevated command. If error returned, password provided for 'st en' was incorrect and authentication failed&amp;nbsp;&lt;BR /&gt;$hwPlat = Invoke-SSHStreamShellCommand -ShellStream $sshStream -Command 'uname -i'&lt;BR /&gt;&lt;BR /&gt;$resultsProperty.Add("RootUser",$rootUser)&lt;BR /&gt;$resultsProperty.Add("RootPass",$rootPass)&lt;BR /&gt;$resultsProperty.Add("HardwarePlatform",$hwPlat)&lt;BR /&gt;&lt;BR /&gt;Write-Host " Disconnecting ssh session to $($item.VIP_FQDN) "&lt;BR /&gt;Remove-SSHSession -SessionId $sshSession.SessionId | Out-Null&lt;BR /&gt;Write-Host ""&lt;/P&gt;&lt;P&gt;} else {&lt;BR /&gt;Write-host "Password for $($item.VIP_FQDN) not working" -ForegroundColor Red&lt;BR /&gt;$_.exception.Message&lt;BR /&gt;$resultsProperty.Add("Username",$sshUsername)&lt;BR /&gt;$resultsProperty.Add("Password","Incorrect")&lt;BR /&gt;$resultsProperty.Add("RootUser",$rootUser)&lt;BR /&gt;$resultsProperty.Add("RootPass","Unable to SSH so cannot check Privileged Password")&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;$resultsArray += New-Object -TypeName psobject -Property $resultsProperty&lt;BR /&gt;$i++&lt;BR /&gt;}&lt;BR /&gt;$resultsArray | Sort-Object -Property {($_ | Get-Member -MemberType NoteProperty).Count } -Descending | Out-GridView&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 19 May 2021 21:21:23 GMT</pubDate>
    <dc:creator>piercj2</dc:creator>
    <dc:date>2021-05-19T21:21:23Z</dc:date>
    <item>
      <title>Try / Catch not working as expected for Invoke-SSHStreamExpectSecureAction</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Try-Catch-not-working-as-expected-for-Invoke/m-p/2847835#M101529</link>
      <description>&lt;P&gt;Hi, i'm trying to verify SSH and root credentials for multiple different systems.&lt;/P&gt;&lt;P&gt;The below script takes as input a .csv file containing the IP of the target systems and associated ssh and root credentials.&lt;/P&gt;&lt;P&gt;The output of the script is a second array, containing the verified Credentials, if any Password is incorrect, this is flagged in the array.&lt;/P&gt;&lt;P&gt;The Try/Catch below is not working as expected for privileged credentials, that is when an incorrect privileged mode password is entered, the expected "Incorrect privileged password passed" is not entered into the results array.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Function Check-Creds {&lt;BR /&gt;&amp;lt;#&lt;BR /&gt;.SYNOPSIS&lt;BR /&gt;Read text file containing System Manager Credentials and returns verify Credentials are working&lt;/P&gt;&lt;P&gt;.EXAMPLE&lt;BR /&gt;Check-Creds -Path C:\temp\list.csv&lt;BR /&gt;.NOTE&lt;BR /&gt;.csv file has the following column headings, VIP_FQDN,VIP_IP,Username,Password,RootUser,RootPass&lt;BR /&gt;#&amp;gt;&lt;/P&gt;&lt;P&gt;param(&lt;BR /&gt;[Parameter(Mandatory)]&lt;BR /&gt;[System.IO.FileInfo]$Path&lt;BR /&gt;)&lt;/P&gt;&lt;P&gt;If (!(Get-Module -ListAvailable Posh-SSH)) {&lt;BR /&gt;find-Module -Name Posh-SSH | Install-Module -Scope CurrentUser -Confirm:$false&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;If (!(Get-module -ListAvailable "ImportExcel")) {&lt;BR /&gt;Find-Module -Name ImportExcel | Install-Module -Scope CurrentUser&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;$systemMgmtArray = @()&lt;BR /&gt;$systemMgmtArray = Import-Csv $Path&lt;BR /&gt;$resultsArray = @()&lt;/P&gt;&lt;P&gt;$count = $systemMgmtArray | Measure-Object&lt;BR /&gt;$i = 1&lt;BR /&gt;&lt;BR /&gt;foreach ($item in $systemMgmtArray){&lt;BR /&gt;Write-Progress -Activity "Checking Credentials of System Managers" -Status "Working on $($item.VIP_FQDN)" -PercentComplete (($i*100)/$count.Count)&lt;/P&gt;&lt;P&gt;$resultsProperty = [ordered] @{&lt;BR /&gt;'VIP_FQDN'=$item.VIP_FQDN&lt;BR /&gt;'VIP_IP' = $item.VIP_IP&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;$sshTarget = $item.VIP_IP&lt;BR /&gt;$sshUsername = $item.Username&lt;BR /&gt;$password = $item.Password&lt;BR /&gt;$sshPassword = $password | ConvertTo-SecureString -AsPlainText -Force&lt;BR /&gt;$sshPassword.MakeReadOnly()&lt;BR /&gt;$sshCred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $sshUsername,$sshPassword&lt;BR /&gt;$sshCredential = Get-Credential -Credential $sshCred&lt;BR /&gt;$rootUser = $item.RootUser&lt;BR /&gt;$rootPass = $item.RootPass&lt;BR /&gt;$sshRootPass = $rootPass | ConvertTo-SecureString -AsPlainText -Force&lt;BR /&gt;$sshRootPass.MakeReadOnly()&lt;/P&gt;&lt;P&gt;Write-Host "Connecting to $($item.VIP_FQDN)"&lt;BR /&gt;$sshSession = New-SSHSession -ComputerName $item.VIP_IP -Credential $sshCredential -AcceptKey:$true&lt;/P&gt;&lt;P&gt;if ($sshSession.Connected -like "True") {&lt;BR /&gt;$resultsProperty.Add("Username",$sshUsername)&lt;BR /&gt;$resultsProperty.Add("Password",$password)&lt;BR /&gt;Write-Host "SSh session to $($item.VIP_FQDN) is connected, checking $($rootUser) credentials now"&lt;BR /&gt;try {&lt;BR /&gt;$sshStream = New-SSHShellStream -index $sshSession.SessionId&lt;BR /&gt;Invoke-SSHStreamExpectSecureAction -ShellStream $SSHStream -Command 'st en' -ExpectString "Password:" -SecureAction ($sshRootPass) -Verbose&lt;BR /&gt;$sshStream.read()&lt;BR /&gt;$sshStream.WriteLine("pwd")&lt;BR /&gt;Start-Sleep -Seconds 3&lt;BR /&gt;$sshStream.Read()&lt;BR /&gt;Start-Sleep -Seconds 3&lt;BR /&gt;&lt;BR /&gt;$resultsProperty.Add("RootUser",$rootUser)&lt;BR /&gt;$resultsProperty.Add("RootPass",$rootPass)&lt;BR /&gt;} catch {&lt;BR /&gt;$_.exception.Message&lt;BR /&gt;Write-Host "Unable to enter Privileged mode, incorrect $($item.RootUser) password supplied"&lt;BR /&gt;$resultsProperty.Add("RootUser",$rootUser)&lt;BR /&gt;$resultsProperty.Add("RootPass","Incorrect privileged password passed")&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;Write-Host " Disconnecting ssh session to $($item.VIP_FQDN) "&lt;BR /&gt;Remove-SSHSession -SessionId $sshSession.SessionId | Out-Null&lt;BR /&gt;Write-Host ""&lt;/P&gt;&lt;P&gt;} else {&lt;BR /&gt;Write-host "Password for $($item.VIP_FQDN) not working" -ForegroundColor Red&lt;BR /&gt;$_.exception.Message&lt;BR /&gt;$resultsProperty.Add("Username",$sshUsername)&lt;BR /&gt;$resultsProperty.Add("Password","Incorrect ssh Password")&lt;BR /&gt;$resultsProperty.Add("RootUser",$rootUser)&lt;BR /&gt;$resultsProperty.Add("RootPass","Unable to SSH so cannot check Privileged Password")&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;$resultsArray += New-Object -TypeName psobject -Property $resultsProperty&lt;BR /&gt;$i++&lt;BR /&gt;}&lt;BR /&gt;$resultsArray | Out-GridView&lt;BR /&gt;&lt;BR /&gt;$downloadsDir = "$HOME\Downloads"&lt;BR /&gt;if (Test-Path "$downloadsDir\Credential_Check.xlsx") {Remove-Item "$downloadsDir\Credential_Check.xlsx"}&lt;BR /&gt;$resultsArray | Export-Excel -Path "$downloadsDir\Credential_Check.xlsx"&lt;BR /&gt;Invoke-Item "$downloadsDir\Credential_Check.xlsx"&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Output from the above is&lt;/P&gt;&lt;P&gt;Connecting to &lt;A href="https://server1.acme.com" target="_blank"&gt;https://server1.acme.com&lt;/A&gt;&lt;BR /&gt;SSh session to &lt;A href="https://server1.acme.com" target="_blank"&gt;https://server1.acme.com&lt;/A&gt; is connected, checking root credentials now&lt;BR /&gt;VERBOSE: Executing command st en.&lt;BR /&gt;VERBOSE: Waiting for match.&lt;BR /&gt;VERBOSE: Matching by String.&lt;BR /&gt;VERBOSE: Executing action.&lt;BR /&gt;VERBOSE: Action has been executed.&lt;BR /&gt;True&lt;/P&gt;&lt;P&gt;pwd&lt;BR /&gt;su: Authentication failure&lt;BR /&gt;PC1RNNSXTNPROD02&amp;gt; pwd&lt;BR /&gt;% Command not found: pwd&lt;BR /&gt;server1&amp;gt;&lt;BR /&gt;Disconnecting ssh session to &lt;A href="https://server1.acme.com" target="_blank"&gt;https://server1.acme.com&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;With the result of "su: Authentication failure", I was expecting to drop out of the Try statement and for the Catch to be used.&lt;/P&gt;&lt;P&gt;What am I missing (not understanding) ?&lt;/P&gt;</description>
      <pubDate>Wed, 19 May 2021 08:34:38 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Try-Catch-not-working-as-expected-for-Invoke/m-p/2847835#M101529</guid>
      <dc:creator>piercj2</dc:creator>
      <dc:date>2021-05-19T08:34:38Z</dc:date>
    </item>
    <item>
      <title>Re: Try / Catch not working as expected for Invoke-SSHStreamExpectSecureAction</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Try-Catch-not-working-as-expected-for-Invoke/m-p/2847842#M101532</link>
      <description>&lt;P&gt;Have you tried adding &lt;STRONG&gt;-ErrorAction Stop&lt;/STRONG&gt; on the&amp;nbsp;&lt;SPAN&gt;Invoke-SSHStreamExpectSecureAction cmdlet?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 19 May 2021 08:44:35 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Try-Catch-not-working-as-expected-for-Invoke/m-p/2847842#M101532</guid>
      <dc:creator>LucD</dc:creator>
      <dc:date>2021-05-19T08:44:35Z</dc:date>
    </item>
    <item>
      <title>Re: Try / Catch not working as expected for Invoke-SSHStreamExpectSecureAction</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Try-Catch-not-working-as-expected-for-Invoke/m-p/2847965#M101544</link>
      <description>&lt;P&gt;Hi Luc, i had tried -erroraction stop as follows, it didn't help&lt;/P&gt;&lt;P&gt;try {&lt;BR /&gt;$sshStream = New-SSHShellStream -index $sshSession.SessionId&lt;BR /&gt;Invoke-SSHStreamExpectSecureAction -ShellStream $SSHStream -Command 'st en' -ExpectString "Password:" -SecureAction ($sshRootPass) -Verbose -ErrorAction Stop&lt;BR /&gt;$sshStream.read()&lt;BR /&gt;$sshStream.WriteLine("pwd")&lt;BR /&gt;Start-Sleep -Seconds 3&lt;BR /&gt;$sshStream.Read()&lt;BR /&gt;Start-Sleep -Seconds 3&lt;BR /&gt;&lt;BR /&gt;$resultsProperty.Add("RootUser",$rootUser)&lt;BR /&gt;$resultsProperty.Add("RootPass",$rootPass)&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It looks like there is no error passing the&amp;nbsp;Invoke-SSHStreamExpectSecureAction command. The output from this is&lt;/P&gt;&lt;P&gt;VERBOSE: Executing command st en.&lt;BR /&gt;VERBOSE: Waiting for match.&lt;BR /&gt;VERBOSE: Matching by String.&lt;BR /&gt;VERBOSE: Executing action.&lt;BR /&gt;VERBOSE: Action has been executed.&lt;BR /&gt;&lt;STRONG&gt;True&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;su: Authentication failure&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;I believe Invoke-SSHStreamExpectSecureAction executes without error, hence "true" in the output.&lt;/P&gt;&lt;P&gt;I think the error happens when the&amp;nbsp;&lt;STRONG&gt;$sshStream.read()&lt;/STRONG&gt; takes an incorrect password from&amp;nbsp;SecureAction ($sshRootPass), that is when&amp;nbsp;&lt;STRONG&gt;su: Authentication failure&lt;/STRONG&gt; happens.&lt;/P&gt;&lt;P&gt;I don't know how to manage this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 May 2021 16:57:55 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Try-Catch-not-working-as-expected-for-Invoke/m-p/2847965#M101544</guid>
      <dc:creator>piercj2</dc:creator>
      <dc:date>2021-05-19T16:57:55Z</dc:date>
    </item>
    <item>
      <title>Re: Try / Catch not working as expected for Invoke-SSHStreamExpectSecureAction</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Try-Catch-not-working-as-expected-for-Invoke/m-p/2847966#M101545</link>
      <description>&lt;P&gt;You might be right, but I suspect the error happens inside shell where you launch the 'su' command.&lt;BR /&gt;And that obviously doesn't trigger an exception.&lt;BR /&gt;&lt;BR /&gt;Is this by any chance against an Ubuntu box?&lt;/P&gt;
&lt;P&gt;Shouldn't that be 'sudo su' instead of just 'su'?&lt;/P&gt;</description>
      <pubDate>Wed, 19 May 2021 17:12:36 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Try-Catch-not-working-as-expected-for-Invoke/m-p/2847966#M101545</guid>
      <dc:creator>LucD</dc:creator>
      <dc:date>2021-05-19T17:12:36Z</dc:date>
    </item>
    <item>
      <title>Re: Try / Catch not working as expected for Invoke-SSHStreamExpectSecureAction</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Try-Catch-not-working-as-expected-for-Invoke/m-p/2847985#M101546</link>
      <description>&lt;P&gt;you could be right, the error is probably happening inside the shell, not in PowerCLI, i hadn't thought of that.&lt;/P&gt;&lt;P&gt;If that is the case, is there any way of managing the messages that are displayed from -verbose ?&lt;/P&gt;&lt;P&gt;One of the outputs from -verbose is "su: Authentication failure", is there any way to manage/use this ?&lt;/P&gt;&lt;P&gt;The boxes in question are NSX Managers, entering privileged mode is done with "start enable", or "st en"&lt;/P&gt;</description>
      <pubDate>Wed, 19 May 2021 18:36:39 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Try-Catch-not-working-as-expected-for-Invoke/m-p/2847985#M101546</guid>
      <dc:creator>piercj2</dc:creator>
      <dc:date>2021-05-19T18:36:39Z</dc:date>
    </item>
    <item>
      <title>Re: Try / Catch not working as expected for Invoke-SSHStreamExpectSecureAction</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Try-Catch-not-working-as-expected-for-Invoke/m-p/2847990#M101547</link>
      <description>&lt;P&gt;You could capture the text returned by&amp;nbsp;&lt;SPAN&gt;$sshStream.Read() in a variable.&lt;BR /&gt;And then examine the content with an If-Then-Else, or something similar.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 19 May 2021 18:57:57 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Try-Catch-not-working-as-expected-for-Invoke/m-p/2847990#M101547</guid>
      <dc:creator>LucD</dc:creator>
      <dc:date>2021-05-19T18:57:57Z</dc:date>
    </item>
    <item>
      <title>Re: Try / Catch not working as expected for Invoke-SSHStreamExpectSecureAction</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Try-Catch-not-working-as-expected-for-Invoke/m-p/2848012#M101550</link>
      <description>&lt;P&gt;Great idea but, there's no Out-Variable option for&amp;nbsp;&lt;SPAN&gt;$sshStream.Read()&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I may have "workable" solution.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Assuming successful authentication for privileged mode, i can run a privileged command and capture the result of that command to a variable. That variable can be written to the results array.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;If authentication to privileged mode fails, the command will error. The error can be written to the results array.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Examining the errors will identify the incorrect privileged credentials.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;(it's not the best solution, but the end result will be good enough)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if ($sshSession.Connected -like "True") {&lt;BR /&gt;$resultsProperty.Add("Username",$sshUsername)&lt;BR /&gt;$resultsProperty.Add("Password",$password)&lt;BR /&gt;&lt;BR /&gt;Write-Host "SSh session to $($item.VIP_FQDN) is connected, checking $($rootUser) credentials now"&lt;BR /&gt;$sshStream = New-SSHShellStream -index $sshSession.SessionId&lt;BR /&gt;Invoke-SSHStreamExpectSecureAction -ShellStream $SSHStream -Command 'st en' -ExpectString "Password:" -SecureAction ($sshRootPass) -Verbose&lt;BR /&gt;$sshStream.read()&amp;nbsp;&lt;BR /&gt;Start-Sleep -Seconds 3&lt;BR /&gt;$sshStream.WriteLine("pwd")&lt;BR /&gt;Start-Sleep -Seconds 3&lt;BR /&gt;$sshStream.Read()&lt;BR /&gt;&lt;BR /&gt;# Verify it's possible to run an elevated command. If error returned, password provided for 'st en' was incorrect and authentication failed&amp;nbsp;&lt;BR /&gt;$hwPlat = Invoke-SSHStreamShellCommand -ShellStream $sshStream -Command 'uname -i'&lt;BR /&gt;&lt;BR /&gt;$resultsProperty.Add("RootUser",$rootUser)&lt;BR /&gt;$resultsProperty.Add("RootPass",$rootPass)&lt;BR /&gt;$resultsProperty.Add("HardwarePlatform",$hwPlat)&lt;BR /&gt;&lt;BR /&gt;Write-Host " Disconnecting ssh session to $($item.VIP_FQDN) "&lt;BR /&gt;Remove-SSHSession -SessionId $sshSession.SessionId | Out-Null&lt;BR /&gt;Write-Host ""&lt;/P&gt;&lt;P&gt;} else {&lt;BR /&gt;Write-host "Password for $($item.VIP_FQDN) not working" -ForegroundColor Red&lt;BR /&gt;$_.exception.Message&lt;BR /&gt;$resultsProperty.Add("Username",$sshUsername)&lt;BR /&gt;$resultsProperty.Add("Password","Incorrect")&lt;BR /&gt;$resultsProperty.Add("RootUser",$rootUser)&lt;BR /&gt;$resultsProperty.Add("RootPass","Unable to SSH so cannot check Privileged Password")&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;$resultsArray += New-Object -TypeName psobject -Property $resultsProperty&lt;BR /&gt;$i++&lt;BR /&gt;}&lt;BR /&gt;$resultsArray | Sort-Object -Property {($_ | Get-Member -MemberType NoteProperty).Count } -Descending | Out-GridView&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 May 2021 21:21:23 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Try-Catch-not-working-as-expected-for-Invoke/m-p/2848012#M101550</guid>
      <dc:creator>piercj2</dc:creator>
      <dc:date>2021-05-19T21:21:23Z</dc:date>
    </item>
    <item>
      <title>Re: Try / Catch not working as expected for Invoke-SSHStreamExpectSecureAction</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Try-Catch-not-working-as-expected-for-Invoke/m-p/2848046#M101552</link>
      <description>&lt;P&gt;Can't you redirect the stream output to a variable?&lt;/P&gt;</description>
      <pubDate>Thu, 20 May 2021 05:48:18 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Try-Catch-not-working-as-expected-for-Invoke/m-p/2848046#M101552</guid>
      <dc:creator>LucD</dc:creator>
      <dc:date>2021-05-20T05:48:18Z</dc:date>
    </item>
    <item>
      <title>Re: Try / Catch not working as expected for Invoke-SSHStreamExpectSecureAction</title>
      <link>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Try-Catch-not-working-as-expected-for-Invoke/m-p/2848084#M101558</link>
      <description>&lt;P&gt;I'd looked at redirecting the verbose output yesterday (&lt;A href="https://docs.microsoft.com/en-gb/powershell/module/microsoft.powershell.core/about/about_redirection?view=powershell-7.1" target="_blank"&gt;about_Redirection - PowerShell | Microsoft Docs&lt;/A&gt;). I didn't have any success so didn't think this was the solution. With you suggesting that this was the solution, I took another look. As usual Luc, you are the man !&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It took some trial and error, redirecting the verbose output from various locations but, eventually I struck gold &lt;img class="lia-deferred-image lia-image-emoji" src="https://communities.vmware.com/html/@DCF4E2F7991292CEECF250394DB2C2BC/emoticons/1f642.png" alt=":slightly_smiling_face:" title=":slightly_smiling_face:" /&gt;&lt;/P&gt;&lt;P&gt;Below is the working code, i've left in the comments, write-host, etc, as it made it easier for me to see what's happening.&amp;nbsp;&lt;/P&gt;&lt;P&gt;# If you can ssh, check elevated privileges by entering 'st en'&lt;BR /&gt;if ($sshSession.Connected -like "True") {&lt;BR /&gt;$resultsProperty.Add("Username",$sshUsername)&lt;BR /&gt;$resultsProperty.Add("Password",$password)&lt;BR /&gt;&lt;BR /&gt;Write-Host "SSh session to $($item.VIP_FQDN) is connected, checking $($rootUser) credentials now"&lt;BR /&gt;$sshStream = New-SSHShellStream -index $sshSession.SessionId&lt;BR /&gt;Invoke-SSHStreamExpectSecureAction -ShellStream $SSHStream -Command 'st en' -ExpectString "Password:" -SecureAction ($sshRootPass) -Verbose&lt;BR /&gt;$sshStream.read()&lt;BR /&gt;Start-Sleep -Seconds 5&lt;BR /&gt;$verboseOut = $sshStream.Read() 4&amp;gt;&amp;amp;1&lt;BR /&gt;Write-Host -ForegroundColor blue "what Luc said .... $verboseOut"&lt;BR /&gt;if ($verboseOut -like '*su: Authentication failure*'){&lt;BR /&gt;Write-Host -ForegroundColor DarkGreen "got the error :)"&lt;BR /&gt;$resultsProperty.Add("RootUser",$rootUser)&lt;BR /&gt;$resultsProperty.Add("RootPass","Incorrect privileged password")&lt;BR /&gt;}else{&lt;BR /&gt;Write-Host -ForegroundColor DarkGreen "su Authentication successful"&lt;BR /&gt;$resultsProperty.Add("RootUser",$rootUser)&lt;BR /&gt;$resultsProperty.Add("RootPass",$rootPass)&lt;BR /&gt;}&lt;BR /&gt;# Verify it's possible to run an elevated command. If error returned, password provided for 'st en' was incorrect and authentication failed inside the remote shell&lt;BR /&gt;$hwPlat = Invoke-SSHStreamShellCommand -ShellStream $sshStream -Command 'uname -i'&lt;BR /&gt;$resultsProperty.Add("HardwarePlatform",$hwPlat)&lt;BR /&gt;&lt;BR /&gt;Write-Host " Disconnecting ssh session to $($item.VIP_FQDN) "&lt;BR /&gt;Remove-SSHSession -SessionId $sshSession.SessionId | Out-Null&lt;BR /&gt;Write-Host -ForegroundColor DarkGray "------------------------------------------------------------------"&lt;/P&gt;&lt;P&gt;} else {&lt;BR /&gt;Write-Warning "Password for $($item.VIP_FQDN) not working"&lt;BR /&gt;Write-Host -ForegroundColor DarkGray "------------------------------------------------------------------"&lt;BR /&gt;$_.exception.Message&lt;BR /&gt;$resultsProperty.Add("Username",$sshUsername)&lt;BR /&gt;$resultsProperty.Add("Password","Incorrect")&lt;BR /&gt;$resultsProperty.Add("RootUser",$rootUser)&lt;BR /&gt;$resultsProperty.Add("RootPass","Unable to SSH so cannot check Privileged Password")&lt;BR /&gt;$resultsProperty.Add("HardwarePlatform","Unable to SSH so cannot check Privileged commands")&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Output is as below&lt;/P&gt;&lt;P&gt;&lt;FONT color="#99CC00"&gt;Connecting to &lt;A href="https://server1.acme.com" target="_blank"&gt;https://server1.acme.com&lt;/A&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#99CC00"&gt;SSH session to &lt;A href="https://server1.acme.com" target="_blank"&gt;https://server1.acme.com&lt;/A&gt; is connected, checking root credentials now&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#00FFFF"&gt;VERBOSE: Executing command st en.&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#00FFFF"&gt;VERBOSE: Waiting for match.&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#00FFFF"&gt;VERBOSE: Matching by String.&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#00FFFF"&gt;VERBOSE: Executing action.&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#00FFFF"&gt;VERBOSE: Action has been executed.&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#99CC00"&gt;True&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT color="#3366FF"&gt;what Luc said .... &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#3366FF"&gt;su: Authentication failure&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#3366FF"&gt;server1&amp;gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#008000"&gt;got the error &lt;img class="lia-deferred-image lia-image-emoji" src="https://communities.vmware.com/html/@DCF4E2F7991292CEECF250394DB2C2BC/emoticons/1f642.png" alt=":slightly_smiling_face:" title=":slightly_smiling_face:" /&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#99CC00"&gt;Disconnecting ssh session to &lt;A href="https://server1.acme.com" target="_blank"&gt;https://server1.acme.com&lt;/A&gt;&lt;/FONT&gt;&lt;BR /&gt;--------------------------------------------------------&lt;BR /&gt;&lt;FONT color="#99CC00"&gt;Connecting to &lt;A href="https://server2.acme.com" target="_blank"&gt;https://server2.acme.com&lt;/A&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#99CC00"&gt;SSh session to &lt;A href="https://server2.acme.com" target="_blank"&gt;https://server2.acme.com&lt;/A&gt; is connected, checking root credentials now&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#00FFFF"&gt;VERBOSE: Executing command st en.&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#00FFFF"&gt;VERBOSE: Waiting for match.&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#00FFFF"&gt;VERBOSE: Matching by String.&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#00FFFF"&gt;VERBOSE: Executing action.&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#00FFFF"&gt;VERBOSE: Action has been executed.&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#99CC00"&gt;True&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT color="#3366FF"&gt;what Luc said .... &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#3366FF"&gt;***************************************************************************&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#3366FF"&gt;NOTICE TO USERS&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#3366FF"&gt;WARNING! Changes made while logged in as the root user&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#3366FF"&gt;can cause system failure and potentially impact your network. Please be&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#3366FF"&gt;advised that changes made to the system as the root user must only be made&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#3366FF"&gt;under the guidance of xxxxxxxxxxx.&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#3366FF"&gt;***************************************************************************&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#3366FF"&gt;root@server2:~#&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#008000"&gt;su Authentication successful&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#99CC00"&gt;Disconnecting ssh session to &lt;A href="https://server2.acme.com" target="_blank"&gt;https://server2.acme.com&lt;/A&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;As always Luc, thank you !&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 20 May 2021 09:42:15 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Try-Catch-not-working-as-expected-for-Invoke/m-p/2848084#M101558</guid>
      <dc:creator>piercj2</dc:creator>
      <dc:date>2021-05-20T09:42:15Z</dc:date>
    </item>
  </channel>
</rss>

