Question:
I’ve read widely that I can pass in an argument to my powershell script via:
1 2 3 4 |
param ( [Datetime]$argument ) |
I’ve also read that I can define a default value:
1 2 3 4 |
param ( [Datetime]$argument = Get-Date "5/1/2006 7:00 AM" ) |
However on doing this I get:
At C:\Users\medmondson\Desktop\Scripts\ScrumTimer.ps1:2 char:26
+ [Datetime]$argument = Get-Date “5/1/2006 7:00 AM”
+ ~ Missing expression after ‘=’. At C:\Users\medmondson\Desktop\Scripts\ScrumTimer.ps1:2 char:24
+ [Datetime]$argument = Get-Date “5/1/2006 7:00 AM”
+ ~ Missing ‘)’ in function parameter list. At C:\Users\medmondson\Desktop\Scripts\ScrumTimer.ps1:3 char:1
+ )
+ ~ Unexpected token ‘)’ in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingExpressionAfterToken
This only appears to occur for DateTime
, any idea where I’m going wrong?
Answer:
try enclose value in ()
1 2 3 4 |
param ( [Datetime]$argument = (Get-Date "5/1/2006 7:00 AM") ) |