I have creates a Powershell script that takes a display name from a CSV looks up there username and then adds them to a security group in AD.
The problem is people with the same Display name. My script when it hits the same display name it will just add every user name with that display name.
I would like an option when it hits a name that returns multiple username that it displays an option that allows someone to pick the right username then add them to the security group.
I am fairly new to PowerShell and have come a bit stuck at this point so any help is greatly appreciated.
Import-Module ActiveDirectory
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "Security Group Tool"
$Form.Size = New-Object System.Drawing.Size(390,150)
$Form.StartPosition = "CenterScreen"
$Form.KeyPreview = $True
$Form.MaximumSize = $Form.Size
$Form.MinimumSize = $Form.Size
$Icon = New-Object System.Drawing.Icon("H: estfavicon.ico")
$Form.Icon = $Icon
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Size(10, 10)
$label.Size = New-Object System.Drawing.Size(400, 15)
$label.Text = "Please enter The name of the Security Group You want to add users too"
$Form.Controls.Add($label)
$textbox = New-Object System.Windows.Forms.TextBox
$textbox.Location = New-Object System.Drawing.Size(10,50)
$textbox.Size = New-Object System.Drawing.Size(240,40)
$Form.Controls.Add($textbox)
$test = {
$secgrp = $textbox.Text
$Sam = @()
$names = Import-Csv "H: estGroups2.csv"
foreach ($name in $names.DisplayName) {
$Sam += Get-ADUser -Filter { Name -like $name } -Properties SamAccountName | Select-Object SamAccountName
}
$User = $Sam
foreach ($User in $User) {
Add-ADGroupMember -Identity $secgrp -Members $User
}
}
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(260,45)
$OKButton.Size = New-Object System.Drawing.Size(75,30)
$OKButton.Text = "OK"
$OKButton.Add_Click($test)
$Form.Controls.Add($OKButton)
$Form.Add_KeyDown({
if ($_.KeyCode -eq "Enter") {
& $test
}
})
$Form.Add_KeyDown({
if ($_.KeyCode -eq "Escape") {
$Form.Close()
}
})
$Form.TopMost = $True
$Form.Add_Shown({ $Form.Activate() })
[void] $Form.ShowDialog()
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…