-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathposh-admin-gui.ps1
131 lines (102 loc) · 4.27 KB
/
posh-admin-gui.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#Powershell Admin GUI
#Creator: Sven Gerber
# -------------------- Adding Prerequisites
Add-Type -AssemblyName System.Drawing, PresentationFramework, System.Windows.Forms, WindowsFormsIntegration
# -------------------- Global variables
$searchcomboxname = "searchcombox"
$searchcombox = $Form.$searchcomboxname
$pathfunctionuis = "$PSScriptRoot\functions\*\ui.xaml"
$pathguixmlraw = "$PSScriptRoot\data\gui\gui.xaml"
$pathstylexml = "$PSScriptRoot\data\gui\style.xaml"
#-------------------- Functions
function Add-FunctionsToGUI($GUI)
{
$uis = Get-ChildItem -Path $pathfunctionuis
foreach ($ui in $uis)
{
$add = Get-Content -Path $ui.FullName
$alluis = $alluis + $add
}
$GUI = $GUI -replace "<!-- INSERT_FUNCTIONS_PLACEHOLDER -->", $alluis
return $GUI
}
#-------------------- Preparing GUI
#Generating XML to render
$inputXMLraw = Get-Content -Path $pathguixmlraw
$inputXML = $inputXMLraw -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window'
$inputXML = Add-FunctionsToGUI -GUI $inputXML
#Adding style to XML
$style = Get-Content -Path $pathstylexml
$inputXML = $inputXML -replace "<!-- INSERT_STYLE_PLACEHOLDER -->", $style
#Creating form with xml
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = $inputXML
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
try{
$Form=[Windows.Markup.XamlReader]::Load( $reader )
}
catch{
Write-Warning "Unable to parse XML, with error: $($Error[0])`n Ensure that there are NO SelectionChanged or TextChanged properties in your textboxes (PowerShell cannot process them)"
throw
}
$xaml.SelectNodes("//*[@Name]") | ForEach-Object{
try {Set-Variable -Name "$($_.Name)" -Value $Form.FindName($_.Name) -ErrorAction Stop}
catch{throw}
}
#Set Icon
$base64 = Get-Content "$PSScriptRoot\data\gui\icon.txt"
$bitmap = New-Object System.Windows.Media.Imaging.BitmapImage
$bitmap.BeginInit()
$bitmap.StreamSource = [System.IO.MemoryStream][System.Convert]::FromBase64String($base64)
$bitmap.EndInit()
$bitmap.Freeze()
$Form.Icon = $bitmap
#-------------------- Loading and adding functions
#Loading Functions in Array with Properties name,creator,gridname
$prop_files = Get-ChildItem -Recurse "$PSScriptRoot\functions\*\properties.json"
$functions = @()
foreach ($prop_file in $prop_files)
{
$props = (Get-Content $prop_file.FullName | ConvertFrom-Json)
$object = New-Object -TypeName PSObject
$object | Add-Member -Name 'name' -MemberType Noteproperty -Value $props.name
$object | Add-Member -Name 'creator' -MemberType Noteproperty -Value $props.creator
$object | Add-Member -Name 'gridname' -MemberType Noteproperty -Value $props.gridname
$functions += $object
}
#Adding Funcions to Searchbox
Foreach ($function in $functions)
{
$searchcombox.Items.Add($function.name)
}
#Set homegrid as visible
$homeGridname = "homegrid"
$Form.FindName($homeGridname).Visibility = "Visible"
$global:currentGRID = $Form.FindName($homeGridname)
#Loading ps1 scripts from functions
$func_files = Get-ChildItem -Recurse "$PSScriptRoot\functions\*\function.ps1"
foreach ($func_file in $func_files)
{
. $func_file.FullName
}
#-------------------- Adding events to form
#Change GRID visibility based on selection
$searchcombox.add_SelectionChanged({
$global:currentGRID.Visibility = "hidden"
$selectedfuntion = ($functions | Where-Object {$_.Name -eq $searchcombox.SelectedItem})
$global:currentGRID = $Form.FindName($selectedfuntion.gridname)
$global:currentGRID.Visibility = "Visible"
})
#-------------------- Showing GUI
#With POSH Console for debugging
$form.ShowDialog() | Out-Null
#Without POSH Console
#$Form.Add_Closing({[System.Windows.Forms.Application]::Exit(); Stop-Process $pid})
#$windowcode = '[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);'
#$asyncwindow = Add-Type -MemberDefinition $windowcode -name Win32ShowWindowAsync -namespace Win32Functions -PassThru
#null = $asyncwindow::ShowWindowAsync((Get-Process -PID $pid).MainWindowHandle, 0)
#[System.Windows.Forms.Integration.ElementHost]::EnableModelessKeyboardInterop($Form)
#$Form.Show()
#$Form.Activate()
#$appContext = New-Object System.Windows.Forms.ApplicationContext
#[void][System.Windows.Forms.Application]::Run($appContext)