I am trying to pass various parameters to a PowerShell script, however if no value is passed that script exits with an error. For example:
.powershellscript.ps1 -path C:\test -Name text.txt -Date 20111212
Works fine, however if I execute:
.powershellscript.ps1 -path C:\test -Name text.txt -Date
I receive the following error
“Missing an argument for parameter ‘Date’. Specify a parameter
of type ‘System.Object’ and try again.”
I want to be able to assign the Date value to $Null if it’s not passed through the script.
Inside the .powershellscript.ps1, the parameters is handled as:
param(
$path,
$Name,
$Date
)
Any help would be greatly appreciated.
If you don’t want to pass a value for
$Datecall the script like this:$Dateshould then be$nullin your script.