0

Function parameters while debugging PowerShell in Visual Studio Code

PowerShell

Visual Studio Code is becoming one of the tools that you need to use when developing PowerShell scripts. It allows you to debug your scripts. But how do you add parameters for debugging. The upcoming paragraphs will explain how.

PowerShell

Parameters are common when developing scripts. Most probably they are configured with the Param object. Just like this example:

Param(
    [String]$ParameterOne,
    [String]$ParameterTwo
)

Write-Output "ParameterOne      :$ParameterOne"
Write-Output "ParameterTwo      :$ParameterTwo"

Running this script called “msftplayground.ps1” within a PowerShell terminal .\msftplayground.ps1will result with no parameters set because they need to be specified.

If you specify the parameters like this .\msftplayground.ps1 -ParameterOne "Test value 1" -ParameterTwo "Test Value 2" those values can then be used during the executing of file.

PowerShell Parameters

Visual Studio Code

With the “Debug” and “Integrated PowerShell Terminal” Visual Studio Code became a real powerfull tool. Debugging sessions can be started by simply pressing “F5”.

When you press “F5” within the PowerShell file Visual Studio Code executes the file. So if we for example start the script that we used in this article the following output will be shown

PowerShell Debugging

This is expected behavior because the debugger does not know the parameters that can be used.

So how do you specify the parameters?

Parameters need to be specified within the debugger configuration. Opening the configuration can be done via the debugger bar. Pressing the settings icon will open the configuration.

PowerShell Debugging Bar

The configuration is done in a JSON file called “launch.json”. In this file there are configurations for different situations. Choose the correct configuration for your situation and add your parameters in the “args” property.

A single configuration could look like this:

{
   "type": "PowerShell",
   "request": "launch",
   "name": "PowerShell Launch Current File",
   "script": "${file}",
   "args": ["-ParameterOne 'Test value 1'  -ParameterTwo 'Test Value 2'"],
   "cwd": "${file}"
}

Save the configuration and press “F5” to start the debugger. The PowerShell terminal will know show the expected result.

Related Posts

Azure Managed Service Identity and Local Development Instead of storing user credentials of an external system in a configuration file, you should store them in the Azure Key Vault. Before MSI (Managed S...
Pass-Through Authentication with Azure Active Directory, Azure SQL, Azure API an... In situations you need to login to an application and use that identity to access an API (pass-through identity) and also get data from Azure SQL Serv...
Azure and Development Resources While browsing the internet and reading blog posts I usually find very useful links or great resources that I could use in many situation. The problem...
Adding Azure App Service Application Settings with PowerShell Within Azure there is a option to change several configuration settings. When working with  Deployment Slots this means you have to click a lot within...
Creating the Release Template that runs after the Build – Part 3 With the component created in part 2 we can start using it to deploy applications to our SharePoint environment. We start by creating a new template. ...
Warm Up Form Based Web Application Normal warm up scripts for SharePoint open a site object or web object and make a request to that specific site. When you use form based authenticatio...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.