Skip to content

fix *-Content calls in psedit scripts and set ComputerName default #599

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

TylerLeonhardt
Copy link
Member

@TylerLeonhardt TylerLeonhardt commented Jan 5, 2018

*-Content -Encoding Byte was changed to *-Content -AsByteStream in PSCore. This PR adds a check for the version of PowerShell... and uses the correct param.

Also, since $env:ComputerName is not set on Linux or Mac, we get null ref exceptions so we are using [Environment]::MachineName instead and defaulting to "localhost" if it does not exist.

This allowed me to remote edit/debug a file on an Ubuntu VM from my Mac 🎉

Resolves #597
Resolves PowerShell/vscode-powershell#789

$params = @{ Path=$filePathName; Raw=$true }
if ($PSVersionTable.PSEdition -eq 'Core')
{
$params += @{ AsByteStream=$true }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be a bit more performant (and PS canonical IMO) to do this: $params['AsByteStream']=$true. This way we modify the original hashtable without creating a new to merge with the original one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated 🙂

Copy link
Contributor

@rkeithhill rkeithhill left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM other than that minor tweak to how the splatted, conditional parameters are specified. And for that matter, what you have works. Typical PowerShell - a half dozen ways to do the same thing. :-)

Copy link
Contributor

@rkeithhill rkeithhill left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@SeeminglyScience SeeminglyScience left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@daviwil daviwil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing this!

@@ -56,7 +56,7 @@ public static PSCommand GetDetailsCommand()
{
PSCommand infoCommand = new PSCommand();
infoCommand.AddScript(
"@{ 'computerName' = $env:ComputerName; 'processId' = $PID; 'instanceId' = $host.InstanceId }");
"@{ 'computerName' = if ($ExecutionContext.Host.Runspace.ConnectionInfo) {$ExecutionContext.Host.Runspace.ConnectionInfo.ComputerName} else {'localhost'}; 'processId' = $PID; 'instanceId' = $host.InstanceId }");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When connected to a remote session on Linux or macOS, does ComputerName contain anything? Aside from that I think this change is fine

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, when I tested on linux ConnectionInfo was null.

I could include a check for ComputerName being null to be safe.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a remote session to a Linux machine ConnectionInfo was null? Pretty unfortunate if so, though I suppose $env:ComputerName never worked on Linux anyway.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure the only time ConnectionInfo is set is when it's WSManConnectionInfo. Frustratingly, even NamedPipeConnectionInfo never gets set 😕

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about [Environment]::MachineName? Looks like it works as expected on my Ubuntu box at least. Maybe something like this just in case it's empty on some distros

if ([string]::IsNullOrWhitespace([Environment]::MachineName)) {
    'localhost'
} else {
    [Environment]::MachineName
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, [Environment]::MachineName seems good to me too, but I'd check to make sure it also works on PowerShell 3-5.1 just in case. I dunno if we can assume 'localhost' as default since we also run this on remote sessions. I think if it turns up empty we're in trouble no matter what :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Environment]::MachineName works on macOS and 5.1 too!

@daviwil I thought PSRP in vscode-powershell wasn't allowed in PS 3 and 4?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remoting works in PS4, but we should make sure that it works on the local machine for PS3/4 too. Though if it works in PS 5.1, I would assume it works in PS 3/4 too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PS 3/4 have [Environment]::MachineName 🎉

Let's go with that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I changed it to [Environment]::MachineName. I kept the if just in case. Let me get some 👍's from you guys and I'll merge this it! 😃

@TylerLeonhardt
Copy link
Member Author

Moving this comment out of the outdated comment...

Ok @daviwil, @rkeithhill, @SeeminglyScience! I changed $env:ComputerName to [Environment]::MachineName. I kept the if just in case. Let me get some 👍's from you guys and I'll merge this it! 😃

@daviwil
Copy link
Contributor

daviwil commented Jan 8, 2018

Do it

@TylerLeonhardt TylerLeonhardt merged commit 33cb421 into PowerShell:master Jan 8, 2018
@TylerLeonhardt TylerLeonhardt deleted the fix-content-calls-and-computer-name branch January 8, 2018 20:23
TylerLeonhardt pushed a commit to TylerLeonhardt/PowerShellEditorServices that referenced this pull request Feb 26, 2019
This change fixes an issue where SQL's SSASCMDLETS module cannot be loaded
into the interactive console because the host currently has a pre-1.0
version number.  For now we will fix this by reporting our version as 1.0
since we're pretty close to 1.0 anyway.
TylerLeonhardt pushed a commit to TylerLeonhardt/PowerShellEditorServices that referenced this pull request Feb 26, 2019
Fix PowerShell#599: SSASCMDLETS module cannot be loaded
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants