Parameters Issues - Scripts

Product: PowerShell Universal
Version: 5.0.8

It appears that parameters are not working as expected in version 5.0.8.

I attempted for example to add the following to my script.

param([Switch]$Switch)

I was expecting a switch or toggle to appear. Instead I have a checkbox.

Since, moving migrating from v4 to v5 I have noticed that many of my script parameters are no longer appearing when the script is run. I first noticed this when the upload file parameter was not working. Now, I came across this now parameter issue.

Ironman might need to recheck that all the parameters are working as expected.

You may want to check out the current version’s script parameter documentation [1] to ensure you are setting the parameters up in the way you expect.

For example, a toggle switch is now expressed as type [bool]$Parameter instead of [switch]Parameter. Functionally they work the same - either they are on or off.

Keep in mind that when you would run a script with PowerShell, script parameters would work like this:

Run-Function -SwitchParameter -OtherParameter
Whereas a boolean parameter would work like this:
Run-Function -BoolParameter $True -OtherParameter
These are equivalent.

You mentioned issues with other parameters as well - please check through the 5.0 documentation to see how those parameters may need to be defined differently in the newer documentation.

Hi Zoe, once again we meet :),

This is what I a currently using. Is this not correct?

param (
[string]$partialAssetId = “”, # Set default to an empty string
[Bool]$ConfirmDeprovision = $false # Correct Bool toggle, default set to false
)

Example of prompting the user for Asset ID if it’s not provided

if (-not $partialAssetId) {
$partialAssetId = Read-Host “Enter the full or partial Asset ID (e.g., 3740)”
}

Now you can use the Boolean parameter for confirmation, like:

if ($ConfirmDeprovision) {
Write-Host “Deprovision confirmed.”
} else {
Write-Host “Deprovision not confirmed.”
}

To my understanding, that looks right!

However, instead of using the Read-Host block there, you can instead set the property to mandatory to enforce the script’s parameters to be filled out at runtime:

param(
  [Parameter(Mandatory)][string]$partialAssetId
  [bool]$confirmDeprovision
)

Although I suppose it depends on how your scripts are being ran - do you have schedules set up? Do end users launch these from within an app? etc. That can help you determine the best way to validate your parameters.

Are you getting the behavior you expect with the [bool] type? Were any other params causing you issues?

Hi Zoe,

This did not work unfortunately. Please see my actual script below.

param (
#[string]$partialAssetId = “Enter the full or partial Asset ID (e.g., 3740)”,
[Parameter(Mandatory)][string]$partialAssetId,
[Bool]$ConfirmDeprovision
)

Check if deprovision is confirmed

if (-not $ConfirmDeprovision) {
Write-Host “Deprovision not confirmed. Please toggle the confirmation switch to proceed.”
exit
}

Define the GAM command to retrieve deviceId and annotatedAssetId

$gamCommand = “gam print cros fields deviceId,annotatedAssetId”

Execute the GAM command and capture the output

$gamOutput = & cmd /c $gamCommand

Parse the output into an array of objects

$deviceInfo = @()
$gamOutput | ForEach-Object {
if ($_ -match ‘([a-f0-9-]+),(.+)’) {
$deviceInfo += [PSCustomObject]@{
deviceId = $matches[1]
annotatedAssetId = $matches[2]
}
}
}

Function to get the correct device based on the annotatedAssetId

function Get-CorrectAnnotatedAssetId {
param (
[string]$partialId
)

# Search for the annotatedAssetId containing the partial number
$matchingDevices = $deviceInfo | Where-Object { $_.annotatedAssetId -like "*$partialId*" }

# Return the matching devices or exit if no match is found
if ($matchingDevices.Count -eq 1) {
    return $matchingDevices
} elseif ($matchingDevices.Count -gt 1) {
    Write-Host "Multiple matches found for the partial annotatedAssetId. Please review:"
    $matchingDevices | ForEach-Object { Write-Host "Device ID: $($_.deviceId), Asset ID: $($_.annotatedAssetId)" }
    exit
} else {
    Write-Host "No match found for the partial annotatedAssetId. Exiting."
    exit
}

}

Call the function to get the correct annotatedAssetId

$matchingDevice = Get-CorrectAnnotatedAssetId -partialId $partialAssetId

Run the GAM command to deprovision the device

$deprovisionCommand = “gam update cros $($matchingDevice.deviceId) action deprovision_different_model_replace acknowledge_device_touch_requirement”
& cmd /c $deprovisionCommand

Write-Host “The device with Asset ID $($matchingDevice.annotatedAssetId) has been deprovisioned.”

What I see when run.

PSU

Ah! Interesting!

The Mandatory flag is being passed through fine, but it looks like the [bool] parameter type is still showing as a checkbox. @adam is the [bool] parameter type behaving differently than the documentation as demonstrated above by design?

Also, @jmartins1 you can use the [Parameter(HelpMessage="")] flag to add helpful text under the parameter. For your asset ID, for example, you could modify the parameter definition to the following:

param(
  [Parameter(Mandatory,HelpMessage="Enter the full or partial Asset ID (e.g., 3740)")][string]$partialAssetId,
  [Parameter(HelpMessage="Please select to confirm deprovisioning of the asset.")][Bool]$ConfirmDeprovision
)

This will display like this:

Thanks Zoe, I do like that much better :). I appreciate your assistance.

2 Likes