Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading