Empty Coder
Empty Coder

Reputation: 589

Join date and time as datetime format and pass it in schedule trigger powershell

I have below date and time in 2 separate variable. I am trying to combine these 2 as date time and create a scheduler based on that but getting error

Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
At line:2 char:1
+ $dt1 = [datetime]::ParseExact($dt, 'g',[CultureInfo]::InvariantCultur ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FormatException

Below is my code

$L_Friday="25/02/2022"
$dt = $L_Friday + " " + "10:00:00 AM"
$dt1 = [datetime]::ParseExact($dt, 'dd/MM/yyyy hh:mm:ss',[CultureInfo]::InvariantCulture)
$Trigger = New-ScheduledTaskTrigger -Once -At ""

Please need your help to get the issue here

Upvotes: 0

Views: 758

Answers (1)

Md. Shadikul Islam
Md. Shadikul Islam

Reputation: 339

Your code will be like this:

$L_Friday="25/02/2022"
$dt = $L_Friday + " " + "10:00:00 AM"
$dt1 = [datetime]::ParseExact($dt, 'dd/MM/yyyy hh:mm:ss tt' [CultureInfo]::InvariantCulture)
echo $dt1

For more details, you can check this StackOverflow Question and Answer.

Upvotes: 1

Related Questions