String to Date Conversion

This topic contains 7 replies, has 3 voices, and was last updated by Profile photo of Itta Itta 1 year, 11 months ago.

  • Author
    Posts
  • #24488
    Profile photo of Itta
    Itta
    Participant

    I have captured "[u][i]4/21/2015 9:56:44 AM[/i][/u]" from a text file and stored it in a string variable ($myContent).
    I now want to convert this into DateTime data type. Here is my code

    [string]$myContent = Get-Content C:\MyFile.txt
    [datetime]::ParseExact($myContent,”MM/dd/yyyy h:mm:ss”,$null) #This throws an error "String was not recognized as a valid DateTime"#

    Can someone help me understand what is going wrong and suggest some solution here?

    Thanks.

  • #24489
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Have you tried just...

    [datetime]$mydate = $string
    

    And see if the parser can figure it out without you specifying the format?

  • #24495
    Profile photo of Itta
    Itta
    Participant

    Hi Don, I tried the way you mentioned. Hope what you mentioned is as how it is in Line 4 (correct me if I am wrong.)

    Line 1. [datetime]$whichDate = Read-Host "Enter date in MM/DD/YYYY format"
    Line 2. [datetime]$myDate = ($whichDate).AddDays(-1)
    Line 3. $myContent = Get-EventLog -LogName System -Message "*Kernel-Boot*" -After $myDate -Newest 1 -OutVariable Var |select TimeGenerated |ft -HideTableHeaders
    Line 4. [datetime]$myTime = $myContent

    Below is the error PS throws:

    Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.DateTime".
    At line:1 char:1
    + [datetime]$myTime = $myContent
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : MetadataError: (:) [], ArgumentTransformationMetadataException
    + FullyQualifiedErrorId : RuntimeException

  • #24498
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Well, without knowing exactly what you're providing as input, I can tell you that the following works:

    PS C:\> [datetime]$x = "11/22/1971"
    
    PS C:\> $x | get-member
    
    
       TypeName: System.DateTime
    
  • #24500
    Profile photo of Dan Potter
    Dan Potter
    Participant

    you are not converting your string in line 2.

    $whichDate = Read-Host "Enter date in MM/DD/YYYY format"
    $mydate = ([datetime]$whichDate).AddDays(-1)

  • #24503
    Profile photo of Dan Potter
    Dan Potter
    Participant

    Here is a better way to filter the eventlog. build your filter in eventvwr and copy from the xml tab.

    
    					$filterXML = @'
    
      
        *[System[Provider[@Name='Microsoft-Windows-WindowsUpdateClient'] and (Level=4 or Level=0) and (EventID=19)]]
      
    
    '@
    					
    					$timespan = (get-date).adddays(-2)
    					$eventlist = Get-WinEvent -computername $server -MaxEvents 10 -FilterXml $filterXML -erroraction "silentlycontinue" -Credential $credential | ? { $_.timecreated -gt $timespan }
    
    
  • #24504
    Profile photo of Dan Potter
    Dan Potter
    Participant

    forum is not accepting code block.

  • #24517
    Profile photo of Itta
    Itta
    Participant

    Thank you all. Below code worked for me, the obstetrical I had was to convert String to datetime.
    If you see in Line 3 even though I am eliminating Header and reading the date, the variable $myContent is storing some extra characters and thus when I try to convert, it throws an error. However I passed $myContent variable data into a text file and then read the content of text file into a separate variable $myVar and then applied datatype conversion on $myVar, which worked out here.

    I am sure there would be much easy approaches, however this worked for me and unblocked me. Once again thank you all for your responses and support.

    Line 1. [datetime]$whichDate = get-date
    Line 2. [datetime]$myDate = ([datetime]$whichDate).AddDays(-1)
    Line 3. $myContent = Get-EventLog -LogName System -Message "*Kernel-Boot*" -After $myDate -Newest 1 |select TimeGenerated |ft -HideTableHeaders
    Line 4. $myContent |Out-File C:\MyFile.txt
    Line 5. $myVar = Get-Content C:\MyFile.txt
    Line 6. [datetime]$myTime = [datetime]::Parse("$myVar")

You must be logged in to reply to this topic.