-
Notifications
You must be signed in to change notification settings - Fork 2
/
PowerShellBooksGui.ps1
214 lines (168 loc) · 5.89 KB
/
PowerShellBooksGui.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<#
A simple GUI for the PowerShell Book Generator
#>
Import-Module $PSScriptRoot\PowerShellBooks -Force
#region XAML window definition
# Right-click XAML and choose WPF/Edit... to edit WPF Design
# in your favorite WPF editing tool
$xaml = @'
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
MinWidth="200"
Width ="400"
SizeToContent="Height"
Title="PowerShell Book Generator"
Topmost="false">
<Grid Margin="10,10,10,10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="35"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="3" Margin="5">The PowerShell Book Generator</TextBlock>
<Label Name="Link" Grid.Column="0" Grid.Row="4" Grid.ColumnSpan="3" Margin="5">www.andreasnick.com
<Label.Style>
<Style TargetType="Label">
<Setter Property="Foreground" Value="Blue" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>
<TextBlock Grid.Column="0" Grid.Row="1" Margin="5">PowerShell Module</TextBlock>
<TextBlock Grid.Column="0" Grid.Row="2" Margin="5">OutputFolder</TextBlock>
<ComboBox Name="ComboModule" Grid.Column="1" Grid.Row="1" Margin="5"></ComboBox>
<TextBox Name="TxtOutputFolder" Grid.Column="1" Grid.Row="2" Margin="5"></TextBox>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,10,0,0" Grid.Row="3" Grid.ColumnSpan="2">
<Button Name="ButStart" MinWidth="80" Height="22" Margin="5">Start</Button>
<Button Name="ButCancel" MinWidth="80" Height="22" Margin="5">Cancel</Button>
</StackPanel>
<Button Grid.Column="2" Grid.Row="2" Name="ButFilesearch" Width="25" Height="22" Margin="5">...</Button>
</Grid>
</Window>
'@
#endregion
#region Code Behind
function Convert-XAMLtoWindow
{
param
(
[Parameter(Mandatory=$true)]
[string]
$XAML
)
Add-Type -AssemblyName PresentationFramework
$reader = [XML.XMLReader]::Create([IO.StringReader]$XAML)
$result = [Windows.Markup.XAMLReader]::Load($reader)
$reader.Close()
$reader = [XML.XMLReader]::Create([IO.StringReader]$XAML)
while ($reader.Read())
{
$name=$reader.GetAttribute('Name')
if (!$name) { $name=$reader.GetAttribute('x:Name') }
if($name)
{$result | Add-Member NoteProperty -Name $name -Value $result.FindName($name) -Force}
}
$reader.Close()
$result
}
function Show-WPFWindow
{
param
(
[Parameter(Mandatory=$true)]
[Windows.Window]
$Window
)
$result = $null
$null = $window.Dispatcher.InvokeAsync{
$result = $window.ShowDialog()
Set-Variable -Name result -Value $result -Scope 1
}.Wait()
$result
}
#endregion Code Behind
#region Convert XAML to Window
$window = Convert-XAMLtoWindow -XAML $xaml
#endregion
#region Define Event Handlers
# Right-Click XAML Text and choose WPF/Attach Events to
# add more handlers
$window.ButCancel.add_Click(
{
$window.DialogResult = $false
}
)
$window.ButStart.add_Click(
{
#
$window.ButCancel.IsEnabled = $false
$window.ButStart.IsEnabled = $false
if(Test-Path $Window.TxtOutputFolder.Text){
$OutputDocument =$($Window.TxtOutputFolder.Text + "\PowerShell_With_" + $window.ComboModule.Text + '.pdf')
$OutputDocument = $OutputDocument -replace '\\','\'
Write-Output $OutputDocument
New-PowerShellBook -Module $window.ComboModule.Text -OutputPdfDocument $OutputDocument
} else {
Write-Error "The output path not exist!"
}
$window.ButCancel.IsEnabled = $true
$window.ButStart.IsEnabled = $true
}
)
$window.ButFilesearch.add_Click{
# remove param() block if access to event information is not required
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
[System.Windows.Forms.Application]::EnableVisualStyles()
$browse = New-Object System.Windows.Forms.FolderBrowserDialog
$browse.SelectedPath = $Window.TxtOutputFolder.Text
$browse.Description = "Select a directory"
if ($browse.ShowDialog() -eq "OK")
{
$Window.TxtOutputFolder.Text = $browse.SelectedPath
}
}
$window.Link.add_MouseDown{
# remove param() block if access to event information is not required
param
(
[Parameter(Mandatory)][Object]$sender,
[Parameter(Mandatory)][Windows.Input.MouseButtonEventArgs]$e
)
start "https://www.andreasnick.com"
}
#endregion Event Handlers
#region Manipulate Window Content
Write-Verbose "Query PowerShell modules.... please wait" -Verbose
$window.ComboModule.ItemsSource = (Get-Command -Module * | select -Property Module -Unique).Module | sort
$window.ComboModule.SelectedIndex=1
$window.TxtOutputFolder.Text = $($env:Userprofile +'\Desktop\')
#endregion
# Show Window
<#
while ($x -gt 0)
{
# Content
}
#>
$result = Show-WPFWindow -Window $window
#region Process results
if ($result -eq $true)
{
}
else
{
}
#endregion Process results