-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathOnlineAppxRemovalDialog.vb
More file actions
147 lines (118 loc) · 7.83 KB
/
Copy pathOnlineAppxRemovalDialog.vb
File metadata and controls
147 lines (118 loc) · 7.83 KB
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
Imports System.IO
Imports System.Management
Imports System.Windows.Forms
Imports Microsoft.Win32
Public Class OnlineAppxRemovalDialog
Private Appxs As New List(Of String)
Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
If ListView1.CheckedItems.Count = 0 Then
MessageBox.Show(GetValueFromLanguageData("OnlineAppxRemovalDialog.AppxRemoval_NoPackages"), Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
MessageBox.Show(GetValueFromLanguageData("OnlineAppxRemovalDialog.AppxRemoval_AutoRestart"), Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
Cursor = Cursors.WaitCursor
Dim MarkedAppxPackages As New List(Of String)
MarkedAppxPackages.AddRange(ListView1.CheckedItems.Cast(Of ListViewItem)().Select(Function(lvi) lvi.Text))
InvokeAppxRemoval(String.Join(";", MarkedAppxPackages.ToArray()))
Me.DialogResult = System.Windows.Forms.DialogResult.OK
Me.Close()
End Sub
Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
Me.Close()
End Sub
Private Sub InvokeAppxRemoval(MarkedAppxPackagesFilter As String)
Dim extAppxHelperPath As String = Path.Combine(Application.StartupPath, "Tools", "RemoveOnlineAppxPackage.ps1")
If File.Exists(extAppxHelperPath) Then
Dim psAppxRemovalProc As New Process() With {
.StartInfo = New ProcessStartInfo() With {
.FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "system32", "WindowsPowerShell", "v1.0", "powershell.exe"),
.Arguments = String.Format("-executionpolicy Bypass -noprofile -nologo -file {0}{1}{0} -appxFullNames {0}{2}{0}", ControlChars.Quote, extAppxHelperPath, MarkedAppxPackagesFilter),
.CreateNoWindow = True,
.WindowStyle = ProcessWindowStyle.Hidden
}
}
psAppxRemovalProc.Start()
psAppxRemovalProc.WaitForExit()
If psAppxRemovalProc.ExitCode = 0 Then
Cursor = Cursors.Arrow
Dim cmdArgParts As IEnumerable(Of String) = Environment.GetCommandLineArgs().Skip(1)
Dim cmdArgs As String = String.Join(" ", cmdArgParts.ToArray())
Dim regArgs As String = String.Format("{0}{0}{0}{1}{0}{0} {2} /skipwelcome{0}", ControlChars.Quote, Application.ExecutablePath, String.Join(" ", cmdArgs))
Dim regProc As New Process() With {
.StartInfo = New ProcessStartInfo() With {
.FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "system32", "reg.exe"),
.Arguments = String.Format("add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /f /v SysprepPrepTool /t REG_SZ /d {0}", regArgs),
.CreateNoWindow = True,
.WindowStyle = ProcessWindowStyle.Hidden
}
}
regProc.Start()
regProc.WaitForExit()
' Invoke the restart operation in 1 minute
DynaLog.LogMessage("Restarting the computer in 1 minute!")
Process.Start(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "system32", "shutdown.exe"),
String.Format("/r /t 60 /c {0}{1}{0}", ControlChars.Quote, GetValueFromLanguageData("OnlineAppxRemovalDialog.RestartPrompt_60sec"))).WaitForExit()
If MessageBox.Show(GetValueFromLanguageData("OnlineAppxRemovalDialog.RestartPrompt_Now"), Text, MessageBoxButtons.OK, MessageBoxIcon.Information) = DialogResult.OK Then
DynaLog.LogMessage("Restarting the computer NOW!!!")
Process.Start(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "system32", "shutdown.exe"), "/a")
Process.Start(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "system32", "shutdown.exe"), "/r /t 0").WaitForExit()
End If
End If
End If
End Sub
Private Sub GetAppxPackages()
Try
Dim thirdPartyAppxsRk As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\AppModel\StagingInfo", False)
Appxs = thirdPartyAppxsRk.GetSubKeyNames().ToList()
thirdPartyAppxsRk.Close()
Dim imgEditionRk As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion", False)
Dim imgEditionId As String = imgEditionRk.GetValue("EditionID", "")
imgEditionRk.Close()
If {"EnterpriseS", "IoTEnterpriseS"}.Contains(imgEditionId) Then
DynaLog.LogMessage("Windows (IoT) Enterprise LTSC detected; checking presence of Store appx...")
Try
Dim userSidMOC As ManagementObjectCollection = New ManagementObjectSearcher(String.Format("SELECT SID FROM Win32_UserAccount WHERE Name = {0}{1}{0}", ControlChars.Quote, Environment.GetEnvironmentVariable("USERNAME"))).Get()
Dim userSid As String = WMIHelper.GetObjectValue(userSidMOC(0), "SID")
Dim pkgRepoRootPath As String = GetAppxPackageRepositoryRootPath()
Dim StoreAppDirectories As String() = Directory.EnumerateDirectories(Path.Combine(pkgRepoRootPath, "Packages"), "Microsoft.WindowsStore*").ToArray()
DynaLog.LogMessage("Verifying Store AppX packages for user with SID " & userSid)
For Each StoreAppDirectory In StoreAppDirectories
Dim pckgdepPath As String = Path.Combine(StoreAppDirectory, String.Format("{0}.pckgdep", userSid))
If File.Exists(pckgdepPath) Then
DynaLog.LogMessage("This appx package has a pckgdep for this user! Adding...")
Appxs.Add(Path.GetDirectoryName(pckgdepPath).Split("\").Last())
End If
Next
Catch ex As Exception
DynaLog.LogMessage(ex.Message)
End Try
End If
Catch ex As Exception
End Try
End Sub
Private Function GetAppxPackageRepositoryRootPath() As String
Dim PackageRepositoryRootPath As String = ""
Try
Dim AppxRootKey As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Appx", False)
PackageRepositoryRootPath = AppxRootKey.GetValue("PackageRepositoryRoot", "")
AppxRootKey.Close()
Catch ex As Exception
DynaLog.LogMessage("Could not check root path. Error message: " & ex.Message)
End Try
Return PackageRepositoryRootPath
End Function
Private Sub OnlineAppxRemovalDialog_Load(sender As Object, e As EventArgs) Handles MyBase.Load
GetAppxPackages()
Text = GetValueFromLanguageData("OnlineAppxRemovalDialog.WndTitle")
Label1.Text = GetValueFromLanguageData("OnlineAppxRemovalDialog.AppxRemoval_Title")
Label2.Text = GetValueFromLanguageData("OnlineAppxRemovalDialog.AppxRemoval_Notes")
OK_Button.Text = GetValueFromLanguageData("Common.Common_OK")
Cancel_Button.Text = GetValueFromLanguageData("Common.Common_Cancel")
ListView1.Items.Clear()
ListView1.Items.AddRange(Appxs.Select(Function(Appx) New ListViewItem(New String() {Appx})).ToArray())
End Sub
Private Sub ListView1_ItemChecked(sender As Object, e As ItemCheckedEventArgs) Handles ListView1.ItemChecked
OK_Button.Enabled = ListView1.CheckedItems.Count > 0
End Sub
End Class