在PowerShell 6.2中,使用Enter-PSSession cmdlet,我们有一个类似于"John Green"的用户名。
这里产生的错误:
PS C:> Enter-PSSession -Hostname 192.168.1.1 -Username "John Green" -SSHTransport
Enter-PSSession : The background process reported an error with the following message: The SSH client session has ended with error message: ssh: Could not resolve hostname john: No such host is known. .
At line:1 char:1
+ Enter-PSSession -Hostname 192.168.1.1 -Username "John Green" -SSHTra ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [Enter-PSSession], PSRemotingTransportException
+ FullyQualifiedErrorId : System.Management.Automation.Remoting.PSRemotingDataStructureException,Microsoft.PowerShell.Commands.EnterPSSessionCommand
现在是一个带有"John.Green"(空格被点替换(的工作示例。
PS C:> Enter-PSSession -Hostname 192.168.1.1 -Username "John" -SSHTransport
The authenticity of host '192.168.1.1 (192.168.1.1)' can't be established.
ECDSA key fingerprint is <snip>.
Are you sure you want to continue connecting (yes/no)?
类似Linux的命令也会在PowerShell中给出错误:
PS C:> ssh "John Green"@192.168.1.1
At line:1 char:22
+ ssh "John Green"@192.168.1.1
+ ~
Missing property name after reference operator.
At line:1 char:17
+ ssh "John Green"@192.168.1.1
+ ~~~~
The splatting operator '@' cannot be used to reference variables in an expression. '@192' can be used only as an
argument to a command. To reference variables in an expression use '$192'.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingPropertyName
问题:
- 如何将带有空格的用户名与此类 cmdlet 一起使用?
- 如果不可能,在PowerShell中,如何使用包含空格的用户名启动SSH会话?
感谢 @LarrySong SSH 的答案:
ssh "John Green@192.168.1.1"
其他提议的格式不起作用。
以下命令在 PowerShell 中工作(无客户端错误(,但由于它将空格替换为"%20",因此用户名在服务器端无效。
Enter-PSSession -Hostname "John Green@192.168.1.1" -SSHTransport
John%20Green@192.168.1.1's password:
Enter-PSSession : The background process reported an error with the following message: The SSH client session has ended with error message: Permission denied, please try again..
这对我有用:
Enter-PSSession -HostName COMPUTERNAME -UserName 'John" "Green'