PowerShell,通过带有选项的交互式菜单配置AD用户

时间:2018-04-23 22:43:25

标签: powershell active-directory

我正在编写我的第一个(非常基本的)PowerShell脚本,它基本上会向用户询问一些信息,然后使用它来配置新的AD用户。一些非常基本的“$ value = Read-Host -Prompt'问题'”正在进行中。

但是,为了保持我们的命名惯例/适当的拼写和某些字段的大写(大多数是位置,可能的部门稍后在路上),我创建了一个菜单,提示用户在几个选项之间进行选择。下面是该代码的截屏:

Image 01

但是,GUI(缺少更好的单词)并不能完全显示出我想看的内容。理想情况下,这将要求用户输入SF,例如,使用描述来阅读旧金山。

Image 02

最后,我想将$ choice变量转换为该位置的实际名称,而不是分配给该选项的数字(0,1,2 ......),这样当我“写入 - 主机”时$ choice“它将填充我们在AD中使用的位置名称。

Image 03

# This script is my first attempt at hacking my way through New-ADUser automation using as little user input as necessary. Bear with me.

# Let's prompt the user for some information.
$name = Read-Host -Prompt 'What is their name?'
$dept = Read-Host -Prompt 'What is their department, man?'
$title = Read-Host -Prompt 'What is their title?'
$mngr = Read-Host -Prompt 'Who is their manager?'

# Let's parse that $name into a first name and last name, and then generate the user's e-mail.
$first = $name.split(" ")[0]
$last = $name.split(" ")[1]
$accountname = $first.tolower() + '.' + $last.tolower()
$email = $first.tolower() + '.' + $last.tolower() + '@mycompany.com'

# Let's prompt the user for the new hire's primary location.
$menu = "Choose Location"
$message = "What is the user's primary location?"

$sf = New-Object System.Management.Automation.Host.ChoiceDescription "SF", "San Francisco"
$01 = New-Object System.Management.Automation.Host.ChoiceDescription "01", "San Leandro"
$02 = New-Object System.Management.Automation.Host.ChoiceDescription "02", "Mechanicsburg"
$03 = New-Object System.Management.Automation.Host.ChoiceDescription "03", "Vernon Hills"
$04 = New-Object System.Management.Automation.Host.ChoiceDescription "04", "Duluth"
$05 = New-Object System.Management.Automation.Host.ChoiceDescription "05", "Phoenix"

$options = [System.Management.Automation.Host.ChoiceDescription[]]($sf, $01, $02, $03, $04, $05)

$choice = $host.ui.PromptForChoice($menu, $message, $options, 0)

switch ($choice)
{
    0 {$location = "San Francisco"}
    1 {$location = "San Leandro"}
    2 {$location = "Mechanicsburg"}
    3 {$location = "Vernon Hills"}
    4 {$location = "Duluth"}
    5 {$location = "Phoenix"}
}

Write-Host $name
Write-Host $dept
Write-Host $title
Write-Host $mngr
Write-Host $accountname
Write-Host $email
Write-Host $location

0 个答案:

没有答案