• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
ControlUp Community

ControlUp Community

Connect, Learn, and Grow

  • Blog
  • Podcast
  • Meetups
  • Archives
  • Categories
    • ControlUp One Platform
    • ControlUp for Apps
    • ControlUp for Compliance
    • ControlUp for Desktops
    • ControlUp Scripts & Triggers
    • ControlUp Synthetic Monitoring
    • ControlUp for VDI
  • Topics
  • Events
    • Logos & Wallpaper
    • ControlUp.com
  • Join

How to Use Parameters in Powershell Scripts for ControlUp

Posted on June 17, 2024

A discussion about different options for using parameters in Powershell scripts was brought up, including using $args without a [param] block, using a [param] block with named parameters, or not specifying parameters at all. The advice given was to only use named parameters with a [param] block for proper integration with ControlUp. Examples of these options were provided.


Read the entire ‘How to Use Parameters in Powershell Scripts for ControlUp’ thread below:

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"

$headers.Add("Content-Type", "application/json")

$body = @"

{

"routing_key": "xxxxxxxxxxxxxxxxxxxxxxx",

"event_action":"trigger",

"payload": {

`"summary`": `This was generated from an event log of 99`",

`"severity`": `"critical`",

`"source`": `"$MachineName`"

}

}

"@

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$response = Invoke-RestMethod ‘https://events.pagerduty.com/v2/enqueue‘ -Method ‘POST’ -Headers $headers -Body $body

$response | ConvertTo-Json


I think you played with similar setup @member?


If you’re using named variables, you have to have a [params] block at the start of your script.

$args

The easiest way is to use $args. This is easy to use inside the script but a little harder to keep organized.

———– args.ps1 example ————-

Use $args without param block

$FirstName = $args[0]

$LastName = $args[1]

$Age = $args[2]

Write-Host "First Name: $FirstName"

Write-Host "Last Name: $LastName"

Write-Host "Age: $Age"


In the console you’d set it up like this. Note how the parameters show as args[0], args[1], etc.

Also note that in the prompt for parameter input, parameter name is left empty. This is important because else the parameter will be turned into a named parameter.

Param() block

Another option is to use named parameters. Using named parameters however you need a bit of code in your script to tell Powershell what parameters to expect.

——————– params.ps1 ——————–

param (

[string]$FirstName,

[string]$LastName,

[int]$Age

)

Write-Host "First Name: $FirstName"

Write-Host "Last Name: $LastName"

Write-Host "Age: $Age"


This script is essentially the same as the $args script, except the parameters are named instead of positional.

In this case we are giving the parameter a name and you can see that the parameter is shown as $FirstName, $lastName, etc.

noParams.ps1

Last but not least, the version I think you might have been using.

This script configures ControlUp to pass named parameters. But doesn’t tell Powershell which parameters to expect.

Note how this script uses named parameters.

————– noParams.ps1 —————

can’t use named parameters without param block

Write-Host "First Name: $FirstName"

Write-Host "Last Name: $LastName"

Write-Host "Age: $Age"


This script does nothing special and assumes it can just use $age, $LastName, etc. However Powershell will not understand how to accept these parameters.

ControlUp will still ask for the values but they will not work inside the script.

I’m showing prompted parameters. Same concept applies to record properties. Only give them names if you have a [param] block

Continue reading and comment on the thread ‘How to Use Parameters in Powershell Scripts for ControlUp’.  Not a member? Join Here!


Categories: All Archives, ControlUp Scripts & Triggers
Topics: Logs, PowerShell, Scripts, Triggers

Ask Us Anything, Connect, Learn, and Grow with the ControlUp Community!

Login to the ControlUp Community to ask us anything, stay up-to-date on what’s new and coming soon and meet other like-minded techies like you.

Not already a member? Join Today!

Primary Sidebar

ControlUp Academy

Enroll in ControlUp Academy for expert-led technical training, equipping you with skills to effectively deploy, manage, and grow your ControlUp investment.

Learn here >

Rotating Images

Hidden Gem from our Community on Slack!

ControlUp Betas - What's Coming Next?
NEW ControlUp Features - Stay Up-to-Date!
ControlUp Scripts - Scripting, Zero to Hero
Latest KB Articles - Be the First to Learn
Did you Know - with Sivan Kroitoru
Practical Perspectives Technical Use Case Training

Video Tutorials Library

Visit our technical how-to videos, offering step-by-step tutorials on advanced features, troubleshooting, and best practices.

Watch here >

ControlUp Blog

Check out the ControlUp blog for expert advice and in-depth analysis.

Read here >

ControlUp Script Library

Visit the ControlUp technical script library, which offers a multitude of pre-built scripts and custom actions for your monitoring and troubleshooting requirements.

See here >

ControlUp Support

Visit the ControlUp support home and to delve deeper into ControlUp solutions.

Browse here >

Download ControlUp RealTime DX

Start with ControlUp for real-time end-user environment insights, swift troubleshooting, and unprecedented performance optimization. Download now.

Download here >

Footer

      

ControlUp Community
Of Techie, By Techie, For Techie!

Terms of Use | Privacy Policy | Security
Dive Deeper, Learn more at ControlUp.com

  • facebook
  • twitter
  • youtube
  • linkedin

© 2023–2025 ControlUp Technologies LTD, All Rights Reserved.

We use cookies to ensure that we give you the best experience on our website. by continuing to use this site you agree to our Cookie policy..