VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

issue validating ssh login

Hi,

I am not able to get the validate of ssh login from below script.

If the password is wrong, I get False and it gets captured in the output.

If the password is correct, Password_Validation field and Version field shows blank and not getting captured in the output

Please help

Script

connect-viserver 10.10.10.10

get-vm | where{$_.ExtensionData.Config.GuestFullname -match "Cent"} | select @{n='Server';e={$_.Name}} | sort Server | export-csv .\hostpassvalidation.csv -NoTypeInformation

disconnect-viserver -server * -force -confirm:$false

$Username = 'root'

$pass = ConvertTo-SecureString -AsPlainText 'password' -Force

$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass

Import-Csv -Path .\hostpassvalidation1.csv -UseCulture -PipelineVariable row |

ForEach-Object -Process {

   try

   {

   $session = New-SSHSession $row.Server -Credential $Cred -AcceptKey -ErrorAction Stop | Out-Null

   $result = $session | Select-Object -ExpandProperty Connected

   $output = $((Invoke-SSHCommand -SSHSession $session -Command 'adinfo -v').output)

  Get-SSHSession | Remove-SSHSession | Out-Null

   }

   catch

   {

   $result = 'False'

   }

   $row | Add-Member -MemberType NoteProperty -Name 'Password_Validation' -Value $result -PassThru | Add-Member -MemberType NoteProperty -Name 'Version' -Value $output -PassThru

} | ft -auto

Output

Server      Password_Validation Version

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

172.27.4.71

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The $session variable will be empty since you redirect the result of the New-SSHSession cmdlet to Out-Null

Replace that line with

$session = New-SSHSession $row.Server -Credential $Cred -AcceptKey -ErrorAction Stop


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

View solution in original post

Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

The $session variable will be empty since you redirect the result of the New-SSHSession cmdlet to Out-Null

Replace that line with

$session = New-SSHSession $row.Server -Credential $Cred -AcceptKey -ErrorAction Stop


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Thank you very much LucD. Smiley Happy

Reply
0 Kudos