命令顺序错误,日历总是显示倒数第二个日期(Powershell)

Wrong order of commands, the calendar always shows the penultimate date (Powershell)

我制作了一个小的powershell 脚本(带有GUI),您可以在其中显示您的计算机在特定日期的启动/关闭时间。我的问题是,每次您在日历上选择所需的日期时,它都会显示从我之前选择的日期开始的启动/关闭时间。因此,当我启动程序并选择 16/1/2021 时,它会显示我选择的日期,即我上次打开程序的日期,例如 8/1/2021。命令的顺序有什么问题。谢谢您的帮助!顺便说一句,我是 PowerShell 的新手。 :)

代码:

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

Add-Type -AssemblyName presentationframework
$GUI1 = New-Object System.Windows.Forms.Form
$Calendar = New-Object System.Windows.Forms.MonthCalendar
$Button = New-Object System.Windows.Forms.Button


$Calendar.Location = '105,23'
$Calendar.Size = '250,250'
$Calendar.MaxSelectionCount = 1
$Calendar.ShowTodayCircle = $false


$Button.Location = '125,315'
$Button.Size = '225,100'
$Button.Text = "Select this date"
$Button.Add_Click({

[System.Windows.Forms.MessageBox]::Show("PC Started at: $event1`nPC Shutdown at: $event2")

})



$GUI1.Size = '500,500'
$GUI1.Controls.Add($Calendar)
$GUI1.Controls.Add($Button)
$GUI1.ShowDialog() | Out-Null


#----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#


$date = $calendar.SelectionStart
$end = $date.AddMinutes(1439)

$date


$event1 = Get-EventLog -LogName System -After $date -Before $end | Where { 6005-contains $_.EventID} | Select -ExpandProperty TimeGenerated -First 1
$event2 = Get-EventLog -LogName System -After $date -Before $end | Where { 6006-contains $_.EventID} | Select -ExpandProperty TimeGenerated -First 1

像这样

$Button.Add_Click({
$date = $calendar.SelectionStart
$end = $date.AddMinutes(1439)
$date
    $event1 = Get-EventLog -LogName System -After $date -Before $end | Where { 6005-contains $_.EventID} | Select -ExpandProperty TimeGenerated -First 1
    $event2 = Get-EventLog -LogName System -After $date -Before $end | Where { 6006-contains $_.EventID} | Select -ExpandProperty TimeGenerated -First 1
[System.Windows.Forms.MessageBox]::Show("PC Started at: $event1`nPC Shutdown at: $event2")

 })