1

I have written a cmdlet called Select-Blah.ps1 that extract certain information from log files. The log files are sent to my cmdlet via the pipeline:

PS> Get-Content log.txt | Select-Blah

How can I run the command above in the vscode debugger? Specifically, what launch configuration can I put into my launch.json file in vscode?

1 Answer 1

1

I have come up with a workable solution. First, define your pipeline input parameter in your cmdlet to take an array rather than just a single object. In my case I am using an array of strings:

[CmdletBinding()]
param(
    [parameter(ValueFromPipeline=$true)]
    [string[]]$InputObject
)

(Side note: $InputObject is the customary name for pipelined input. Don't use $Input because that is an automatic variable.)

In the Process section of the script, add a foreach loop to operate on the array:

Process
{
    foreach ($object in $InputObject) {
        # Do stuff with $object
    }
}

To run the script in the vscode debugger, you can specify the arguments in your launch configuration (in launch.json) something like this:

"args": ["-InputObject (Get-Content /path/to/myFile.txt)"]

For reference, this is what my launch configuration looks like:

{
    "name": "PowerShell Launch Select-Blah.ps1",
    "type": "PowerShell",
    "request": "launch",
    "script": "${workspaceFolder}/Select-Blah.ps1",
    "cwd": "${cwd}",
    "args": ["-InputObject (Get-Content /path/to/myFile.txt)"]
}

You can select "PowerShell Launch Select-Blah.ps1" in the dropdown in the debugger, and hit f5 to launch it.

Edit, another possibility:

If you want to have a singular $InputObject:

[CmdletBinding()]
param(
    [parameter(ValueFromPipeline=$true)]
    [string]$InputObject
)

You can use the -Raw switch with Get-Content to get a single string:

"args": ["-InputObject (Get-Content -Raw /path/to/myFile.txt)"]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.