Skip to content

Instantly share code, notes, and snippets.

@igeta
Created September 26, 2017 10:23
Show Gist options
  • Save igeta/0e315f86a9f46b730dc571dcb35a4976 to your computer and use it in GitHub Desktop.
Save igeta/0e315f86a9f46b730dc571dcb35a4976 to your computer and use it in GitHub Desktop.
Hello world script in PowerShell. Should save with UTF-16 BE encoding.
param(
[ValidateSet("CUI", "GUI")]
[string]$RunningStyle,
[string]$Text
)
### bootstrap
if (-not $RunningStyle) {
if ((Get-Host).Name -eq "ConsoleHost") {
Write-Host "Loading..."
$cmd = '& "{0}" -RunningStyle GUI' -f (&{ $MyInvocation.ScriptName })
$arg = '-NoLogo -NoProfile -NonInteractive -WindowStyle Hidden -Command ' + $cmd
Start-Process powershell -WindowStyle Minimized -ArgumentList $arg
Start-Sleep -Seconds 2
return
}
$RunningStyle = "GUI"
}
function Start-Main {
[OutputType([Void])]
[CmdletBinding()]
param()
switch ($RunningStyle) {
"CUI" { Start-Cui }
"GUI" { Start-Gui }
Default { throw [NotSupportedException] }
}
}
function Start-Cui {
[OutputType([Void])]
[CmdletBinding()]
param()
Write-Host $Text
}
function Start-Gui {
[OutputType([Void])]
[CmdletBinding()]
param()
try { Add-Type -AssemblyName PresentationCore,PresentationFramework,WindowsBase,System.Windows.Forms }
catch { throw "Failed to load Windows Presentation Framework assemblies." }
$xaml = [xml] @'
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Hello Real World!" Height="180" Width="600">
<Window.Resources>
<Style x:Key="BaseStyle" TargetType="Control">
<Setter Property="FontSize" Value="10.5pt" />
</Style>
<Style x:Key="SimpleStyle" TargetType="Control" BasedOn="{StaticResource BaseStyle}">
<Setter Property="Margin" Value="2" />
</Style>
<Style TargetType="Label" BasedOn="{StaticResource SimpleStyle}" />
<Style TargetType="TextBox" BasedOn="{StaticResource SimpleStyle}" />
<Style TargetType="Button" BasedOn="{StaticResource SimpleStyle}" />
</Window.Resources>
<Grid Margin="8,4,8,16">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Content="Text:" Target="{Binding inputText}" Grid.Row="0" Grid.Column="0" />
<TextBox x:Name="inputText" Grid.Row="0" Grid.Column="1" />
<Grid HorizontalAlignment="Right" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2">
<StackPanel Orientation="Horizontal">
<Button x:Name="okButton" Content="Run" IsDefault="True" Width="100" />
<Button x:Name="cancelButton" Content="Exit" IsCancel="True" Width="100" />
</StackPanel>
</Grid>
</Grid>
</Window>
'@
$global:window = [Windows.Markup.XamlReader]::Load((New-Object Xml.XmlNodeReader $xaml))
$nsmgr = New-Object Xml.XmlNamespaceManager $xaml.NameTable
$nsmgr.AddNamespace("x", "http://schemas.microsoft.com/winfx/2006/xaml")
$xaml.SelectNodes("//*[@x:Name]", $nsmgr) | ForEach-Object {
Set-Variable -Scope Global -Name $_.Name -Value $window.FindName($_.Name)
}
Initialize-Component
$window.ShowDialog() > $null
}
function Initialize-Component {
[OutputType([Void])]
[CmdletBinding()]
param()
$window.add_Loaded({
$inputText.Text = $Text
})
$okButton.add_Click({
[Windows.MessageBox]::Show(
$window, $inputText.Text, "Result"
) > $null
})
}
Start-Main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment