Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - The alert window in PowerShell

Reply
 
Old 07-18-2009   #1 (permalink)
Jim Wang


 
 

The alert window in PowerShell

Hi,

I know you can use:
Write-Host "Hello World"

command to echo information to the user, however the information shows in
the PowerShell window(CMD window).

How can PowerShell alert user in a Window form?

My second question is: how to run a .ps1 file without show up the powershell
window?

Cheers,
Jim


--
Jim Wang
MVP - Dynamics CRM

My System SpecsSystem Spec
Old 07-18-2009   #2 (permalink)
Vadims Podans [MVP]


 
 

Re: The alert window in PowerShell

1) you can use Windows.Forms class to display message boxes:
function msgbox ($title, $text, $type = "None") {
[void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
$msg = [Windows.Forms.MessageBox]::Show($text, $title,
[Windows.Forms.MessageBoxButtons]:k,
[Windows.Forms.MessageBoxIcon]::$type)
}

and usage:
msgbox "This is my titile" "some text in message box" "Information"

2) if you have PowerShell 1.0 version then you should place this code at a
top of script code after Param() section:
$cp = new-object Microsoft.CSharp.CSharpCodeProvider
$cpar = New-Object System.CodeDom.Compiler.CompilerParameters
$HideWindow = 0x0080
$ShowWindow = 0x0040
$Code = @"
using System;
using System.Runtime.InteropServices;
namespace Win32API
{
public class Window
{
[DllImport("user32.dll")]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr
hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
}
}
"@
$cp.CompileAssemblyFromSource($cpar, $code)
$PSHandle = (Get-Process –id $pid).MainWindowHandle
[Win32API.Window]::SetWindowPos($PSHandle, 0, 0, 0, 0, 0, $HideWindow)

this will hide PS console.

if you have PowerShell V2 then you can use '-WindowStyle Hidden' option when
you call your script:

powershell.exe -WindowStyle Hidden script.ps1
--
WBR, Vadims Podans
MVP: PowerShell
PowerShell blog - www.sysadmins.lv

"Jim Wang" <JimWang@xxxxxx> rakstīja ziņojumā
"news:101238AB-5A4E-4D4B-9B77-A11152F99296@xxxxxx"...
Quote:

> Hi,
>
> I know you can use:
> Write-Host "Hello World"
>
> command to echo information to the user, however the information shows in
> the PowerShell window(CMD window).
>
> How can PowerShell alert user in a Window form?
>
> My second question is: how to run a .ps1 file without show up the
> powershell
> window?
>
> Cheers,
> Jim
>
>
> --
> Jim Wang
> MVP - Dynamics CRM
My System SpecsSystem Spec
Old 07-18-2009   #3 (permalink)
tojo2000


 
 

Re: The alert window in PowerShell

On Jul 18, 9:13*am, Jim Wang <JimW...@xxxxxx>
wrote:
Quote:

> Hi,
>
> I know you can use:
> Write-Host "Hello World"
>
> command to echo information to the user, however the information shows in
> the PowerShell window(CMD window).
>
> How can PowerShell alert user in a Window form?
>
> My second question is: how to run a .ps1 file without show up the powershell
> window?
>
> Cheers,
> Jim
>
> --
> Jim Wang
> MVP - Dynamics CRM
I would really recommend checking out PrimalForms. You can find the
free version here: You can use a visual editor to design your form
and it will generate the code. Even if you don't end up using it
forever, it generates some great boilerplate code that really helps
you understand the options for generating forms in PowerShell.
My System SpecsSystem Spec
Old 07-18-2009   #4 (permalink)
Joel Bennett


 
 

Re: The alert window in PowerShell

And *I* would really recommend checking out PowerBoots, in PowerBoots, a
script to just display a popup message would just be like this:

Boots { Textblock "HELLO WORLD" -fontsize 48 }

And you could even do it -Async so that your script can continue and
just leave that message there for the user to discover and read later.

As for hiding the window, Vadim's script works ... you should check out
the tricks script on PoshCode: http://poshcode.org/search/HideWindow

It's basically a slightly more feature-full version of what Vadims
posted already, but it keeps track of the windows it hides, so you can
easily unhide them again later (and a few other things too). Of course,
it's a lot easier to just use PowerShell 2, if you can...
--
Joel


tojo2000 wrote:
Quote:

> On Jul 18, 9:13 am, Jim Wang<JimW...@xxxxxx>
> wrote:
Quote:

>> Hi,
>>
>> I know you can use:
>> Write-Host "Hello World"
>>
>> command to echo information to the user, however the information shows in
>> the PowerShell window(CMD window).
>>
>> How can PowerShell alert user in a Window form?
>>
>> My second question is: how to run a .ps1 file without show up the powershell
>> window?
>>
>> Cheers,
>> Jim
>>
>> --
>> Jim Wang
>> MVP - Dynamics CRM
>
> I would really recommend checking out PrimalForms. You can find the
> free version here: You can use a visual editor to design your form
> and it will generate the code. Even if you don't end up using it
> forever, it generates some great boilerplate code that really helps
> you understand the options for generating forms in PowerShell.
My System SpecsSystem Spec
Old 07-18-2009   #5 (permalink)
Marco Shaw [MVP]


 
 

Re: The alert window in PowerShell

> My second question is: how to run a .ps1 file without show up the
Quote:

> powershell
> window?
With PowerShell v2:

DOS>powershell -windowstyle hidden

Of course, you want the above to run a .ps1 as well...

Marco

My System SpecsSystem Spec
Old 07-19-2009   #6 (permalink)
Jim Wang


 
 

Re: The alert window in PowerShell

Thanks guys! that helps!
--
Jim Wang
MVP - Dynamics CRM


"Marco Shaw [MVP]" wrote:
Quote:
Quote:

> > My second question is: how to run a .ps1 file without show up the
> > powershell
> > window?
>
> With PowerShell v2:
>
> DOS>powershell -windowstyle hidden
>
> Of course, you want the above to run a .ps1 as well...
>
> Marco
>
>
My System SpecsSystem Spec
Old 07-19-2009   #7 (permalink)
Flowering Weeds


 
 

Re: The alert window in PowerShell

Quote:

>
> How can PowerShell alert user in a Window form?
>
Here using Windows PowerShell 2 programming
to demo some PowerShell BareFootin' WPF actions!

# Add some needed assemblies.
Add-Type –assemblyName PresentationFramework
Add-Type –assemblyName PresentationCore
Add-Type –assemblyName WindowsBase

" "
"Mmm perhaps one made a mistake?"

# Use a Here string - instead of defining
# all these WPF objects in Powershell.
$xamlString = @"
<Window
xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
Title="PowerShell BareFootin' WPF" SizeToContent="WidthAndHeight"
WindowStyle="ToolWindow" ResizeMode="NoResize"
Quote:

>
<Label Name="alert" Padding="30" Foreground="Green" FontSize="35"
Content="Alert! Alert! Alert!" >
<Label.Background>
<LinearGradientBrush>
<GradientStop Color="Red" Offset="0" />
<GradientStop Color="White" Offset="0.5" />
<GradientStop Color="Red" Offset="1" />
</LinearGradientBrush>
</Label.Background>
<Label.Triggers>
<EventTrigger RoutedEvent="Label.Loaded">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard TargetProperty="Background.GradientStops[1].Color">
<ColorAnimation From="White" To="Black" Duration="0:0:4"
AutoReverse="True" />
</Storyboard>
</BeginStoryboard>
<BeginStoryboard>
<Storyboard TargetProperty="FontSize">
<DoubleAnimation To="52" Duration="0:0:4" AutoReverse="True"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Label.Triggers>
</Label>
</Window>
"@

[xml]$xaml = $xamlString
$control = [Windows.Markup.XamlReader]::Load( (New-Object
System.Xml.XmlNodeReader $xaml) )

$null = $control.ShowDialog()

" "
"Done!"
" "
Exit

Remember any PowerShell 2 user
can help one
for PowerShell 2 = WPF usage!

As always enjoy the automation of tools
within the Windows-based, .NET aware,
WPF accessible, multi-processes on the
same IP / Port usage, admin's automation tool,
powershell.exe!


My System SpecsSystem Spec
Old 07-21-2009   #8 (permalink)
Hans Dingemans


 
 

Re: The alert window in PowerShell

Exception calling "Load" with "1" argument(s): "Cannot create instance of
'Window' defined in assembly 'PresentationFramework, Version=3.0.0.0,
ulture=neutral, PublicKeyToken=31bf3856ad364e35'. The calling thread must be
STA, because many UI components require this. "
At line:1 char:45
+ $control = [Windows.Markup.XamlReader]::Load <<<< ( (New-Object
System.Xml.XmlNodeReader $xaml) )
+ CategoryInfo : NotSpecified: ( [],
MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException


Note that you need to run the code below in a single-threaded apartment. In
PowerShellPlus you can hold the Shift key during startup and check the STA
button, normal PowerShell.exe (at least V2) has the -STA commandline switch.

Is there a way to specify this STA requirement while PowerShell has already
been started?

thanx,
~Hans


"Flowering Weeds" <no@xxxxxx> wrote in message
news:On5%23X6ICKHA.3732@xxxxxx
Quote:

>
Quote:

>>
>> How can PowerShell alert user in a Window form?
>>
>
> Here using Windows PowerShell 2 programming
> to demo some PowerShell BareFootin' WPF actions!
>
> # Add some needed assemblies.
> Add-Type –assemblyName PresentationFramework
> Add-Type –assemblyName PresentationCore
> Add-Type –assemblyName WindowsBase
>
> " "
> "Mmm perhaps one made a mistake?"
>
> # Use a Here string - instead of defining
> # all these WPF objects in Powershell.
> $xamlString = @"
> <Window
> xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
> xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
> Title="PowerShell BareFootin' WPF" SizeToContent="WidthAndHeight"
> WindowStyle="ToolWindow" ResizeMode="NoResize"
Quote:

>>
> <Label Name="alert" Padding="30" Foreground="Green" FontSize="35"
> Content="Alert! Alert! Alert!" >
> <Label.Background>
> <LinearGradientBrush>
> <GradientStop Color="Red" Offset="0" />
> <GradientStop Color="White" Offset="0.5" />
> <GradientStop Color="Red" Offset="1" />
> </LinearGradientBrush>
> </Label.Background>
> <Label.Triggers>
> <EventTrigger RoutedEvent="Label.Loaded">
> <EventTrigger.Actions>
> <BeginStoryboard>
> <Storyboard TargetProperty="Background.GradientStops[1].Color">
> <ColorAnimation From="White" To="Black" Duration="0:0:4"
> AutoReverse="True" />
> </Storyboard>
> </BeginStoryboard>
> <BeginStoryboard>
> <Storyboard TargetProperty="FontSize">
> <DoubleAnimation To="52" Duration="0:0:4"
> AutoReverse="True"/>
> </Storyboard>
> </BeginStoryboard>
> </EventTrigger.Actions>
> </EventTrigger>
> </Label.Triggers>
> </Label>
> </Window>
> "@
>
> [xml]$xaml = $xamlString
> $control = [Windows.Markup.XamlReader]::Load( (New-Object
> System.Xml.XmlNodeReader $xaml) )
>
> $null = $control.ShowDialog()
>
> " "
> "Done!"
> " "
> Exit
>
> Remember any PowerShell 2 user
> can help one
> for PowerShell 2 = WPF usage!
>
> As always enjoy the automation of tools
> within the Windows-based, .NET aware,
> WPF accessible, multi-processes on the
> same IP / Port usage, admin's automation tool,
> powershell.exe!
>
>
My System SpecsSystem Spec
Old 07-21-2009   #9 (permalink)
Hans Dingemans


 
 

To run WPF, run PowerShell in STA mode

I'll answer this myself ...

In order to script WPF, you have to do one of three things:

· Run the Script in Graphical PowerShell, where WPF scripts will run
without any changes because Graphical PowerShell runspaces are STA.
· Run the script in PowerShell.exe and add the –STA script
· Create a background runspace that is STA, and run the script in
the background runspace

Got this via:
http://blogs.msdn.com/powershell/arc...ek-of-wpf.aspx



"Hans Dingemans" <hansdingemans@xxxxxx> wrote in message
news:euS6jZdCKHA.4004@xxxxxx
Quote:

> Exception calling "Load" with "1" argument(s): "Cannot create instance of
> 'Window' defined in assembly 'PresentationFramework, Version=3.0.0.0,
> ulture=neutral, PublicKeyToken=31bf3856ad364e35'. The calling thread must
> be STA, because many UI components require this. "
> At line:1 char:45
> + $control = [Windows.Markup.XamlReader]::Load <<<< ( (New-Object
> System.Xml.XmlNodeReader $xaml) )
> + CategoryInfo : NotSpecified: ( [],
> MethodInvocationException
> + FullyQualifiedErrorId : DotNetMethodException
>
>
> Note that you need to run the code below in a single-threaded apartment.
> In PowerShellPlus you can hold the Shift key during startup and check the
> STA button, normal PowerShell.exe (at least V2) has the -STA commandline
> switch.
>
> Is there a way to specify this STA requirement while PowerShell has
> already been started?
>
> thanx,
> ~Hans
>
>
> "Flowering Weeds" <no@xxxxxx> wrote in message
> news:On5%23X6ICKHA.3732@xxxxxx
Quote:

>>
Quote:

>>>
>>> How can PowerShell alert user in a Window form?
>>>
>>
>> Here using Windows PowerShell 2 programming
>> to demo some PowerShell BareFootin' WPF actions!
>>
>> # Add some needed assemblies.
>> Add-Type –assemblyName PresentationFramework
>> Add-Type –assemblyName PresentationCore
>> Add-Type –assemblyName WindowsBase
>>
>> " "
>> "Mmm perhaps one made a mistake?"
>>
>> # Use a Here string - instead of defining
>> # all these WPF objects in Powershell.
>> $xamlString = @"
>> <Window
>> xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
>> xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
>> Title="PowerShell BareFootin' WPF" SizeToContent="WidthAndHeight"
>> WindowStyle="ToolWindow" ResizeMode="NoResize"
Quote:

>>>
>> <Label Name="alert" Padding="30" Foreground="Green" FontSize="35"
>> Content="Alert! Alert! Alert!" >
>> <Label.Background>
>> <LinearGradientBrush>
>> <GradientStop Color="Red" Offset="0" />
>> <GradientStop Color="White" Offset="0.5" />
>> <GradientStop Color="Red" Offset="1" />
>> </LinearGradientBrush>
>> </Label.Background>
>> <Label.Triggers>
>> <EventTrigger RoutedEvent="Label.Loaded">
>> <EventTrigger.Actions>
>> <BeginStoryboard>
>> <Storyboard
>> TargetProperty="Background.GradientStops[1].Color">
>> <ColorAnimation From="White" To="Black" Duration="0:0:4"
>> AutoReverse="True" />
>> </Storyboard>
>> </BeginStoryboard>
>> <BeginStoryboard>
>> <Storyboard TargetProperty="FontSize">
>> <DoubleAnimation To="52" Duration="0:0:4"
>> AutoReverse="True"/>
>> </Storyboard>
>> </BeginStoryboard>
>> </EventTrigger.Actions>
>> </EventTrigger>
>> </Label.Triggers>
>> </Label>
>> </Window>
>> "@
>>
>> [xml]$xaml = $xamlString
>> $control = [Windows.Markup.XamlReader]::Load( (New-Object
>> System.Xml.XmlNodeReader $xaml) )
>>
>> $null = $control.ShowDialog()
>>
>> " "
>> "Done!"
>> " "
>> Exit
>>
>> Remember any PowerShell 2 user
>> can help one
>> for PowerShell 2 = WPF usage!
>>
>> As always enjoy the automation of tools
>> within the Windows-based, .NET aware,
>> WPF accessible, multi-processes on the
>> same IP / Port usage, admin's automation tool,
>> powershell.exe!
>>
>>
>
My System SpecsSystem Spec
Old 07-21-2009   #10 (permalink)
Joel Bennett


 
 

Re: To run WPF, run PowerShell in STA mode

OR ... use PowerBoots. http://boots.codeplex.com
It doesn't need PowerShell to be in STA mode.
--
Joel


Hans Dingemans wrote:
Quote:

> I'll answer this myself ...
>
> In order to script WPF, you have to do one of three things:
>
> · Run the Script in Graphical PowerShell, where WPF scripts will run
> without any changes because Graphical PowerShell runspaces are STA.
> · Run the script in PowerShell.exe and add the –STA script
> · Create a background runspace that is STA, and run the script in the
> background runspace
>
> Got this via:
> http://blogs.msdn.com/powershell/arc...ek-of-wpf.aspx
>
>
>
>
> "Hans Dingemans" <hansdingemans@xxxxxx> wrote in message
> news:euS6jZdCKHA.4004@xxxxxx
Quote:

>> Exception calling "Load" with "1" argument(s): "Cannot create instance
>> of 'Window' defined in assembly 'PresentationFramework,
>> Version=3.0.0.0, ulture=neutral, PublicKeyToken=31bf3856ad364e35'. The
>> calling thread must be STA, because many UI components require this. "
>> At line:1 char:45
>> + $control = [Windows.Markup.XamlReader]::Load <<<< ( (New-Object
>> System.Xml.XmlNodeReader $xaml) )
>> + CategoryInfo : NotSpecified: ( [], MethodInvocationException
>> + FullyQualifiedErrorId : DotNetMethodException
>>
>>
>> Note that you need to run the code below in a single-threaded
>> apartment. In PowerShellPlus you can hold the Shift key during startup
>> and check the STA button, normal PowerShell.exe (at least V2) has the
>> -STA commandline switch.
>>
>> Is there a way to specify this STA requirement while PowerShell has
>> already been started?
>>
>> thanx,
>> ~Hans
>>
>>
>> "Flowering Weeds" <no@xxxxxx> wrote in message
>> news:On5%23X6ICKHA.3732@xxxxxx
Quote:

>>>
>>>>
>>>> How can PowerShell alert user in a Window form?
>>>>
>>>
>>> Here using Windows PowerShell 2 programming
>>> to demo some PowerShell BareFootin' WPF actions!
>>>
>>> # Add some needed assemblies.
>>> Add-Type –assemblyName PresentationFramework
>>> Add-Type –assemblyName PresentationCore
>>> Add-Type –assemblyName WindowsBase
>>>
>>> " "
>>> "Mmm perhaps one made a mistake?"
>>>
>>> # Use a Here string - instead of defining
>>> # all these WPF objects in Powershell.
>>> $xamlString = @"
>>> <Window
>>> xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
>>> xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
>>> Title="PowerShell BareFootin' WPF" SizeToContent="WidthAndHeight"
>>> WindowStyle="ToolWindow" ResizeMode="NoResize"
>>>>
>>> <Label Name="alert" Padding="30" Foreground="Green" FontSize="35"
>>> Content="Alert! Alert! Alert!" >
>>> <Label.Background>
>>> <LinearGradientBrush>
>>> <GradientStop Color="Red" Offset="0" />
>>> <GradientStop Color="White" Offset="0.5" />
>>> <GradientStop Color="Red" Offset="1" />
>>> </LinearGradientBrush>
>>> </Label.Background>
>>> <Label.Triggers>
>>> <EventTrigger RoutedEvent="Label.Loaded">
>>> <EventTrigger.Actions>
>>> <BeginStoryboard>
>>> <Storyboard TargetProperty="Background.GradientStops[1].Color">
>>> <ColorAnimation From="White" To="Black" Duration="0:0:4"
>>> AutoReverse="True" />
>>> </Storyboard>
>>> </BeginStoryboard>
>>> <BeginStoryboard>
>>> <Storyboard TargetProperty="FontSize">
>>> <DoubleAnimation To="52" Duration="0:0:4" AutoReverse="True"/>
>>> </Storyboard>
>>> </BeginStoryboard>
>>> </EventTrigger.Actions>
>>> </EventTrigger>
>>> </Label.Triggers>
>>> </Label>
>>> </Window>
>>> "@
>>>
>>> [xml]$xaml = $xamlString
>>> $control = [Windows.Markup.XamlReader]::Load( (New-Object
>>> System.Xml.XmlNodeReader $xaml) )
>>>
>>> $null = $control.ShowDialog()
>>>
>>> " "
>>> "Done!"
>>> " "
>>> Exit
>>>
>>> Remember any PowerShell 2 user
>>> can help one
>>> for PowerShell 2 = WPF usage!
>>>
>>> As always enjoy the automation of tools
>>> within the Windows-based, .NET aware,
>>> WPF accessible, multi-processes on the
>>> same IP / Port usage, admin's automation tool,
>>> powershell.exe!
>>>
>>>
>>
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Alert Window Live Mail
Alert Window on top of all other windows - like Task Manager VB Script
Is there a new-mail alert window? Live Mail
Can alert window be kept open longer or repeat Live Messenger
chat window alert? Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46