GUI/AutoPilotGUI.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#=======================================================================
# PowershellWindow Functions
#=======================================================================
$Script:showWindowAsync = Add-Type -MemberDefinition @"
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
"@
 -Name "Win32ShowWindowAsync" -Namespace Win32Functions -PassThru
function Show-PowershellWindow() {
    $null = $showWindowAsync::ShowWindowAsync((Get-Process -Id $pid).MainWindowHandle, 10)
}
function Hide-PowershellWindow() {
    $null = $showWindowAsync::ShowWindowAsync((Get-Process -Id $pid).MainWindowHandle, 2)
}

#Hide-PowershellWindow
#=======================================================================
# MahApps.Metro
#=======================================================================
# Assign current script directory to a global variable
$Global:MyScriptDir = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition)

# Load presentationframework and Dlls for the MahApps.Metro theme
[System.Reflection.Assembly]::LoadWithPartialName("presentationframework") | Out-Null
[System.Reflection.Assembly]::LoadFrom("$Global:MyScriptDir\assembly\System.Windows.Interactivity.dll") | Out-Null
[System.Reflection.Assembly]::LoadFrom("$Global:MyScriptDir\assembly\MahApps.Metro.dll") | Out-Null

# Set console size and title
$host.ui.RawUI.WindowTitle = "OSDeploy AutopilotGUI"
#=======================================================================
# Test-InWinPE
#=======================================================================
function Test-InWinPE {
    return Test-Path -Path Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlset\Control\MiniNT
}
#=======================================================================
# LoadForm
#=======================================================================
function LoadForm {
    [CmdletBinding()]
    Param(
     [Parameter(Mandatory=$False,Position=1)]
     [string]$XamlPath
    )
    
    # Import the XAML code
    #[xml]$Global:xmlWPF = Get-Content -Path $XamlPath

    [xml]$Global:xmlWPF = @"
    <Controls:MetroWindow
        xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Controls = "http://metro.mahapps.com/winfx/xaml/controls"
 
        Title = "AutopilotGUI"
        Width = "600"
        Height = "360"
 
        GlowBrush = "{DynamicResource MahApps.Brushes.Accent}"
         
        WindowStartupLocation = "CenterScreen"
    >
 
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
                <!-- Accent and AppTheme setting -->
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
 
        <Grid>
            <StackPanel>
                <CheckBox Name = "OnlineCheckbox"
                    IsChecked = "True"
                    IsEnabled = "False"
                    HorizontalAlignment = "Left" Margin = "20,10,0,0" VerticalAlignment = "Top" Width = "500" FontSize = "14">
                    Online: Register this device to the Microsoft Graph authenticated Tenant
                </CheckBox>
            </StackPanel>
 
            <StackPanel>
                <CheckBox Name = "AssignCheckbox"
                    HorizontalAlignment = "Left" Margin = "20,40,0,0" VerticalAlignment = "Top" Width = "500" FontSize = "14" Background = "LightBlue">
                    Assign: Wait for Intune to assign an Autopilot profile to the device
                </CheckBox>
            </StackPanel>
 
            <StackPanel>
                <Label
                    Name = "GroupTagLabel"
                    Content = "GroupTag:"
                    HorizontalAlignment = "Left" Margin = "20,70,0,0" VerticalAlignment = "Top" Width = "100" FontSize = "14"
                />
            </StackPanel>
            <StackPanel>
                <TextBox
                    Name = "GroupTagTextBox"
                    HorizontalAlignment = "Left" Margin = "120,70,0,0" VerticalAlignment = "Top" Width = "320" FontSize = "14" Height = "30" Background = "LightBlue"
                />
            </StackPanel>
 
            <StackPanel>
                <Label
                    Name = "AddToGroupLabel"
                    Content = "AddToGroup:"
                    HorizontalAlignment = "Left" Margin = "20,110,0,0" VerticalAlignment = "Top" Width = "100" FontSize = "14"
                />
            </StackPanel>
            <StackPanel>
                <TextBox
                    Name = "AddToGroupTextBox"
                    HorizontalAlignment = "Left" Margin = "120,110,0,0" VerticalAlignment = "Top" Width = "320" Height = "30" FontSize = "14"
                />
            </StackPanel>
 
            <StackPanel Orientation="Horizontal">
                <Button
                    Name = "GetWindowsAutopilotInfoButton"
                    Content = "Register"
                    HorizontalAlignment = "Right" Margin = "470,70,0,0" VerticalAlignment = "Top" Width = "100" Height = "70" FontSize = "14" Background = "LightBlue"
                />
            </StackPanel>
 
            <Separator/>
 
            <StackPanel Orientation="Horizontal">
                <Button
                    Name = "RunButton"
                    Content = "Run"
                    HorizontalAlignment = "Right" Margin = "20,190,0,0" VerticalAlignment = "Top" Width = "50" Height = "30" FontSize = "14"
                />
            </StackPanel>
 
            <StackPanel>
                <TextBox
                    Name = "RunTextBox"
                    HorizontalAlignment = "Left" Margin = "75,190,0,0" VerticalAlignment = "Top" Width = "495" Height = "30" FontSize = "14"
                />
            </StackPanel>
 
            <StackPanel Orientation="Horizontal">
                <Button
                    Name = "PowerShellButton"
                    Content = "PowerShell"
                    HorizontalAlignment = "Right" Margin = "20,240,0,0" VerticalAlignment = "Top" Width = "150" Height = "30" FontSize = "14"
                />
            </StackPanel>
 
            <StackPanel Orientation="Horizontal">
                <Button
                    Name = "SettingsButton"
                    Content = "Settings"
                    HorizontalAlignment = "Right" Margin = "220,240,0,0" VerticalAlignment = "Top" Width = "150" Height = "30" FontSize = "14"
                />
            </StackPanel>
 
            <StackPanel Orientation="Horizontal">
                <Button
                    Name = "WirelessButton"
                    Content = "Wireless"
                    HorizontalAlignment = "Right" Margin = "420,240,0,0" VerticalAlignment = "Top" Width = "150" Height = "30" FontSize = "14"
                />
            </StackPanel>
 
            <StackPanel Orientation="Horizontal">
                <Button
                    Name = "SysprepButton"
                    Content = "Sysprep OOBE"
                    HorizontalAlignment = "Right" Margin = "20,280,0,0" VerticalAlignment = "Top" Width = "150" Height = "30" FontSize = "14" Background = "LightBlue"
                />
            </StackPanel>
 
            <StackPanel Orientation="Horizontal">
                <Button
                    Name = "RestartButton"
                    Content = "Restart"
                    HorizontalAlignment = "Right" Margin = "220,280,0,0" VerticalAlignment = "Top" Width = "150" Height = "30" FontSize = "14" Background = "LightBlue"
                />
            </StackPanel>
 
            <StackPanel Orientation="Horizontal">
                <Button
                    Name = "ShutdownButton"
                    Content = "Shutdown"
                    HorizontalAlignment = "Right" Margin = "420,280,0,0" VerticalAlignment = "Top" Width = "150" Height = "30" FontSize = "14"
                />
            </StackPanel>
 
        </Grid>
    </Controls:MetroWindow>
"@


    # Add WPF and Windows Forms assemblies
    Try {
        Add-Type -AssemblyName PresentationCore,PresentationFramework,WindowsBase,system.windows.forms
    } 
    Catch {
        Throw "Failed to load Windows Presentation Framework assemblies."
    }

    #Create the XAML reader using a new XML node reader
    $Global:xamGUI = [Windows.Markup.XamlReader]::Load((new-object System.Xml.XmlNodeReader $xmlWPF))

    #Create hooks to each named object in the XAML
    $xmlWPF.SelectNodes("//*[@Name]") | foreach {
        Set-Variable -Name ($_.Name) -Value $xamGUI.FindName($_.Name) -Scope Global
    }
}
#=======================================================================
# LoadForm
#=======================================================================
LoadForm
#=======================================================================
# Initialize
#=======================================================================
#$AddToGroupTextBox.Text = 'Enterprise'
#$GroupTagTextBox.Text = 'Enterprise'
$RunTextBox.Text = 'https://docs.microsoft.com/en-us/mem/autopilot/'
#=======================================================================
# add_Click
#=======================================================================
$GetWindowsAutopilotInfoButton.add_Click({
    $xamGUI.Close()
    Show-PowershellWindow

    Write-Host -ForegroundColor Cyan "Online: $true"
    $Params = @{
        Online = $true
    }

    if ($AssignCheckbox.IsChecked) {
        Write-Host -ForegroundColor Cyan "Assign: $true" 
        $Params.Assign = $true
    }

    if ($GroupTagTextBox.Text -gt 0) {
        $Params.GroupTag = $GroupTagTextBox.Text
        Write-Host -ForegroundColor Cyan "GroupTag: $($Params.GroupTag)" 
    }

    if ($AddToGroupTextBox.Text -gt 0) {
        $Params.AddToGroup = $AddToGroupTextBox.Text
        Write-Host -ForegroundColor Cyan "AddToGroup: $($Params.AddToGroup)" 
    }

    Write-Host -ForegroundColor Cyan "Install-Script Get-WindowsAutoPilotInfo"
    Start-Sleep -Seconds 3
    Install-Script Get-WindowsAutoPilotInfo -Force

    Write-Host -ForegroundColor Cyan "Get-WindowsAutoPilotInfo"
    Start-Sleep -Seconds 3

    Get-WindowsAutoPilotInfo @Params
    Start-Sleep -Seconds 3
    Start-AutopilotGUI
})
#=======================================================================
# RunButton
#=======================================================================
$RunButton.add_Click({
    Write-Host -ForegroundColor Cyan "Run: $($RunTextBox.Text)"
    try {
        Start-Process $RunTextBox.Text
    }
    catch {
        Write-Warning "Could not execute $($RunTextBox.Text)"
    }
})
#=======================================================================
# SettingsButton
#=======================================================================
$SettingsButton.add_Click({
    Start-Process ms-settings:
})
#=======================================================================
# WirelessButton
#=======================================================================
$WirelessButton.add_Click({
    Start-Process ms-availablenetworks:
})
#=======================================================================
# PowerShellButton
#=======================================================================
$PowerShellButton.add_Click({
    Start-Process PowerShell.exe -ArgumentList "-Nologo"
})
#=======================================================================
# SysprepButton
#=======================================================================
$SysprepButton.add_Click({
    Start-Process "$env:SystemRoot\System32\Sysprep\Sysprep.exe" -ArgumentList "/oobe","/quit"
})
#=======================================================================
# RestartButton
#=======================================================================
$RestartButton.add_Click({
    Restart-Computer
})
#=======================================================================
# ShutdownButton
#=======================================================================
$ShutdownButton.add_Click({
    Stop-Computer
})
#=======================================================================
# ShowDialog
#=======================================================================
$xamGUI.ShowDialog() | Out-Null
#=======================================================================