PowerCLI

 View Only
  • 1.  Help getting a date and time string into get-date format

    Posted Jul 04, 2010 10:03 PM

    When taking a date / time string from an object:

    The format of the input is a string and get-date doesn't seem to be able to convert it to

    to a System.DateTime.

    I want to compare that value to 30 days ago so get-date((get-date).adddays(-30))

    When I try the following, it seems to work and import it:

    (get-date -UFormat "%d/%m/%Y %T"("30/06/2010 00:00:00"))

    If I add the following:

    '[datetime]'(get-date -UFormat "%d/%m/%Y %T"("30/06/2010 00:00:00"))

    It can't convert it.

    Without the datetime component, when I try a greater than test it uses just the date

    part so:

    $Test = "10/06/2009 00:00:00"

    If($Test -gt (get-date -UFormat "%d/%m/%Y %T"("05/06/2010 00:00:00")))

    {

    $Tested = "Yes"

    }

    Else

    {

    $Tested = "No"

    }

    $tested

    Results in a Yes desite being earlier.

    So in the script it returns any of the entries where the date is greater

    than 05 and ignore the year, month and time.

    Any help with this would be greatly appreciated.

    Thanks,



  • 2.  RE: Help getting a date and time string into get-date format
    Best Answer

    Posted Jul 05, 2010 05:31 AM

    This has to do with the "culture" you're using in your PowerShell session.

    You can use the Get-Culture cmdlet to see which one you're using.

    If you try

    [datetime](get-date -UFormat "%m/%d/%Y %T"("30/06/2010 00:00:00"))
    

    The -UFormat parameter produces a string and

    the cast expects by default the mm/dd/yyyy format for the date part.

    So by reversing the month and day in your -UFormat string that is corrected.

    Btw I attached the line since the forum SW doesn't like square brackets.

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 3.  RE: Help getting a date and time string into get-date format

    Posted Jul 05, 2010 08:42 AM

    Thank you once again for your amazing PS knowledge. This worked perfectly although not sure exactly why,

    The culture settings displayed from get-culture indicate English (UK) and EN-GB.

    Anyway it works like this and displays the date in UK after import and the calculation works fine so I'm happy.

    Thanks again,

    Dan



  • 4.  RE: Help getting a date and time string into get-date format

    Posted Jul 05, 2010 08:55 AM

    Fyi, this whole type casting discussion is a bit religous (is it a language feature or is it a bug ?).

    Have a look at the thread bug in string<->datetime conversion?, where Lee Holmes explains this far better then I ever could.

    ____________

    Blog: LucD notes

    Twitter: lucd22