1

I have this script

$db = import-csv -Path ".\testdb.csv"
 
$inputID = Read-Host -Prompt "ID"
 
$entry = $db -match $inputID

Write-Host "IP:" $entry.IP

    $User = "user"
    $Password = "pass"
    $User2 = "user2"
    $Password2 = "pass2"
    $Command = "C:\test.exe"

    $secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
    $Credentials = New-Object System.Management.Automation.PSCredential($User, $secpasswd)
    $secpasswd2 = ConvertTo-SecureString $Password2 -AsPlainText -Force
    $Credentials2 = New-Object System.Management.Automation.PSCredential($User2, $secpasswd2)

Get-SSHTrustedHost | Remove-SSHTrustedHost

try {
    $SessionID = New-SSHSession -ComputerName $entry.IP -Credential $Credentials -AcceptKey:$true
}
catch {
    $SessionID = New-SSHSession -ComputerName $entry.IP -Credential $Credentials2 -AcceptKey:$true
}

Is any chance to try connect with 2 different credentials ; if one fail try to other? So.. first time try user and password ; second time try user2 and password2

Thank you. :)

  • Does variable `$entry` indeed have a property called `IP` ? (your code shows nothing about that, so as far as we are concerned it is undefined..) Do you get error messages? – Theo Sep 28 '21 at 09:10
  • I edit post. > complete script. –  Sep 28 '21 at 09:40
  • `$entry = $db -match $inputID` will result in either `$true` or `$false`.. Without knowing what the csv file looks like, but that should be `$entry = $db | where-Object {$_.IP -eq $inputID}` (provided the CSV has a column called `IP`) – Theo Sep 28 '21 at 09:55
  • my db.csv looks like Hostname,IP ; where -prompt "ID" = hostname ; I enter hostname and script find IP. –  Sep 29 '21 at 07:37
  • 1
    In that case, find the correct entry using `$entry = $db | where-Object {$_.HostName -eq $inputID}` – Theo Sep 29 '21 at 13:58

0 Answers0