Banji
Banji

Reputation: 91

Getting "InvalidOperation: Cannot run a document in the middle of a pipeline:" error when building powershell module in Visual Studio Code

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?

Upvotes: 7

Views: 9818

Answers (1)

Michael Brogan
Michael Brogan

Reputation: 51

Looks to me like a typo ConvertTo-PascalCase was the name of the function. Convert-ToPascalCase was the $null function that got exported

Upvotes: 2

Related Questions