Issue
The following Powershell script is prompting the credentials to the user but I am sending all the necessary information to execute the ssh session connection.
What can I change to remove the user prompt request?
$sshParams = @{
Credential="user_name"
HostName = $sshHostName
KeyFile = $sshIdentityFile # .pem file
}
$sshSession = New-SSHSession @sshParams
if ($null -ne $sshSession) {
Write-Output "Connected to $sshHost"
}
else {
$errorMessage = "SSH connection failed!"
Write-Output $errorMessage
throw $errorMessage
}
Write-Output "Closing SSH session..."
Remove-SSHSession -SessionId $sshSession.SessionId
User Prompt Request:
After click Ok with no password the connection works fine => Promt is unnecessary!
I need to execute the script with no Windows Prowershell credential request.
Solution
This is the behavior you can expect from a parameter with type pscredential
or a parameter decorated with the Credential
Attribute, if you supply a string to it it will automatically prompt you for a password:
function Test-Param {
param([pscredential] $Credential)
}
Test-Param -Credential willPromptForPassword
If you want the cmdlet to not prompt for a password then you need to construct the pscredential
beforehand and pass it as argument. For example:
function Test-Param {
param([pscredential] $Credential)
}
$credential = [pscredential]::new(
'doesNotPromptForPassword',
(ConvertTo-SecureString 'thePassword' -AsPlainText -Force))
Test-Param -Credential $credential
As for the code you currently have, I have not personally used New-SSHSession
but reading the documentation states the following:
If a key file is used the password field is used for the Key pass phrase.
Unfortunately the documentation for this cmdlet only has 1 example and is not a particularly clear.
EDIT: OP has confirmed that the issue was solved by using an empty password:
$credential = [pscredential]::new(
'user_name',
[securestring]::new())
$sshParams = @{
Credential = $credential
HostName = $sshHostName
KeyFile = $sshIdentityFile # .pem file
}
$sshSession = New-SSHSession @sshParams
Answered By - Santiago Squarzon Answer Checked By - David Goodson (WPSolving Volunteer)