Thursday, September 11, 2014

Pop up window with PowerShell

PowerShell popup with use of xaml for formatting the output. Enjoy! I know I did, I used this with a gpo login script to help remind users what to do...

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
## creates the runspace
$run = [RunspaceFactory]::CreateRunspace()
$run.ApartmentState = “STA”
$run.Open()
## opens PowerShell.exe
$ps = {Add-Type -AssemblyName PresentationCore}.GetPowerShell()
$ps.Runspace = $run
## specify xaml formatting
$ps.AddScript({
    [xml]$xaml = @"
    <Window
        xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
        xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
        x:Name='Window' WindowStartupLocation = 'CenterScreen' Width = '1000' Height = '500' ShowInTaskbar = 'True' WindowStyle = 'None' AllowsTransparency = 'true'>
        <Window.Background>
            <SolidColorBrush Opacity= '0' ></SolidColorBrush>
        </Window.Background>
        <Grid x:Name = 'Grid' ShowGridLines='false' >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height = '*'/>
            </Grid.RowDefinitions>
            <Viewbox Stretch = 'Fill'>
                <Label x:Name='Content' FontWeight = 'Bold' Content = 'PowerShell Rules!' FontSize = '30' FontStyle = 'Normal' Foreground = 'Blue' />
            </Viewbox>
        </Grid>
    </Window>
"@

    ## read xaml code
    $reader=(New-Object System.Xml.XmlNodeReader $xaml)
    $Global:popup=[Windows.Markup.XamlReader]::Load( $reader )
    ## sets right click to "Close Window"
    $popup.Add_MouseRightButtonUp({$this.close()})
    ## sets left click to "Drag\move Window"
    $popup.Add_MouseLeftButtonDown({$This.DragMove()})
    ## Always on top of all other windows
    $popup.Topmost = $True
    $popup.ShowDialog()
}).BeginInvoke() | Out-Null


No comments:

Post a Comment