Guest User

Untitled

a guest
Jan 27th, 2016
242
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $inputXML = @"
  2. <Window x:Class="Services.MainWindow"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. Title="PowerShell Services" Height="400" Width="600">
  6. <Grid Width="514">
  7. <Label x:Name="label" Content="ComputerName" HorizontalAlignment="Left" Height="46" Margin="28,55,0,0" VerticalAlignment="Top" Width="118" FontSize="12"/>
  8. <Label Content="Service" Height="33" HorizontalAlignment="Left" Margin="28,116,0,0" Name="ServicesName" VerticalAlignment="Top" Width="152" />
  9. <TextBlock Height="19" HorizontalAlignment="Left" Margin="220,104,0,0" Name="Action" Text="Action" VerticalAlignment="Top" Width="40" />
  10. <Button BorderBrush="#EE000000" Content="StartServices" Height="22" HorizontalAlignment="Left" Margin="189,129,0,0" Name="StartService" VerticalAlignment="Top" Width="103" />
  11. <Button Content="StopService" Height="22" HorizontalAlignment="Left" Margin="189,170,0,0" Name="ServicesStop" VerticalAlignment="Top" Width="103" />
  12. <Button BorderBrush="#EE000000" Content="Exit Form" Height="20" HorizontalAlignment="Left" Margin="396,116,0,0" Name="Exit" VerticalAlignment="Top" Width="53" />
  13. <ListView x:Name="listView" HorizontalAlignment="Left" Height="156" Margin="24,195,0,0" VerticalAlignment="Top" Width="511" FontSize="16">
  14. <ListView.View>
  15. <GridView>
  16. <GridViewColumn Header="Service Name" DisplayMemberBinding ="{Binding 'Service Name'}" Width="120"/>
  17. <GridViewColumn Header="Service Status" DisplayMemberBinding ="{Binding 'Service Status'}" Width="120"/>
  18. </GridView>
  19. </ListView.View>
  20. </ListView>
  21. <TextBox Height="22" HorizontalAlignment="Left" Margin="29,76,0,0" Name="Server" VerticalAlignment="Top" Width="152" />
  22. <TextBox Height="22" HorizontalAlignment="Left" Margin="28,154,0,0" Name="Service" VerticalAlignment="Top" Width="152" />
  23. <Button BorderBrush="#EE000000" Content="Get Services" Height="22" HorizontalAlignment="Left" Margin="187,76,0,0" Name="GetServices" VerticalAlignment="Top" Width="103" />
  24. </Grid>
  25. </Window>
  26. "@
  27.  
  28. $inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window'
  29. [void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
  30. [xml]$XAML = $inputXML
  31. #Read XAML
  32.  
  33. $reader=(New-Object System.Xml.XmlNodeReader $xaml)
  34. try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
  35. catch [System.Management.Automation.MethodInvocationException] {
  36. Write-Warning "We ran into a problem with the XAML code. Check the syntax for this control..."
  37. write-host $error[0].Exception.Message -ForegroundColor Red
  38. if ($error[0].Exception.Message -like "*button*"){
  39. write-warning "Ensure your &lt;button in the `$inputXML does NOT have a Click=ButtonClick property. PS can't handle this`n`n`n`n"}
  40. }
  41. catch{#if it broke some other way <span class="wp-smiley wp-emoji wp-emoji-bigsmile" title=":D">:D</span>
  42. Write-Host "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .net is installed."
  43. }
  44.  
  45. #===========================================================================
  46. # Store Form Objects In PowerShell
  47. #===========================================================================
  48.  
  49. $xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name)}
  50.  
  51. Function Get-FormVariables{
  52. if ($global:ReadmeDisplay -ne $true){Write-host "If you need to reference this display again, run Get-FormVariables" -ForegroundColor Yellow;$global:ReadmeDisplay=$true}
  53. write-host "Found the following interactable elements from our form" -ForegroundColor Cyan
  54. get-variable WPF*
  55. }
  56.  
  57. Get-FormVariables
  58. $form.FindName("Exit").add_click({
  59. $form.Close()
  60. })
  61.  
  62. Function Get-Services {
  63. param($computername)
  64. Get-Service -ComputerName $computername | Select-Object @{Name='ComputerName';Ex={$computername}},`
  65. @{Name=‘Service Name‘;Expression={$_.Name}},`
  66. @{Name=‘Service Status’;Expression={$_.Status}}
  67. }
  68.  
  69. $WPFGetServices.Add_Click({
  70. $WPFlistView.Items.Clear()
  71. start-sleep -Milliseconds 840
  72. Get-Services -computername $WPFServer.Text | % {$WPFlistView.AddChild($_)}
  73. })
  74.  
  75.  
  76. Function StartServices {
  77. $WPFStartService.Add_Click({
  78. Get-Service -Name $WPFService.Text -computername $WPFServer.Text | Set-Service -Status Running
  79. })
  80. }
  81.  
  82. $Form.ShowDialog() | out-null
RAW Paste Data