7

Problem Description

When I select Run Without Debugging in Visual Studio Code 1.58.2 on the module SampleModule.psm1

function ConvertTo-PascalCase([String []] $words) {
    [String] $pascalCaseString = [String]::Empty

    foreach ($word in $words) {
        $pascalCaseString = $pascalCaseString + ($word.Substring(0,1).ToUpper() + $word.Substring(1))
    }
    $pascalCaseString = ($pascalCaseString.TrimEnd(',')).Trim()
    
    return $pascalCaseString
}

Export-ModuleMember -Function Convert-ToPascalCase

I keep getting the error message:

InvalidOperation: Cannot run a document in the middle of a pipeline

The screenshot shows it in more detail. I have got this same error message whenever I build any module

vscode error message for running *.psm1 file

Things I Tried That Did Not Work:

  1. Checking my PowerShell extensions in VSCode and their settings
  2. Using different PowerShell versions in VSCode
  3. Including a manifest makes no difference
  4. Researching the error message for more details about it's meaning in this specific case
  5. Checked the configuration of my VSCode Terminal environment

How I Know It's an Issue With Running PSM1 Files

If I convert the .psm1 module file back into a plain old Powershell script (*.ps1) script and remove all the Export-Module member commands the script runs just fine.

My Work Around

To execute the module in VSCode I have to dotsource to successfully import the module:

PS C:\Users\griot\SampleModule> .\SampleModule.psm1

PS C:\Users\griot\SampleModule> Import-Module .\SampleModule.psm1

PS C:\Users\griot\SampleModule>

My Questions

  1. Any ideas why I am getting the error message?

  2. What is the recommended way to run and debug PowerShell modules in VSCode?

Tuor
  • 875
  • 1
  • 8
  • 32
Banji
  • 81
  • 1
  • 5
  • Modules are not designed to be run like .ps1 script files, they must be **imported** first. Trying to run the module will generate an error about _"cannot run a document in the middle of a pipeline"_. (I agree this is a confusing error message..) Therefore, the script that needs to make use of the functionality of the module needs to first use [Import-Module](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/import-module) – Theo Aug 03 '21 at 18:21
  • 1
    I do use Import-Modue when I want to invoke the script in another function. And that works fine. It's more that when I want to test the module in VS Code I am having to go through a process of tfirst saving the file as a ps1 script. debugging and testing then saving as a psm1 file. Is there an easier way? that you know of doing this? – Banji Aug 03 '21 at 19:55
  • @The thanks for your answer it gives me an idea of why I am getting the error message. Do you know of a better method of testing powershell modules in VS Code? – Banji Aug 03 '21 at 20:01
  • AFAIK that is the way to do it. You can ofcourse open the saved module in a second tab and work on it there while you have the calling script in the first tab. In that case however, you must use `Import-Module .. -Force`, otherwise the once imported module is kept in memory and your changes won't be used/loaded. – Theo Aug 03 '21 at 20:09
  • 1
    I'll just stick to that method then. If I find a better way of testing modules in VS Code I will probably write an article about it here – Banji Aug 03 '21 at 20:31
  • I mean, you could always rename the `*.psm1` to `*.ps1` while you're working on it, and replace the testing call with `Export-ModuleMember` only when you're ready to go. I think that's the way I'm headed if there's not something easier. – ruffin Nov 01 '21 at 21:40

0 Answers0