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.
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.”
}
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:
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?
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
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
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
)