Sunday 20 January 2013

Powershell GUI



Basic template for when I want to make a Powershell XAML GUI.


    <#
        .SYNOPSIS
        .DESCRIPTION
        .PARAMETER
        .EXAMPLE
        .NOTES
            FunctionName :
            by   : administrator
            Date Coded   : 03/09/2012 20:32:56
        .LINK
    #>

$RunSpace=[RunspaceFactory]::CreateRunspace()
$RunSpace.ApartmentState = "STA"
$RunSpace.ThreadOptions = "ReuseThread"
$RunSpace.Open()
$PowerShellRunSpace = [PowerShell]::Create()
$PowerShellRunSpace.Runspace = $RunSpace
$PowerShellRunSpace.Runspace.SessionStateProxy.SetVariable("pwd",$pwd)
$handle = $PowerShellRunSpace.AddScript({

    Add-Type -AssemblyName Presentationframework
    [xml]$XAML = @'
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
      
    </Grid>
</Window>

'@
    $reader=(New-Object System.Xml.XmlNodeReader $xaml)
    $Form=[Windows.Markup.XamlReader]::Load( $reader )

    #******
    #Interface declarations
    #******
    $Button1 = $Form.FindName("Button1")

    #******
    #Declare variables
    #******
    $Invocation = (Get-Variable MyInvocation -Scope 0).Value
    $ScriptPath = Split-Path $Invocation.MyCommand.Path
    $ScriptRoot = Split-Path $ScriptPath

    #******
    #Functions
    #******

    Function My-Function {
    }

    #******
    #On startup events
    #******


    #******
    #On Click events
    #******

    $Button1.add_Click({
    }) #end $Button1.add_Click


    #Show the form
    $Form.ShowDialog() | out-null

#End the runspace
}).BeginInvoke()

No comments:

Post a Comment