0

Ok, Basically i'm doing a read/write to a .ini config file here. What I'm pulling from the .ini is a string in the format of MM/dd/yyyy HH:mm:ss tt. The part I cant seem to incorporate is the 'tt' portion (AM/PM). Any time I try to execute this code:

$ConvertStringToDate = [datetime]::ParseExact($value,"MM/dd/yyyy HH:mm:ss",[System.Globalization.CultureInfo]::InvariantCulture)

I get this error:

Cannot find an overload for "ParseExact" and the argument count: "3".
At line:1 char:1
+ $ConvertStringToDate = [datetime]::ParseExact($value,"MM/dd/yyyy HH:m ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

I'm very certain that I have the:

[System.Globalization.CultureInfo]::InvariantCulture)

Wrong. I've tried tons of variations of how to fix this and cant seem to figure it out.

function Get-IniContent ($FilePathINI)
{
    $ini = @{}
    switch -regex -file $FilePathINI
    {
        “^\[(.+)\]” # Section
        {
            $section = $matches[1]
            $ini[$section] = @{}
            $CommentCount = 0
        }
        “^(;.*)$” # Comment
        {
            $value = $matches[1]
            $CommentCount = $CommentCount + 1
            $name = “Comment” + $CommentCount
            $ini[$section][$name] = $value
        } 
        “(.+?)\s*=(.*)” # Key
        {
            $name,$value = $matches[1..2]
            $ini[$section][$name] = $value
        }
    }
    return $ini
}
function Out-IniFile($InputObject, $FilePathINI)
{
    $outFile = New-Item -ItemType file -Path $FilePathINI -Force
    foreach ($i in $InputObject.keys)
    {
        if (!($($InputObject[$i].GetType().Name) -eq “Hashtable”))
        {
            #No Sections
            Add-Content -Path $outFile -Value “$i=$($InputObject[$i])”
        } else {
            #Sections
            Add-Content -Path $outFile -Value “[$i]”
            Foreach ($j in ($InputObject[$i].keys | Sort-Object))
            {
                if ($j -match “^Comment[\d]+”) {
                    Add-Content -Path $outFile -Value “$($InputObject[$i][$j])”
                } else {
                    Add-Content -Path $outFile -Value “$j=$($InputObject[$i][$j])” 
                }

            }
            Add-Content -Path $outFile -Value “”
        }
    }
}



$store = @()

Foreach($event in Get-EventLog "Application" -Newest 50 | Where-Object {$_.EventID -eq 18456})
{
   #$EventID = $event.EventID
   $store += $Event
}

$sortedEvents = $store | Sort-Object -Descending

$latestEvent = $sortedEvents[0]

$eventDate = $latestEvent.TimeGenerated
$culture = New-Object System.Globalization.CultureInfo("en-US")
#$eventDate = (get-date).Date.ToString("MM/dd/yyyy")



$iniContent = Get-IniContent "C:\Scripts\EventLog\EventConfig.ini"
$value = $iniContent["section"]["LastProcessDate"]


$ConvertStringToDate = [datetime]::ParseExact($value,"MM/dd/yyyy HH:mm:ss",$null)
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
Keifer
  • 39
  • 2
  • 7

0 Answers0