Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

active directory - Powershell: How to get powershell to give you the option to select the right user when you search with Known name

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can check the number of Users returned by Get-ADUser by using the Count property. This will tell you how many objects (users) were returned.

If there is more than 1 user, you can use Out-GridView to display a popup dialogue to select the result you want from the list:

Out-GridView example

By default this allows multiple selections, but adding -OutputMode Single will then only allow a single selection to be chosen.

Your script can be updated like this:

$test = {
    $secgrp = $textbox.Text
    $Users = New-Object System.Collections.ArrayList
    $names = Import-Csv "H:	estGroups2.csv" 

    foreach ($name in $names.DisplayName) {
        $ReturnedUser = Get-ADUser -Filter { Name -like $name } -Properties SamAccountName | Select-Object -ExpandProperty SamAccountName

        if ($ReturnedUser.count > 1) {
            $SelectedUser = $ReturnedUser | Out-GridView -Title "Multiple Users have matched, select User to process" -OutputMode Single
            $null = $Users.Add($SelectedUser) #this syntax surpresses the .Add() from displaying the index of each item added
        }
        else {
            $null = $Users.Add($ReturnedUser)
        }
    }

    foreach ($User in $Users) {
        Add-ADGroupMember -Identity $secgrp -Members $User
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...