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?
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.