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
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
Can you show what is returned?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
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
Here is the requested output
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
still not capture the output $session.connected
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
That worked LucD.
But for failed logins, the uptime is taking from previous sessions which should be ideally blank right ?
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
This one is helpful.
Perfect LucD. That worked perfectly 🙂
Thank you very much.