Powershell Windows Forms onClick event

4.8k Views Asked by At

I have a Windows forms (in PowerShell), and the form has a button with onclick event but the command in the onclick isn't run. I don't know why, because it can be run sepratley. My sciptblock is here:

[void][reflection.assembly]::loadwithpartialname("System.Windows.Forms")
$form = New-Object System.Windows.Forms.Form
$form.Width = 500
$form.Height = 200
$form.formborderstyle = 3
$form.StartPosition = "CenterScreen"

$button1 = New-Object System.Windows.Forms.Button
$button1.Text = "Search"
$button1.Height = 25
$button1.Width = 85
$button1.Top = 100
$button1.Left = 25
$button1_OnClick = 
    {
        Get-EventLog -LogName Security -After $a2.Value.Date | Where-Object 
{$_.EventID -eq "4800" -or $_.EventID -eq "4801"} | Format-Table
    }
$button1.add_Click($button1_OnClick)
$form.Controls.Add($button1)

$form.ShowDialog()

Please help to resolv my problem. Thanks advance!

1

There are 1 best solutions below

1
On BEST ANSWER

2 things

You did not define $a in

Get-EventLog -LogName Security -After $a2.Value.Date

Second you didnt decide how to output the table. You can do something like...

Get-EventLog -LogName Security -After $a2.Value.Date | Where-Object{$_.EventID -eq "4800" -or $_.EventID -eq "4801"} | Format-Table | Out-Host