首页 > 解决方案 > Connect to ssh server with powershell

问题描述

Windows offers a ssh client. In Powershell I can connect with ssh user @ server.

How can I use the client in a script while passing credentials?

There are examples on the net about New-PSsession:

$ session = New-PSSession -HostName WinVM2 -UserName PSRemoteUser

Powershell Version 5.1 / New Pssession does not know the parameters hostname and username.

The following have no Error but the script hangs:

  $ session = New-PSSession -SSHTransport -ComputerName 192.168.2.50 -Credential root

If you use ssh user @ server directly in the script you will not be asked to enter a password.

Other modules should not be installed.

Does one have an idea?

标签: powershellssh

解决方案


New-PSSession 的 -Credential 参数不是字符串,而是您需要创建的 PSCredential 对象。MS给出了一个很好的例子来说明如何做到这一点。

https://blogs.msdn.microsoft.com/koteshb/2010/02/12/powershell-how-to-create-a-pscredential-object/


推荐阅读