ShowDialog
waits until the window is closed. Your script will not continue running until the user closes the window manually.
You need to use
Show
instead. You'll need to keep a reference to the form in a variable so that you can close it when the file is found. That will be easier to do if you keep your script in a single file.
For example:
function CreateForm {
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object System.Windows.Forms.Form
$Form.ClientSize = '578,400'
$Form.Text = "Form"
$Form.BackColor = "#c1daf7"
$Form.WindowState = 'Maximized'
$Form.FormBorderStyle = "FixedDialog"
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Text = "UNDER PROCESS"
$Label1.AutoSize = $true
$Label1.Width = 25
$Label1.Height = 10
$Label1.Location = New-Object System.Drawing.Point(600,300)
$Label1.Font = 'Microsoft Sans Serif,30,style=Bold,Underline'
$Label1.ForeColor = "#d0021b"
$Label2 = New-Object System.Windows.Forms.Label
$Label2.Text = "WAITING"
$Label2.AutoSize = $true
$Label2.Width = 25
$Label2.Height = 10
$Label2.Location = New-Object System.Drawing.Point(770,500)
$Label2.Font = 'Microsoft Sans Serif,20,style=Bold'
$Label2.ForeColor = "#fb0505"
$Form.Controls.AddRange(@($Label1,$Label2))
return $Form
}
function Find($folder, $pattern) {
$n = 0
$form = $null
while (-not (Get-ChildItem -Name $folder | Where-Object {$_ -like $pattern})) {
if ($form -eq $null) {
$form = CreateForm
$form.Show()
}
Start-Sleep -s 1
$n++
Write-Host "Attempt no $n"
}
if ($form -ne $null) {
$form.Close()
$form.Dispose()
}
Write-Host ">>Flag found after $n attempts"
return $true
}
$SN = "708TSTA"
$MAC = "2E5961370"
$folder = "D:\SERVER\"
$pattern = "*$SN-$MAC*"
if (Find $folder $pattern) {
Write-Host "Found"
}
Form.ShowDialog Method (System.Windows.Forms) | Microsoft Docs[
^]
Form.Show(IWin32Window) Method (System.Windows.Forms) | Microsoft Docs[
^]