Witam,
Tworzę sobie mały skrypt, który będzie powiadamiać mnie o tym, jeśli coś zostanie wklejone do określonego folderu na dysku. Piszę to w powershellu z użyciem Windows Forms, gdzie jestem dużym laikiem.
Skrypt jest praktycznie ukończony, lecz mam mały problem.. Przy naciśnięciu przycisku "OK", aplikacja mi się zawiesza, muszę skillować proces. Jeśli uruchomię program to nie mam problemów gdy nacisnę "X". Chciałbym aby skrypt nadal działał w tle, aby na bieżąco mnie informował, czyli tak jakby proces ma nie być killowany, tylko zamykane okno powiadomienia.
function eventhandler {
Function Generate-Form {
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Build Form
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "ALERT"
$Form.StartPosition = "CenterScreen"
$Form.Topmost = $True
$Form.BackColor = "White"
$Form.Width = 1920
$Form.Height = 920
$Image = [system.drawing.image]::FromFile("C:\photo.jpg")
$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.width=600
$pictureBox.height=200
$pictureBox.top=15
$pictureBox.left=1300
$pictureBox.Image=$Image
#$Form.BackgroundImage = $Image
#$Form.BackgroundImageLayout = "None"
$redLabel = New-Object System.Windows.Forms.Label
$redLabel.Location = "150,400"
$redLabel.Height = 133
$redLabel.Width = 500
$redLabel.Text = "Cos przyszlo!"
$redLabel.ForeColor = "Red"
$redLabel.BackColor = "Transparent"
$redLabel.Autosize = $True
$Font = New-Object System.Drawing.Font("Arial",75,[System.Drawing.FontStyle]::Bold)
$redLabel.Font = $Font
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(1000,800)
$Button.Size = New-Object System.Drawing.Size(150,50)
$Button.Text = "OK"
$Button.Add_Click({$Form.Close()})
$Form.Controls.Add($pictureBox)
$Form.Controls.Add($redLabel)
$Form.Controls.Add($Button)
$Form.ShowDialog()
}
Generate-Form
}
$folder = “C:\test”
$filter = “*”
$fsw = New-Object -TypeName System.IO.FileSystemWatcher -ArgumentList $folder, $filter
$fsw.IncludeSubDirectories = $true
$action = {eventhandler $($event)}
Register-ObjectEvent -InputObject $fsw -EventName “Created” -SourceIdentifier “File System Creation” -Action $action
Prosiłbym o pomoc, gdyż używałem kilku opcji (Exit, tworzyłem oddzielną funkcję itd) i żadne nie przyniosło rezultatów.