Skip to content

Commit 308a5c2

Browse files
committed
[Unattended answer file/ADDS] Add OU/User Picker
1 parent 7ca4077 commit 308a5c2

File tree

3 files changed

+150
-18
lines changed

3 files changed

+150
-18
lines changed

Elements/AutoUnattend/ActiveDirectory/ADDSJoinDialog.Designer.vb

Lines changed: 98 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Elements/AutoUnattend/ActiveDirectory/ADDSJoinDialog.vb

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Imports System.Net.NetworkInformation
33
Imports Microsoft.VisualBasic.ControlChars
44
Imports System.Text.RegularExpressions
5+
Imports System.DirectoryServices.AccountManagement
56

67
Public Class ADDSJoinDialog
78

@@ -65,6 +66,9 @@ Public Class ADDSJoinDialog
6566

6667
Private NtLogonPathStart As String = ""
6768

69+
Private userMappings As Dictionary(Of String, List(Of Principal))
70+
Private initialUserName As String = ""
71+
6872
Private Sub Cancel_Button_Click(sender As Object, e As EventArgs) Handles Cancel_Button.Click
6973
DialogResult = Windows.Forms.DialogResult.Cancel
7074
Close()
@@ -74,6 +78,8 @@ Public Class ADDSJoinDialog
7478
BackColor = CurrentTheme.SectionBackgroundColor
7579
ForeColor = CurrentTheme.ForegroundColor
7680
ComboBox1.BackColor = BackColor
81+
ComboBox2.BackColor = BackColor
82+
ComboBox3.BackColor = BackColor
7783
TextBox1.BackColor = BackColor
7884
TextBox2.BackColor = BackColor
7985
TextBox3.BackColor = BackColor
@@ -83,6 +89,8 @@ Public Class ADDSJoinDialog
8389
GroupBox1.BackColor = BackColor
8490
RichTextBox1.BackColor = BackColor
8591
ComboBox1.ForeColor = ForeColor
92+
ComboBox2.ForeColor = ForeColor
93+
ComboBox3.ForeColor = ForeColor
8694
TextBox1.ForeColor = ForeColor
8795
TextBox2.ForeColor = ForeColor
8896
TextBox3.ForeColor = ForeColor
@@ -97,6 +105,8 @@ Public Class ADDSJoinDialog
97105
ChangePage(WizardPage.DnsConfigPage)
98106
dnsMappings.Clear()
99107
ComboBox1.Items.Clear()
108+
ComboBox2.Items.Clear()
109+
ComboBox3.Items.Clear()
100110
Visible = True
101111
ADDSInitBW.RunWorkerAsync()
102112
Do Until Not ADDSInitBW.IsBusy
@@ -116,18 +126,30 @@ Public Class ADDSJoinDialog
116126
dsIsInDomain = DomainServicesModule.DSIsInDomain()
117127
If dsIsInDomain Then
118128
ProgressReporter.SetMessage("Getting name of assigned domain...")
119-
ADDSInitBW.ReportProgress(25)
129+
ADDSInitBW.ReportProgress(20)
120130
dsDomainName = DomainServicesModule.DSGetDomainName()
121131
End If
122132
ProgressReporter.SetMessage("Getting network adapters...")
123-
ADDSInitBW.ReportProgress(50)
133+
ADDSInitBW.ReportProgress(40)
124134
GetNetworkAdapters()
125135
ProgressReporter.SetMessage("Getting DNS addresses for each network adapter...")
126-
ADDSInitBW.ReportProgress(75)
136+
ADDSInitBW.ReportProgress(60)
127137
GetDnsAddresses()
128138
ProgressReporter.SetMessage("Getting primary domain controller NetBIOS name...")
129-
ADDSInitBW.ReportProgress(88)
139+
ADDSInitBW.ReportProgress(80)
130140
NtLogonPathStart = DomainServicesModule.DSGetDomainControllerNetBIOSName()
141+
If dsIsInDomain Then
142+
ProgressReporter.SetMessage("Mapping organizational units and users...")
143+
ADDSInitBW.ReportProgress(90)
144+
userMappings = DomainServicesModule.DSMapOrganizationalUnitsAndUsers(dsDomainName, NtLogonPathStart)
145+
If userMappings IsNot Nothing Then
146+
ComboBox2.Items.AddRange(userMappings.Keys.ToArray())
147+
ComboBox2.SelectedIndex = 0
148+
End If
149+
Else
150+
DomainAutoUserPanel.Enabled = False
151+
RadioButton3.Enabled = False
152+
End If
131153
ProgressReporter.SetMessage("Initialization complete.")
132154
ADDSInitBW.ReportProgress(100)
133155
End Sub
@@ -257,7 +279,7 @@ Public Class ADDSJoinDialog
257279
DialogResult = Windows.Forms.DialogResult.None
258280
Exit Sub
259281
Else
260-
dsInfo = New DomainInformation(TextBox4.Text, TextBox5.Text, TextBox6.Text)
282+
dsInfo = New DomainInformation(TextBox4.Text, initialUserName, TextBox6.Text)
261283
End If
262284
If ApplyDsSettings() Then
263285
MsgBox("Domain settings were added successfully to the answer file. You can further modify these components in the System components section.")
@@ -464,5 +486,28 @@ Public Class ADDSJoinDialog
464486
Private Sub TextBox5_TextChanged(sender As Object, e As EventArgs) Handles TextBox5.TextChanged
465487
AddsUpnPathText.Text = String.Format("{0}@{1}", TextBox5.Text, TextBox4.Text)
466488
AddsNtLogonPathText.Text = String.Format("{0}\{1}", NtLogonPathStart, TextBox5.Text)
489+
initialUserName = TextBox5.Text
490+
End Sub
491+
492+
Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
493+
DomainAutoUserPanel.Enabled = RadioButton3.Checked
494+
TextBox5.Enabled = Not RadioButton3.Checked
495+
TextBox4.Enabled = Not RadioButton3.Checked
496+
End Sub
497+
498+
Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
499+
Try
500+
ComboBox3.Items.Clear()
501+
ComboBox3.Items.AddRange(userMappings(ComboBox2.SelectedItem).Select(Function(adUser) adUser.DisplayName).ToArray())
502+
ComboBox3.SelectedIndex = 0
503+
Catch ex As Exception
504+
505+
End Try
506+
End Sub
507+
508+
Private Sub ComboBox3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox3.SelectedIndexChanged
509+
AddsUpnPathText.Text = String.Format("{0}@{1}", userMappings(ComboBox2.SelectedItem)(ComboBox3.SelectedIndex).SamAccountName, TextBox4.Text)
510+
AddsNtLogonPathText.Text = String.Format("{0}\{1}", NtLogonPathStart, userMappings(ComboBox2.SelectedItem)(ComboBox3.SelectedIndex).SamAccountName)
511+
initialUserName = userMappings(ComboBox2.SelectedItem)(ComboBox3.SelectedIndex).SamAccountName
467512
End Sub
468513
End Class

Elements/AutoUnattend/ActiveDirectory/DomainServicesModule.vb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ Module DomainServicesModule
188188

189189
searcher.Dispose() ' We need to call this due to some implementation quirks in the directory searcher
190190

191-
For i = 0 To orgUnits.Count
191+
For i = 0 To orgUnits.Count - 1
192192
Dim allUsers As New List(Of String)
193193
Dim ctx As PrincipalContext = New PrincipalContext(ContextType.Domain, dsDomainControllerNetBios, orgUnitPaths(i).Replace("LDAP://", ""))
194194
Dim qbeUser As UserPrincipal = New UserPrincipal(ctx)
@@ -214,7 +214,7 @@ Module DomainServicesModule
214214
Private Function GetLdapPathFromDnsName(dnsName As String) As String
215215
Dim pathParts() As String = dnsName.Split(".")
216216

217-
For i = 0 To pathParts.Length
217+
For i = 0 To pathParts.Length - 1
218218
pathParts(i) = String.Format("DC={0}", pathParts(i))
219219
Next
220220

0 commit comments

Comments
 (0)