1+ # Requires -Version 5.0.9814.0
2+
3+
4+
5+ if (! ($PSVersionTable.PSVersion.Major -ge 5 -and $PSVersionTable.PSVersion.Build -ge 9814 )) {
6+
7+
8+
9+ ' Sorry you need PSVersion 5.0.9814.0 or newer'
10+
11+ $psversiontable
12+
13+ return
14+
15+ }
16+
17+
18+
19+ Add-Type - AssemblyName presentationframework
20+
21+
22+
23+ $XAML = @'
24+
25+ <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
26+
27+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
28+
29+ WindowStartupLocation="CenterScreen"
30+
31+ Title="ConvertFrom-String Buddy" Height="650" Width="850">
32+
33+
34+
35+ <Grid >
36+
37+ <Grid.ColumnDefinitions>
38+
39+ <ColumnDefinition/>
40+
41+ <ColumnDefinition/>
42+
43+ </Grid.ColumnDefinitions>
44+
45+
46+
47+ <Grid.RowDefinitions>
48+
49+ <RowDefinition Height="42"/>
50+
51+ <RowDefinition/>
52+
53+ <RowDefinition/>
54+
55+ <RowDefinition Height="150"/>
56+
57+ </Grid.RowDefinitions>
58+
59+
60+
61+ <StackPanel Orientation="Horizontal" Grid.Column="0" Margin="3">
62+
63+ <Button x:Name="btnClear" Content=" C_lear All " Grid.Row="0" Margin="3" Width="Auto" HorizontalAlignment="Left"/>
64+
65+ <Button x:Name="btnCopy" Content=" _Copy PowerShell Code " Margin="3" Width="Auto" HorizontalAlignment="Left"/>
66+
67+ </StackPanel>
68+
69+
70+
71+ <GroupBox Header=" _Data " Grid.Row="1" Grid.Column="0" Margin="3">
72+
73+ <TextBox x:Name="Data" Margin="3"
74+
75+ FontFamily="Consolas"
76+
77+ FontSize="14"
78+
79+ AcceptsReturn="True"
80+
81+ AcceptsTab="True"
82+
83+ VerticalScrollBarVisibility="Visible"
84+
85+ HorizontalScrollBarVisibility="Visible"/>
86+
87+ </GroupBox>
88+
89+
90+
91+ <GroupBox Header=" _Template " Grid.Row="1" Grid.Column="1" Margin="3">
92+
93+ <TextBox x:Name="Template" Margin="3"
94+
95+ FontFamily="Consolas"
96+
97+ FontSize="14"
98+
99+ AcceptsReturn="True"
100+
101+ AcceptsTab="True"
102+
103+ VerticalScrollBarVisibility="Visible"
104+
105+ HorizontalScrollBarVisibility="Visible"/>
106+
107+ </GroupBox>
108+
109+
110+
111+ <GroupBox Header=" _Result " Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" Margin="3">
112+
113+ <TextBox x:Name="Result" Margin="3" IsReadOnly="True"
114+
115+ FontFamily="Consolas"
116+
117+ FontSize="14"
118+
119+ TextWrapping="Wrap"
120+
121+ VerticalScrollBarVisibility="Visible"
122+
123+ HorizontalScrollBarVisibility="Visible"/>
124+
125+ </GroupBox>
126+
127+
128+
129+ <GroupBox Header=" C_ode " Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" Margin="3">
130+
131+ <TextBox x:Name="Code" Margin="3" IsReadOnly="True"
132+
133+ FontFamily="Consolas"
134+
135+ FontSize="14"
136+
137+ TextWrapping="Wrap"
138+
139+ VerticalScrollBarVisibility="Visible"
140+
141+ HorizontalScrollBarVisibility="Visible"/>
142+
143+ </GroupBox>
144+
145+
146+
147+ </Grid>
148+
149+ </Window>
150+
151+ '@
152+
153+
154+
155+ $Window = [Windows.Markup.XamlReader ]::Load((New-Object System.Xml.XmlNodeReader ([xml ]$XAML )))
156+
157+
158+
159+ $DataPane = $Window.FindName (' Data' )
160+
161+ $TemplatePane = $Window.FindName (' Template' )
162+
163+ $ResultPane = $Window.FindName (' Result' )
164+
165+ $CodePane = $Window.FindName (' Code' )
166+
167+
168+
169+ $ButtonCopy = $Window.FindName (' btnCopy' )
170+
171+ $ButtonClear = $Window.FindName (' btnClear' )
172+
173+
174+
175+ $DataPane.Text = @"
176+
177+ Aaron Crow,java clojure gcal
178+
179+ BAD ENTRY
180+
181+ Alvin Chyan,java ruby clojure
182+
183+ Artem Boytsov,flying southwest
184+
185+ REAL BAD ENTRY
186+
187+ Maverick Lou,java clojure jenkins
188+
189+ Vinnie Pepi,javascript ruby clojure
190+
191+ Will Lao,java ruby javascript clojure
192+
193+ "@
194+
195+
196+
197+ $TemplatePane.Text = @"
198+
199+ {First*:Aaron} {Last:Crow},{Skills:java cloj}
200+
201+ {First*:Alvin}
202+
203+ "@
204+
205+
206+
207+ function Export-Code {
208+
209+ if ($ResultPane.Text.Trim ().Length -eq 0 ) {return }
210+
211+ $CodePane.Text = @"
212+
213+
214+
215+ `$ targetData = @'
216+
217+ $ ( $DataPane.Text )
218+
219+ '@
220+
221+
222+
223+ `$ TemplateContent = @'
224+
225+ $ ( $TemplatePane.Text )
226+
227+ '@
228+
229+
230+
231+ `$ targetData | ConvertFrom-String -TemplateContent `$ TemplateContent
232+
233+ "@
234+
235+ }
236+
237+
238+
239+ function Invoke-CFS {
240+
241+
242+
243+ $ResultPane.Foreground = ' Black'
244+
245+ $ResultPane.Background = ' White'
246+
247+ $ResultPane.FontWeight = ' Normal'
248+
249+
250+
251+ $Error.Clear ()
252+
253+ try {
254+
255+ $r = $DataPane.Text |
256+
257+ ConvertFrom-String - TemplateContent $TemplatePane.Text |
258+
259+ Select * - ExcludeProperty ExtentText |
260+
261+ Ft - A |
262+
263+ Out-String
264+
265+ } catch {
266+
267+ $ResultPane.Foreground = ' Red'
268+
269+ $ResultPane.Background = ' Blue'
270+
271+ $ResultPane.FontWeight = ' Bold'
272+
273+ $r = $Error [0 ]
274+
275+ }
276+
277+
278+
279+ $ResultPane.Text = $r # .Trim()
280+
281+ }
282+
283+
284+
285+ function Invoke-CFSGen {
286+
287+ Invoke-CFS
288+
289+ Export-Code
290+
291+ }
292+
293+
294+
295+ $ButtonCopy.Add_Click ({$CodePane.Text | Clip})
296+
297+
298+
299+ $ButtonClear.Add_Click ({
300+
301+ $CodePane.Text = $null
302+
303+ $TemplatePane.Text = $null
304+
305+ $ResultPane.Text = $null
306+
307+ $DataPane.Text = $null
308+
309+ })
310+
311+
312+
313+ $timer = New-Object System.Windows.Threading.DispatcherTimer
314+
315+ $timer.Interval = [timespan ]' 0:0:0.500'
316+
317+
318+
319+ $timer.Add_Tick ({
320+
321+ Invoke-CFSGen
322+
323+ $timer.Stop ()
324+
325+ })
326+
327+
328+
329+ $DataPane.Add_TextChanged ({
330+
331+ $timer.Stop ()
332+
333+ $timer.Start ()
334+
335+ })
336+
337+
338+
339+ $TemplatePane.Add_TextChanged ({
340+
341+ $timer.Stop ()
342+
343+ $timer.Start ()
344+
345+ })
346+
347+
348+
349+ Invoke-CFSGen
350+
351+
352+
353+ [void ]$DataPane.Focus ()
354+
355+ [void ]$Window.ShowDialog ()
0 commit comments