I'm newer to Powershell and am running into this issue. I have a very long automated install script that runs through MySQL install/account setup and other .msi's.
While the users wait for the script to complete, I have this "Please Wait" prompt display that contains a spinning pinwheel .gif in the picturebox.
The problem is that if I set the form.visible to $true and have it close later on in the script, then the .gif itself will not move. It displays but loses its animation. If I change the form.visible to "false" and change the form to modal with adding a form.showdialog() parameter, the animation is perfect in the .gif but the script halts and can only progress after the window is closed. Is there a way for the script to progress without losing the animation in the .gif? Please help. I've found very little forums on powershell and .gif's for some reason. Here is the code for the form.
$Form = New-Object system.Windows.Forms.Form
$Form.Location= New-Object System.Drawing.Size(100,100)
$Form.Size= New-Object System.Drawing.Size(550,170)
$Form.StartPosition = "Manual"
$Form.Visible=$true
$Form.Enabled = $true
[reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
$file = (get-item 'PathToMyGifFile')
$img = [System.Drawing.Image]::Fromfile($file);
[System.Windows.Forms.Application]::EnableVisualStyles();
$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Location = New-Object System.Drawing.Size(0,1)
$pictureBox.Size = New-Object System.Drawing.Size(100,80)
$pictureBox.Image = $img
$Form.controls.add($pictureBox)
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Text = "Please wait for Installation to complete"
$Label1.Location= New-Object System.Drawing.Size(110,35)
$Label1.Size = New-Object System.Drawing.Size(400,25)
$Label1Font = New-Object System.Drawing.Font("Tahoma",10,
[System.Drawing.FontStyle]::Bold)
$Label1.Font = $Label1Font
$Label1.BackColor = "white"
$Form.Controls.Add($Label1)
$Label2 = New-Object System.Windows.Forms.Label
$Label2.Text = "This may take several minutes . . ."
$Label2.Location= New-Object System.Drawing.Size(120,60)
$Label2.Size = New-Object System.Drawing.Size(370,75)
$Label2Font = New-Object System.Drawing.Font("Tahoma",10,
[System.Drawing.FontStyle]::Regular)
$Label2.Font = $Label2Font
$Label2.BackColor = "white"
$Form.Controls.Add($Label2)
$BackDrop = New-Object System.Windows.Forms.Label
$BackDrop.Location = New-Object System.Drawing.Size(0,0)
$BackDrop.Size = New-Object System.Drawing.Size(550,150)
$BackDrop.BorderStyle = [System.Windows.Forms.BorderStyle]::FixedSingle;
$BackDrop.BackColor = [System.Drawing.Color]::White
$Form.Controls.Add($WaitBackGroundBox)
$Form.Topmost = $True
[void] $Form.ShowDialog() # If absent, animation is lost. If present
# (combined with form.visible = $false), script
# halts until the form itself is closed.
I found a thread with a workaround for this that creates a new runspace and opens the form within it. That way the .gif runs normally and you can issue the form.close() command whenever you'd like further down in your code. This would come in handy just in case someone wants to use a .gif for something other than a Please Wait indicator. Posted from this site:
$Form = New-Object system.Windows.Forms.Form
$Form.Location= New-Object System.Drawing.Size(100,100)
$Form.Size= New-Object System.Drawing.Size(550,170)
$Form.StartPosition = "Manual"
$Form.Visible=$false
$Form.Enabled = $true
$Form.Add_Shown({$Form.Activate()})
[reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
$file = (get-item 'C:\path_to_your_gif.gif')
$img = [System.Drawing.Image]::Fromfile($file);
[System.Windows.Forms.Application]::EnableVisualStyles();
$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Location = New-Object System.Drawing.Size(0,1)
$pictureBox.Size = New-Object System.Drawing.Size(100,80)
$pictureBox.Image = $img
$Form.controls.add($pictureBox)
$Label = New-Object System.Windows.Forms.Label
$Label.Text = "Please wait for Installation to complete"
$Label.Location= New-Object System.Drawing.Size(110,35)
$Label.Size = New-Object System.Drawing.Size(400,25)
$LabelFont = New-Object System.Drawing.Font("Tahoma",10,
[System.Drawing.FontStyle]::Bold)
$Label.Font = $PleaseWaitLabelFont
$Label.BackColor = "white"
$Form.Controls.Add($WaitLabel)
$WaitForm.Topmost = $True
$rs = [Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace()
$rs.Open()
$rs.SessionStateProxy.SetVariable("Form", $Form)
$data = [hashtable]::Synchronized(@{text=""})
$rs.SessionStateProxy.SetVariable("data", $data)
$p = $rs.CreatePipeline({ [void] $Form.ShowDialog()})
$p.Input.Close()
$p.InvokeAsync()
## Enter the rest of your script here while you want the form to display
$WaitForm.close()
$rs.close()