Skip to content
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

Run Script in External PowerShell Console, accepting parameter #705

Closed
mpearon opened this issue Apr 29, 2017 · 14 comments
Closed

Run Script in External PowerShell Console, accepting parameter #705

mpearon opened this issue Apr 29, 2017 · 14 comments
Milestone

Comments

@mpearon
Copy link

mpearon commented Apr 29, 2017

System Details

  • Operating system name and version: Windows 10 v1607 Build 14939.1066
  • VS Code version: 1.11.2
  • PowerShell extension version: 0.12.2
  • Output from $PSVersionTable:

PSVersion 5.1.14393.1066
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14393.1066
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

Issue Description

The ability to run a script in an external console from within vsCode would be awesome.
This feature was present in PowerGUI, but broke with the release of Win10 v1607 - and I don't have a machine pre-1607 around, so I can't demonstrate the feature in its entirety...but what I can show follows.

There were two ways to execute a script, and a way to pass a parameter to both methods:
1. Running in integrated terminal
This did just want vsCode does, but it offered an extra feature that I'll mention in No. 3.
powergui-1

 2.  Running in external console
      I can't demonstrate this one (afore mentioned reasons), but this is what I would love to see.  This saved a temporary copy of the script, then launched PowerShell externally with the path to the temporary script as the -File parameter.  This helped eliminate any "ISE-style" weirdness, and showed me exactly how the end-user's experience would be - which was awesome.

 3.  You could pass an argument list to both methods via an input box in editor.

powergui-3

@mpearon
Copy link
Author

mpearon commented Apr 29, 2017

I'm not sure why, but the descriptions on points 2 and 3 are showing as code. Sorry!

@rkeithhill
Copy link
Contributor

rkeithhill commented Apr 29, 2017

When you run in the integrated terminal you can pass parameters to this script. Click on the gear icon in the debug view to edit the launch.json file:

        {
            "type": "PowerShell",
            "request": "launch",
            "name": "PowerShell Launch (current file)",
            "program": "${file}",
            "args": [],
            "cwd": "${file}"
        },

Add your args to the args: field and they will be passed to the script when you debug. That said, I think we could add a launch config option that would prompt in the UI for arguments. That might be worth adding. Hmm...

For the external console debug support, you can attach to an external console. I would stick a temporary Read-Host "press enter to continue" at the start of your script. Before running the script, execute $pid and note the process id on the console. Then run the script from the console. Then in VSCode, edit the launch file, add the "PowerShell: Attach to PowerShell Host Process" configuration. Save the file and then select that config in the debug view and press F5. Then select your external PowerShell process (via the pid) and you'll be attached to that external console and you can debug it.

@mpearon
Copy link
Author

mpearon commented Apr 29, 2017

@rkeithhill
As far as the parameter solution goes, that isn't a very easy solution - and I haven't quite gotten it to work, but that is probably operator error. Here is what I've done to my config:

{
            "type": "PowerShell",
            "request": "launch",
            "name": "PowerShell Launch (current file)",
            "script": "${file}",
            "args": ["test"],
            "cwd": "${file}"
        },

...and this is the test script I'm try to run:

Param(
    $test = 'not defined'
)
Write-Host $test

The editor's status bar turns orange, but the script doesn't appear to execute.

Regarding the external launch:
I'm not really interested in the debugging functionality (although that is a neat feature). It's more of the ability to quickly launch the script (like an end-user would), and see how it behaves outside of the editor. It's probably too niche to really get anything done about it.

@rkeithhill
Copy link
Contributor

That config looks good. Two things. First, make sure you have that config selected in the Debug view drop down e.g.:

image

Second, make sure the script you want to run is in the active editor window.

@mpearon
Copy link
Author

mpearon commented Apr 29, 2017

@rkeithhill
I need to start by saying that I really appreciate your assistance with this. Communities like this are awesome, and are what make me proud to be a geek.

I bounced the editor, and now the debugger is actually running the script - so that bit is better now.
Here is what I'm still encountering:
Note: I've spun up a duplicate launch configuration to demonstrate that I'm using the correct one (and so I can easily switch to a functional one).
args1

So, if I understand how this is supposed to be working, the Write-Host should return test instead of the default value of undefined, right?

@rkeithhill
Copy link
Contributor

This seems to be a bug in the handling of an unsaved file w/launch.json. Trivial workaround though, save the file to a .ps1 file and it works as expected (at least on my machine).

@mpearon
Copy link
Author

mpearon commented Apr 29, 2017

@rkeithhill
That did the trick, sir! Thank you!

Is it possible to have the editor interactively prompt you for this value (like it does for the PID)?
I have tried "args": ["${args}"] and "args": "${args}" to no avail. The first does nothing, and the second causes the debug to churn endlessly.

@rkeithhill
Copy link
Contributor

rkeithhill commented Apr 30, 2017

OK I looked into the ignored arguments issue. The problem doesn't reside in VSCode. The args get passed to the PowerShellEditorServices:

    READ MESSAGE:
    
    {
      "command": "launch",
      "arguments": {
        "type": "PowerShell",
        "request": "launch",
        "name": "PowerShell Launch (current file)",
        "script": "untitled:Untitled-1",
        "args": [
          "test"
        ],
        "cwd": "c:\\Users\\Keith\\temp",
        "internalConsoleOptions": "neverOpen"
      },
      "type": "request",
      "seq": 2
    }

The problem in PSES is that we execute the unsaved editor contents by essentially asking PowerShell to execute the string param($test='undefined')\nWrite-Host. Same as if you pasted this into the console. In this scenario, there is no named command on which to pass the arguments. And in fact, the internal method that executes the script doesn't even take the arguments.

@daviwil We might be able to work around this by wrapping the string like so:

if (arguments != null)
{
    scriptString = string.Forma("& {{ {0} }} {1}", scriptString, arguments);
}
psCommand.AddScript(scriptString);

There are probably several ways to make this work. What do you think?

@rkeithhill
Copy link
Contributor

@mpearon RE prompting for arguments ... this is a feature from PowerGUI editor that I miss. I think we might be able to do this. I'm going to look into it. If I can make it work, you'll probably have to replace the "args:" value with something like `"${command:SpecifyScriptArguments}".

@rkeithhill
Copy link
Contributor

That was relatively easy to prototype:
image
Looks like:
image
You can enter say 50 100 or test or just press Enter to send no arguments.
I wonder if we should just add this as-is or maybe add a debug configuration snippet that uses this? Not sure what we would call it. Perhaps PowerShell Launch w/args (current file)? Or maybe PowerShell Launch w/args UI (current file)?

@rkeithhill
Copy link
Contributor

I want this UI to remember the args you typed in the previous debug session so you don't have to keep re-entering the same values (at least during the same VSCode session). But there appears to be a bug in VSCode (or I'm using it wrong). Waiting to get resolution on this before finalizing the PR for this feature.

@daviwil daviwil modified the milestones: 1.0.0, May 2017 May 10, 2017
@mpearon
Copy link
Author

mpearon commented May 10, 2017

@rkeithhill @daviwil
It is awesome to see this in the 1.0.0 release! It might be worth mentioning that the script must be saved for this to work.

@mpearon
Copy link
Author

mpearon commented May 12, 2017

If there are no objections from @rkeithhill and @daviwil , I'll close this issue.

@daviwil daviwil modified the milestones: 1.0.0, May 2017 May 12, 2017
@daviwil
Copy link
Contributor

daviwil commented May 12, 2017

Ahh! I didn't realize we hadn't closed it out yet :) Thanks Matthew!

@daviwil daviwil closed this as completed May 12, 2017
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

No branches or pull requests

3 participants