How can I ensure that a specified date for a parameter is in a certain format?

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.

Hi
the
| Out-null
was the culprit.
So I do get a true now when the user specifies the date in the correct format.
However, if he/she does not, there is no soft exit but the unwanted error message by powershell in red squiggly lines.
That is exactly what I want to avoid at all cost.
Thank you for some idea how to get around that.

Funny - only minutes after I published the topic I realized the possible error with out-null.

Andreas,
Welcome to the forum. :wave:t4:

There are actually two solutions for your challenge. :wink:

  • 1 - You could check the input you get for your function with a [ValidatePattern] and an according HelpMessage.

  • 2 - You could add a proper error handling with a try-catch-block.

Here you have the links for both of the help topics.

Edit:

And BTW: You should not use Write-Host!!

Hi,
I found the solution and it was quite simple. I just wouldn’t see the tree for the forest - or however you say in english.
This is the function now and it works:

function Test-Date($passedDateString){
    $result= $passedDateString -as [DateTime] -ne $null

    If ($result){

        try{ 
            [bool]$Is_exactDate = [datetime]::ParseExact($passedDateString,"dd.MM.yyyy HH:mm",$Null) 
            write-host $Is_exactDate -f red
            return $Is_exactDate

        } catch {
            return $false
        }
    }
# If first check is false script ends here:
    return $result
}

I just had to put it in a try-catch, what else, right?
Thank you anyway. Just the fact that I opened a topic here helped me to think it thrue more precisely.
And so I found the solution myself. It often happens that way to me.
Cheers,
Andreas

P.S. the write-host I will comment out, as I don’t need it any longer.

Yes - thank you.
Validate Pattern is no good though. That is not really a soft exit.
Try-Catch is the way to go.
To me, at least.

Well - here write-host was reflecting what was going on though.
It gave me a false as long as it went wrong because of | out-null
and True when it worked.
OK - thank you.
Till next time …