PowerShell / vscode-powershell

Provides PowerShell language and debugging support for Visual Studio Code
https://marketplace.visualstudio.com/items/ms-vscode.PowerShell
MIT License
1.72k stars 492 forks source link

Provide support for custom console file or pssnapin preloading #135

Open stevebeauge opened 8 years ago

stevebeauge commented 8 years ago

I'm trying to debug a bunch of SharePoint scripts.

These scripts starts with #requires -PSSnapin Microsoft.SharePoint.PowerShell in order to tell Powershell to require a specific snapin to be loaded.

With Powershell, we can load a snapin either by calling Add-PSSnapin Microsoft.SharePoint.PowerShell prior to run the script, or by running a custom PowerShell console (a .psc1 file). The custom console can target when launching PowerShell : powershell.exe -PSConsoleFile c:\path\to\console.psc1.

using VSCode, I wasn't able to inject the snapin.

I tried to tweak the launch.json file like this:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "PowerShell",
            "type": "PowerShell",
            "request": "launch",
            "program": "${file}",
            "args": ["-PSConsoleFile \"C:\\Program Files\\Common Files\\Microsoft Shared\\Web Server Extensions\\15\\CONFIG\\POWERSHELL\\Registration\\psconsole.psc1\"","\"An argument to pass to my script\""],
            "cwd": "${file}"
        }
    ]
}

or

"program": "Add-PSSnapin Microsoft.SharePoint.PowerShell;${file}"

The error is always:

The script 'test.ps1' cannot be run because the following Windows PowerShell snap-ins that are specified by its "#requires" statements are missing: Microsoft.SharePoint.Powershell.

Did I missed something ? Maybe supplemental parameters to the configuration would be simplier. Either

Thanks

daviwil commented 8 years ago

Hmmm, interesting. I think the snapInsToLoad idea is a good one to solve this problem. I'm not sure if the logic to load .psc1 files is available to anything other than powershell.exe so we might not be able to use that. I'll do a little research.

For now your best bet may be to have a launcher script which calls Add-PSSnapIn and then dot-sources the script you're trying to test. You'd then run the launcher script so that everything gets loaded up correctly. Let me know if this approach works for you and then I'll see if we can get a better solution working for the next release.

stevebeauge commented 8 years ago

You were right. I can add a launch script run.ps1 that contains:

Add-PSSnapin Microsoft.sharePoint.PowerShell

. .\MyActualScript.ps1

thanks for the suggestion