My script's input parameter is a date or a number. Here's a script that works fine, so you can see what I am trying to do:
param($date = (Get-Date))
if ($date -match "^\d+$")
{
$date = (Get-Date).AddDays($date)
}
elseif ($date -as [DateTime])
{
$date = [DateTime]::Parse($date)
}
else
{
'You entered an invalid date'
exit 1
}
Here's my previous attempt that does not work:
param($date = (Get-Date))
if ($date -as [DateTime])
{
$date = [DateTime]::Parse($date)
}
elseif ($date -match "^\d+$")
{
$date = (Get-Date).AddDays($date)
}
else
{
'You entered an invalid date'
exit 1
}
When I input a number, the script breaks at date parsing line. It looks like my "is is date" check returns true when given a number.
Is it a bug? Is it by design?