Automation

 View Only
  • 1.  issue validating ssh login

    Posted Dec 17, 2019 11:40 AM

    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



  • 2.  RE: issue validating ssh login
    Best Answer

    Posted Dec 17, 2019 01:22 PM

    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


  • 3.  RE: issue validating ssh login

    Posted Dec 18, 2019 04:39 AM

    Thank you very much LucD. :smileyhappy: