VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Unable to Validate SSH Login for Linux VMs

Hi,

I am unable to validate the SSH logins for Linux VMs. Even though the password is correct, I am getting the output as false for Login but for uptime, it is getting the correct information.

Please help

Import-Csv -Path "D:\MyVM.csv" -UseCulture -PipelineVariable row |
ForEach-Object -Process{
try
{
$session = New-SSHSession $row.Name -Credential $Cred -AcceptKey -ErrorAction Stop
$result = $session | Select-Object -ExpandProperty Connected
$output = $((Invoke-SSHCommand -SSHSession $session -Command 'uptime -s').output)
Get-SSHSession | Remove-SSHSession | Out-Null
} catch {
$result = 'False'
}
$row | Add-Member -MemberType NoteProperty -Name 'Login' -Value $result -PassThru | Add-Member -MemberType NoteProperty -Name 'Uptime' -Value $output -PassThru
} | ft -auto

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Correct, you could do something like this

Import-Csv -Path "D:\MyVM.csv" -UseCulture -PipelineVariable row |
    ForEach-Object -Process {
        try {
            $session = New-SSHSession $row.Name -Credential $Cred -AcceptKey -ErrorAction Stop
            $connected = $session.Connected

            $result = Invoke-SSHCommand -SSHSession $session -Command 'shell; uptime -s'
            $uptime = $result.output -join "`n"
            Remove-SSHSession $session -Verbose | Out-Null
        }
        catch {
            $connected = $false
            $uptime = ''
        }
        $row | Add-Member -MemberType NoteProperty -Name 'Login' -Value $connected -PassThru |
        Add-Member -MemberType NoteProperty -Name 'Uptime' -Value $uptime -PassThru
    } | Format-Table -auto


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
11 Replies
LucD
Leadership
Leadership
Jump to solution

Can you show what is returned?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

The password is correct hence it is able to get the uptime but the Password validation shows False even though it is correct.

Output

Login Uptime

------      ------------------- -------

False     2022-05-17 09:06:17

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

It is not clear to me if your script stays in the Try part or runs the Catch part.
What does the following version return?

Import-Csv -Path "D:\MyVM.csv" -UseCulture -PipelineVariable row |
    ForEach-Object -Process {
        $output = ''
        try {
            $session = New-SSHSession $row.Name -Credential $Cred -AcceptKey -ErrorAction Stop -Verbose
            $result = $session | Select-Object -ExpandProperty Connected
            $output = $((Invoke-SSHCommand -SSHSession $session -Command 'uptime -s' -Verbose).output)
            Get-SSHSession | Remove-SSHSession | Out-Null
        } catch {
            Write-Host "In catch"
            $result = 'False'
        }
        $row | Add-Member -MemberType NoteProperty -Name 'Login' -Value $result -PassThru | Add-Member -MemberType NoteProperty -Name 'Uptime' -Value $output -PassThru
    } | Format-Table -auto


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Here is the requested output

ganapa2000_0-1652955190827.png

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Still not sure what exactly fails here.
Try with this code, and show what it produces

Import-Csv -Path "D:\MyVM.csv" -UseCulture -PipelineVariable row |
    ForEach-Object -Process {
        try {
            $session = New-SSHSession $row.Name -Credential $Cred -AcceptKey -ErrorAction Stop -Verbose
            $session | Format-List
            $result = Invoke-SSHCommand -SSHSession $session -Command 'uptime -s' -Verbose
            $result | Format-List
            Remove-SSHSession $session -Verbose
            Write-Host "End of Try"
        } 
        catch {
            Write-Host "In catch"
        }
        $row | Add-Member -MemberType NoteProperty -Name 'Login' -Value $session.Connected -PassThru | 
        Add-Member -MemberType NoteProperty -Name 'Uptime' -Value $result.output -PassThru
    } | Format-Table -auto

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

still not capture the output $session.connected

ganapa2000_0-1652957907132.png

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, the Connected property changes to $false after the Remove-SSHSession is executed.
The Output property is an array of strings, hence the join.

Try with this version

Import-Csv -Path "D:\MyVM.csv" -UseCulture -PipelineVariable row |
    ForEach-Object -Process {
        try {
            $session = New-SSHSession $row.Name -Credential $Cred -AcceptKey -ErrorAction Stop
            $connected = $session.Connected

            $result = Invoke-SSHCommand -SSHSession $session -Command 'shell; uptime -s'
            Remove-SSHSession $session -Verbose | Out-Null
        }
        catch {
            $connected = $false
        }
        $row | Add-Member -MemberType NoteProperty -Name 'Login' -Value $connected -PassThru |
        Add-Member -MemberType NoteProperty -Name 'Uptime' -Value ($result.output -join "`n") -PassThru
    } | Format-Table -auto

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

That worked LucD.

But for failed logins, the uptime is taking from previous sessions which should be ideally blank right ?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Correct, you could do something like this

Import-Csv -Path "D:\MyVM.csv" -UseCulture -PipelineVariable row |
    ForEach-Object -Process {
        try {
            $session = New-SSHSession $row.Name -Credential $Cred -AcceptKey -ErrorAction Stop
            $connected = $session.Connected

            $result = Invoke-SSHCommand -SSHSession $session -Command 'shell; uptime -s'
            $uptime = $result.output -join "`n"
            Remove-SSHSession $session -Verbose | Out-Null
        }
        catch {
            $connected = $false
            $uptime = ''
        }
        $row | Add-Member -MemberType NoteProperty -Name 'Login' -Value $connected -PassThru |
        Add-Member -MemberType NoteProperty -Name 'Uptime' -Value $uptime -PassThru
    } | Format-Table -auto


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
anilspp
Enthusiast
Enthusiast
Jump to solution

This one is helpful.

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Perfect LucD. That worked perfectly 🙂

Thank you very much.

Reply
0 Kudos