How to open then close a window/app/object run by powershell script.

I'm trying to use a batch file run via GPO to open a powershell script at login that will display a splash image.  Script code is here:



# .Net methods for hiding/showing the console in the background
Add-Type -Name Window -Namespace Console -MemberDefinition '
[DllImport("Kernel32.dll")]
public static extern IntPtr GetConsoleWindow();

[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
'
function Hide-Console
{
    $consolePtr = [Console.Window]::GetConsoleWindow()
    #0 hide
    [Console.Window]::ShowWindow($consolePtr, 0)
}
Hide-Console

# Loosely based on http://www.vistax64.com/powershell/202216-display-image-powershell.html

[void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")

$file = (get-item 'C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg')
#$file = (get-item "c:\image.jpg")

$img = [System.Drawing.Image]::Fromfile($file);

# This tip from http://stackoverflow.com/questions/3358372/windows-forms-look-different-in-powershell-and-powershell-ise-why/3359274#3359274
[System.Windows.Forms.Application]::EnableVisualStyles();
$form = new-object Windows.Forms.Form
$form.Text = "I'm obv not a programmer"
$form.Width = $img.Size.Width;
$form.Height =  $img.Size.Height;
$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Width =  $img.Size.Width;
$pictureBox.Height =  $img.Size.Height;

$pictureBox.Image = $img;
$form.controls.add($pictureBox)
$form.Add_Shown( { $form.Activate() } )
$form.ShowDialog()
#$form.Show();




The script creates an object then displays the image in it.    I've added some code to the beginning that will hide the powershell console window so that only the image object shows.
Users can close the window, but it would be better if I could have the image displayed and then close after 15 seconds or so.  It would be REALLY cool if the object title bar and window options (min/max/close) were not available and they'd have to endure whatever image I throw up there.  Any advice would be greatly appreciated.
orbisuserAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Like that?
$imageFile = 'C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg'
$displayTime = 15

Add-Type -AssemblyName 'System.Windows.Forms'
# .Net methods for hiding/showing the console in the background
Add-Type -Name Window -Namespace Console -MemberDefinition @'
	[DllImport("Kernel32.dll")]
	public static extern IntPtr GetConsoleWindow();
	[DllImport("user32.dll")]
	public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
'@
Function Hide-Console {
	$consolePtr = [Console.Window]::GetConsoleWindow()
	#0 hide
	[void][Console.Window]::ShowWindow($consolePtr, 0)
}
Function Show-Console {
	$consolePtr = [Console.Window]::GetConsoleWindow()
	#0 hide
	[void][Console.Window]::ShowWindow($consolePtr, 1)
}

Hide-Console

$image = [System.Drawing.Image]::Fromfile($imageFile);

$pictureBox = New-Object -TypeName Windows.Forms.PictureBox
$pictureBox.Width = $image.Size.Width
$pictureBox.Height = $image.Size.Height
$pictureBox.Image = $image;

$form = New-Object -TypeName Windows.Forms.Form
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
$form.Width = $image.Size.Width
$form.Height = $image.Size.Height
$form.ControlBox = $false
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::None
$form.MaximizeBox = $false
$form.MinimizeBox = $false
$form.ShowIcon = $false
$form.ShowInTaskbar = $false

$form.Controls.Add($pictureBox)

$timer = New-Object -TypeName System.Windows.Forms.Timer
$timer.Interval = $displayTime * 1000
$timer.add_Tick({$form.Close()})
$timer.Start()

[void][System.Windows.Forms.Application]::Run($form)
$timer.Stop()
Show-Console

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
orbisuserAuthor Commented:
Yeah, exactly like that!  Your code is much better.  Thx very much for your help.
0
orbisuserAuthor Commented:
Expert wrote code that was much better than the stuff I googled and slapped together.  TYVM.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.