Skip to content

Instantly share code, notes, and snippets.

@Sam-Martin
Last active April 28, 2016 08:11
Show Gist options
  • Save Sam-Martin/f532ab67864a0ae82f7aa07d5c6c5b85 to your computer and use it in GitHub Desktop.
Save Sam-Martin/f532ab67864a0ae82f7aa07d5c6c5b85 to your computer and use it in GitHub Desktop.
Get-Service WPF PowerShell Get-PSUGUK 2016.04.27
Add-Type -AssemblyName PresentationFramework
[xml]$XAMLWindow = '
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="Auto"
SizeToContent="WidthAndHeight"
Title="Get-Service">
<ScrollViewer Padding="20">
<StackPanel>
<StackPanel Orientation="Horizontal">
<Label>ComputerName</Label>
<TextBox Name="Input" Width="250px"></TextBox>
</StackPanel>
<DockPanel>
<Button Name="ButtonGetService" Content="Get-Service" Width="150px" IsEnabled="false"/>
<Button Name="ButtonClose" Content="Close" HorizontalAlignment="Right" Width="50px"/>
</DockPanel>
</StackPanel>
</ScrollViewer >
</Window>
'
# Create the Window Object
$Reader=(New-Object System.Xml.XmlNodeReader $XAMLWindow)
$Window=[Windows.Markup.XamlReader]::Load( $Reader )
# TextChanged Event Handler for Input
$TextboxInput = $Window.FindName("Input")
$TextboxInput.add_TextChanged.Invoke({
$ComputerName = $TextboxInput.Text
$ButtonGetService.IsEnabled = $ComputerName -ne ''
})
# Click Event Handler for ButtonClose
$ButtonClose = $Window.FindName("ButtonClose")
$ButtonClose.add_Click.Invoke({
$Window.Close();
})
# Click Event Handler for ButtonGetService
$ButtonGetService = $Window.FindName("ButtonGetService")
$ButtonGetService.add_Click.Invoke({
$ComputerName = $TextboxInput.text.Trim()
try{
Get-Service -ComputerName $computerName | Out-GridView -Title "Get-Service on $ComputerName"
}catch{
[System.Windows.MessageBox]::Show($_.exception.message,"Error",[System.Windows.MessageBoxButton]::OK,[System.Windows.MessageBoxImage]::Error)
}
})
# Open the Window
$Window.ShowDialog() | Out-Null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment