Hello,
I have a script with a string parameter that should be given in a DateTime format.
To be precise in this format:
dd.MM.yyyy HH:mm (“30.12.2021 08:41”)
I have a function to check the passed date:
function Test-Date($passedDateString){
$result= $passedDateString -as [DateTime] -ne $null
If ($result){
[bool]$Is_exactDate = [datetime]::ParseExact($passedDateString,"dd.MM.yyyy HH:mm",$Null) |Out-Null
write-host $Is_exactDate -f red
return $Is_exactDate
}
return $result
}
-as [DateTime]
would accept a date without a time specified.
Because of this I continued to test $passedDateString
with parseExact
for a precise format.
But if I feed the function a date as I want it – for example, "30.12.2021 08:41"
, that is including the time, it returns false
regardless.
Any ideas would be welcome.
I need to make sure that I get a date with hours and minutes specified.
But I don’t want the script to throw an error message in red to the user, but rather intercept the error if the user just specifies a date without time for the datetime parameter. Then I want to tell the user what happened and what he/she needs to do and why this failed this time.
I want a soft exit rather than an error message in squiggly red lines of text by powershell.
Should you have a different idea or approach to accomplish this that would be just as well.
I googled quite a bit and tried many things. It just won’t work, it seems.
The setting for culture at my place is this:
LCID Name
---- ----
1031 de-DE
Thank you.