Unable to connect to server with SSH using predefined Credential-object
Hello,
I am trying to open an SSH connection without the need to enter my password. According to the docs I can do something like:
$Username = "xxx"
$Password = ConvertTo-SecureString -String "xxx" -AsPlainText -Force
$Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $Username, $Password
New-SSHSession -Verbose -ComputerName xxx.com -Credential $Credential -AcceptKey
However, this fails with message:
New-SSHSession : Permission denied (password).
At C:\Users\xxx.ps1:5 char:1
+ New-SSHSession -Verbose -ComputerName xxx ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (Renci.SshNet.SshClient:SshClient) [New-SSHSession], SshAuthenticationException
+ FullyQualifiedErrorId : SSH.NewSshSession
But when I type in the credentials, I can connect aka this works:
$Username = "xxx"
$Password = ConvertTo-SecureString -String "xxx" -AsPlainText -Force
$Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $Username, $Password
New-SSHSession -Verbose -ComputerName xxx.com -Credential (Get-Credential) -AcceptKey
To be honest no clue. The error says there is a problem with the password, maybe encoding, special char or a space.
PS C:\Users\carlos\Documents\WindowsPowerShell\Modules\Posh-SSH> $password = ConvertTo-SecureString '*****' -AsPlainText -Force
PS C:\Users\carlos\Documents\WindowsPowerShell\Modules\Posh-SSH> $credential = New-Object System.Management.Automation.PSCredential ('carlos', $password)
PS C:\Users\carlos\Documents\WindowsPowerShell\Modules\Posh-SSH> New-SSHSession -ComputerName 192.168.1.169 -Credential $credential
SessionId Host
--------- ----
2 192.168.1.169
I know its 2 years later but wanted to say to @cambiph you should get in the habit of quoting strings using single quotes 'not double quotes". Both are valid but they act very different. Using single quotes will tell PowerShell to treat it as a string and nothing else. If you use a double quotes PowerShell reads every character in the string, and looks for characters that can be substituted for a variable defined outside the string, or are actually evaluated as PowerShell operations. This is especially an issue with Passwords since they almost always contain special characters.
I know its 2 years later but wanted to say to @cambiph you should get in the habit of quoting strings using single quotes 'not double quotes". Both are valid but they act very different. Using single quotes will tell PowerShell to treat it as a string and nothing else. If you use a double quotes PowerShell reads every character in the string, and looks for characters that can be substituted for a variable defined outside the string, or are actually evaluated as PowerShell operations. This is especially an issue with Passwords since they almost always contain special characters.
You know I am looking for the answer for four hours straight and was already about to give up and then I stumble upon your post and it fixed the problem. Thank you for this amazing insight!