Link to home
Start Free TrialLog in
Avatar of orbisuser
orbisuser

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of orbisuser
orbisuser

ASKER

Yeah, exactly like that!  Your code is much better.  Thx very much for your help.
Expert wrote code that was much better than the stuff I googled and slapped together.  TYVM.