Update remote prompt when using ssh to show username if different#7191
Update remote prompt when using ssh to show username if different#7191TravisEz13 merged 6 commits intoPowerShell:masterfrom
Conversation
| return string.Format(CultureInfo.InvariantCulture, "[{0}]: {1}", runspace.ConnectionInfo.ComputerName, basePrompt); | ||
| SSHConnectionInfo sshConnectionInfo = runspace.ConnectionInfo as SSHConnectionInfo; | ||
| if (!string.IsNullOrEmpty(sshConnectionInfo.UserName) && | ||
| !System.Environment.UserName.Equals(sshConnectionInfo.UserName, StringComparison.OrdinalIgnoreCase)) |
There was a problem hiding this comment.
Seems we could remove !string.IsNullOrEmpty(sshConnectionInfo.UserName).
There was a problem hiding this comment.
In case where username is not explicitly used, at this point in the code UserName is empty so without this check it shows up as [@computer] since empty != current username.
| return string.Format(CultureInfo.InvariantCulture, "[{0}@{1}]: {2}", sshConnectionInfo.UserName, sshConnectionInfo.ComputerName, basePrompt); | ||
| } | ||
| } | ||
| return string.Format(CultureInfo.InvariantCulture, "[{0}]: {1}", runspace.ConnectionInfo.ComputerName, basePrompt); |
There was a problem hiding this comment.
Please add new line after line 783 (from CodeFactor).
| return string.Format(CultureInfo.InvariantCulture, "[{0}]: {1}", runspace.ConnectionInfo.ComputerName, basePrompt); | ||
| SSHConnectionInfo sshConnectionInfo = runspace.ConnectionInfo as SSHConnectionInfo; | ||
| if (!string.IsNullOrEmpty(sshConnectionInfo.UserName) && | ||
| !System.Environment.UserName.Equals(sshConnectionInfo.UserName, StringComparison.OrdinalIgnoreCase)) |
There was a problem hiding this comment.
For Windows sshConnectionInfo.UserName can have a domain component (domainName\userName), so we need to handle that unless we make this Linux platform only. We can create a common helper method from here: https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs#L2052. Or maybe update SSHConnectionInfo to automatically parse and expose UserName and DomainName properties.
There was a problem hiding this comment.
I am assuming we would want the prompt to look something like: [user@domain@computer]: PS C:\>
There was a problem hiding this comment.
Or maybe just drop the domain...
There was a problem hiding this comment.
I think you display it as [<username>@<hostname>] where <username> may or may not specify the additional @domain. That's what I see on Linux with sh (or Bash or whatever ssh is using as a shell).
There was a problem hiding this comment.
Currently, it depends on how the user specified the domain name. If they used user@domain, it would be [user@domain@computer]. Alternatively, if they used domain\user, it shows up as [domain\user@computer]. Personally, I think we should show the domain and fine with whichever form the user provided.
There was a problem hiding this comment.
We don't currently support user@domain format. It seems like domain information is not needed, but I am fine with [domain\user@computer]. We can easily change it later if it is unpopular.
| return basePrompt; | ||
| } | ||
| else | ||
| else if (runspace.ConnectionInfo is SSHConnectionInfo) |
There was a problem hiding this comment.
There is no need to do the runtime cast twice. I suggest:
else
{
var sshConnectionInfo = runspace.ConnectionInfo as SSHConnectionInfo;
if (sshConnectionInfo != null)
{
...
}
else
{
return string.Format(CultureInfo.InvariantCulture, "[{0}]: {1}", runspace.ConnectionInfo.ComputerName, basePrompt);
}
}
PaulHigin
left a comment
There was a problem hiding this comment.
Nitpick: I just realized looking at the whole method that we don't need the else block because we are doing early returns. We can just remove it.
|
@PaulHigin agree it's not needed. Can you take a quick look and re-approve? |
| SSHConnectionInfo sshConnectionInfo = runspace.ConnectionInfo as SSHConnectionInfo; | ||
| if (sshConnectionInfo != null && | ||
| !string.IsNullOrEmpty(sshConnectionInfo.UserName) && | ||
| !System.Environment.UserName.Equals(sshConnectionInfo.UserName, StringComparison.OrdinalIgnoreCase)) |
There was a problem hiding this comment.
You're right, Unix usernames are case-sensitive. I'll change this to a case sensitive comparison.
| SSHConnectionInfo sshConnectionInfo = runspace.ConnectionInfo as SSHConnectionInfo; | ||
| if (sshConnectionInfo != null && | ||
| !string.IsNullOrEmpty(sshConnectionInfo.UserName) && | ||
| !System.Environment.UserName.Equals(sshConnectionInfo.UserName, StringComparison.Ordinal)) // Usernames are case-sensitive on Unix |
There was a problem hiding this comment.
Please move the comment on separate line.
|
@anmenaga Please update your review |
|
If our docs have related samples we should correct them. |
PR Summary
Currently, the remote prompt only shows the remote computer name:
With this change, if you use PSRP over SSH and you specified a different username, it shows whom you logged in as:
Fix is when the remote prompt is being created to check if using SSH, if so, check if the current user is different than the specified user and specified. If different, use
[user@computer], otherwise fallback to default[computer]prompt.Fix #7156
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:to the beginning of the title and remove the prefix when the PR is ready.[feature]if the change is significant or affects feature tests