ganapa2000
Hot Shot
Hot Shot

Unable to Validate SSH Login for Linux VMs

Jump to solution

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

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

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

0 Kudos
11 Replies
LucD
Leadership
Leadership

Can you show what is returned?


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot

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

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
ganapa2000
Hot Shot
Hot Shot

Here is the requested output

ganapa2000_0-1652955190827.png

 

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
ganapa2000
Hot Shot
Hot Shot

still not capture the output $session.connected

ganapa2000_0-1652957907132.png

 

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
ganapa2000
Hot Shot
Hot Shot

That worked LucD.

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

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
anilspp
Enthusiast
Enthusiast

This one is helpful.

0 Kudos
ganapa2000
Hot Shot
Hot Shot

Perfect LucD. That worked perfectly 🙂

Thank you very much.

0 Kudos