Good morning Powershell people!! I am writing a script that needs to take a wait time parameter and pass it to Start-Sleep. My script works exactly as I expect when I hard code my wait time. The issue I am having is when I declare my parameter in the script and pass a default of 30, I get an error. Here is my declaration section:
[pre][CmdletBinding()]
param (
[Parameter(Mandatory)]
[String]$Environment,
[ValidateNotNullOrEmpty]
[Parameter]
[Int32]
$Wait = 30
)[/pre]
The error I am getting is on initiation of the script. I have googled, but my google-fu is weak today
[pre]: Cannot process argument transformation on parameter ‘Wait’. Cannot convert the “30”
value of type “System.Int32” to type “System.Management.Automation.ParameterAttribute”.[/pre]
Edit: A couple of things.
- Parameter should have () or it probably would not validate properly. Although params don't need to be strongly declared (e.g. Mandatory=$true), it makes it easier to understand what you are doing.
- [ValidateNotNullOrEmpty] isn't necessary, you a declaring it as an INT, so you have to pass a valid number to the cmdlet
- int should be more than sufficient for sleep, no need to do int32
- The variable should be $WaitInSec or something that tells a user what time measurement is being used. Minimally help provided to ensure they understand it's seconds vs minutes.
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[string]$Environment,
[Parameter(Mandatory=$false)]
[int]$Wait = 30
)
Rob, Thanks for the quick reply. It didn’t change the behavior. Here’s the output:
[pre]
PS C:\WINDOWS\system32> [CmdletBinding()]
>> param (
>> [Parameter(Mandatory=‘False’)]
>> [String]$Environment,
>> [ValidateNotNullOrEmpty]
>> [Int]$Wait = 30
>> )
cmdlet at command pipeline position 1
Supply values for the following parameters:
Environment: TEST
Cannot process argument transformation on parameter ‘Wait’. Cannot convert the “30” value of type “System.Int32” to type “System.Management.Automation.ValidateNotNullOrEmptyAttribute”.
+ CategoryInfo : InvalidData: (
, ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError
[/pre]
Good points. Let me implement and see if that fixes the issue I am seeing. The reason I used [ValidateNotNullOrEmpty] is so I can pass the default of 30 seconds if the user doesn’t specify a wait time.
You are setting a default value, so if a user does not pass a value it will use that. If a user does pass a value, it has to validate to be a numeric number in the int range.
function Test-It {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[string]$Environment,
[Parameter(Mandatory=$false)]
[int]$Wait = 30
)
'Waiting {0} seconds for environment {1}' -f $Wait, $Environment
}
Test-It -Environment Alpha
Output:
PS C:\Users\rasim> Test-It -Environment Alpha
Waiting 30 seconds for environment Alpha
PS C:\Users\rasim> Test-It -Environment Bravo -Wait 3
Waiting 3 seconds for environment Bravo
PS C:\Users\rasim> Test-It -Environment Bravo -Wait
Test-It : Missing an argument for parameter 'Wait'. Specify a parameter of type 'System.Int32' and try again.
At line:1 char:28
+ Test-It -Environment Bravo -Wait
+ ~~~~~
+ CategoryInfo : InvalidArgument: (:) [Test-It], ParameterBindingException
+ FullyQualifiedErrorId : MissingArgument,Test-It
PS C:\Users\rasim>
It was the [Validatenotnullorempty] that was causing the problem. I modified it to what you have and no issue. Thanks.