Powershell SSH凭据

3bygqnnd  于 2023-11-18  发布在  Shell
关注(0)|答案(1)|浏览(119)

我有这个脚本自动执行命令在多台机器上,但出现一个问题,当我试图迫使用户输入凭据:

Write-Host "Enter credentials!" -ForegroundColor Green

$user1 = Read-Host "user"
$pwd1 = Read-Host "pass" -AsSecureString

Write-Host "type command" -ForegroundColor Cyan  
Write-Host "--------------" -ForegroundColor Cyan
Write-Host "example: shutdown -f -r -t 0" -ForegroundColor Cyan
$Command1 = Read-Host -Prompt "Comanda"

$Computers = Get-Content -Path C:\Users\Meo\Desktop\ipMachine.txt | Where-Object { $_ -match '\S' }

foreach($Computer in $Computers){
Write-Host $Computer

    $User = "$user1"
    $pwd1_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd1))
    $secpasswd = ConvertTo-SecureString $pwd1 -AsPlainText -Force
    $Credentials = New-Object System.Management.Automation.PSCredential ($User, $secpasswd)

    $Command1 = "$Command"
    Write-Host $Command

Get-SSHTrustedHost | Remove-SSHTrustedHost

$SessionID = New-SSHSession -ComputerName $Computer -Credential $Credentials -AcceptKey:$true

Invoke-SSHCommand -Index $sessionid.sessionid -Command $Command
}

字符串
return(如果我把凭证放在脚本中,它工作得很好..任何想法?thx)

New-SSHSession : Permission denied (keyboard-interactive).
At line:26 char:14
+ ... SessionID = New-SSHSession -ComputerName $Computer -Credential $Crede ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (Renci.SshNet.SshClient:SshClient) [New-SSHSession], SshAuthenticationException
    + FullyQualifiedErrorId : SSH.NewSshSession
 
Invoke-SSHCommand : Cannot bind argument to parameter 'SessionId' because it is null.
At line:28 char:26
+ Invoke-SSHCommand -Index $sessionid.sessionid -Command $Command
+                          ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Invoke-SSHCommand], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Invoke-SSHCommand
    ```

lkaoscv7

lkaoscv71#

我今天也遇到了这个问题,我知道这是关于主机密钥验证的问题,但我想不通,所以我的临时解决方案是使用-Force
New-SSHSession -ComputerName $Computer -Credential $Credentials -AcceptKey -ConnectionKey 30 -Force

相关问题