Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. #References
  2. #https://blogs.technet.microsoft.com/platformspfe/2014/01/20/integrating-xaml-into-powershell/
  3. #https://smsagent.blog/2017/08/24/a-customisable-wpf-messagebox-for-powershell/
  4. #https://stackoverflow.com/questions/38663029/altering-datagrid-with-textbox-powershell
  5.  
  6. # XAML Code - Imported from Visual Studio Community 2017 WPF Application
  7. [void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
  8. [xml]$XAML = @'
  9. <Window Name="Form"
  10. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  11. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  12. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  13. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  14. Title="Services" Height="488.773" Width="797.65" ShowInTaskbar="False">
  15. <Grid Margin="0,0,-8,-21">
  16. <DataGrid Name="DataGrid1" HorizontalAlignment="Left" Height="368" VerticalAlignment="Top" Width="772" Margin="10,41,0,0">
  17.  
  18.  
  19. </DataGrid>
  20.  
  21. <Label Content="Filter" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
  22. <TextBox Name="FilterTextBox" HorizontalAlignment="Left" Height="26" Margin="78,10,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="172"/>
  23. <Button Name="CloseButton" Content="Do Stuff" HorizontalAlignment="Left" Margin="703,414,0,0" VerticalAlignment="Top" Background="Transparent" BorderBrush="Transparent" FontWeight="Bold" FontFamily="Segoe UI Semibold" Width="75" Height="25" FontSize="16"/>
  24. </Grid>
  25. </Window>
  26. '@
  27.  
  28. #Read XAML
  29. $reader=(New-Object System.Xml.XmlNodeReader $xaml)
  30. try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
  31. catch{Write-Host "Unable to load Windows.Markup.XamlReader. Some possible causes for this problem include: .NET Framework is missing PowerShell must be launched with PowerShell -sta, invalid XAML code was encountered."; exit}
  32.  
  33. # Store Form Objects In PowerShell
  34. $xaml.SelectNodes("//*[@Name]") | ForEach-Object{
  35. Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name)
  36. Write-host $_.Name
  37. }
  38.  
  39. $Fields = @(
  40. 'Status'
  41. 'DisplayName'
  42. 'ServiceName'
  43. )
  44.  
  45. $Services = Get-Service | Select-object $Fields
  46.  
  47. # Add Services to a datatable
  48. $Datatable = New-Object System.Data.DataTable
  49. [void]$Datatable.Columns.AddRange($Fields)
  50. foreach ($Service in $Services)
  51. {
  52. $Array = @()
  53. Foreach ($Field in $Fields)
  54. {
  55. $array += $Service.$Field
  56. }
  57. [void]$Datatable.Rows.Add($array)
  58. }
  59. #$filter = "DisplayName LIKE 'B%'"
  60. #$Datatable.DefaultView.RowFilter = $filter
  61.  
  62. # Create a datagrid object and populate with datatable
  63. $DataGrid1.ItemsSource = $Datatable.DefaultView
  64. $DataGrid1.CanUserAddRows = $False
  65. $DataGrid1.IsReadOnly = $True
  66. $DataGrid1.GridLinesVisibility = "None"
  67. $DataGrid1.SelectionUnit = "FullRow"
  68.  
  69. $CloseButton.Add_Click({Do-Stuff})
  70.  
  71. function Do-Stuff {
  72.  
  73. $test = $DataGrid1.SelectedCells[0]
  74. write-host $test.Column[0].GetCellContent($test.Item).Text
  75.  
  76.  
  77. }
  78.  
  79.  
  80.  
  81.  
  82. $FilterTextBox.Add_TextChanged({
  83. $InputText = $FilterTextBox.Text
  84. $filter = "DisplayName LIKE '$InputText%'"
  85. $Datatable.DefaultView.RowFilter = $filter
  86. $DataGrid1.ItemsSource = $Datatable.DefaultView
  87. })
  88.  
  89. # Shows the form
  90. $Form.ShowDialog() | out-null
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement