读取.ini并将字符串转换为DateTime powershell

时间:2017-05-18 04:38:54

标签: c# powershell datetime scripting

好的,基本上我在这里读取/写入.ini配置文件。我从.ini中提取的是一个格式为MM/dd/yyyy HH:mm:ss tt的字符串。我似乎无法合并的部分是'tt'部分(AM / PM)。每当我尝试执行此代码时:

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

我收到此错误:

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

我非常确定我有:

[System.Globalization.CultureInfo]::InvariantCulture)

错误。我已经尝试了很多关于如何解决这个问题的变化,似乎无法解决这个问题。

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)

0 个答案:

没有答案