powershell多用户输入分支

时间:2017-03-22 15:10:20

标签: powershell

在编程和PowerShell方面,我是新手。我确实有很高的抱负,所以我要求自己创造比现在更难的东西。到目前为止工作得很好,但可能会更好。

我的目标: 创建一个powershell脚本,该脚本将复制现有用户以创建具有类似功能的新用户,并具有一些铃声和口哨声。

脚本功能:

询问管理员哪个AD用户要复制 请管理员为新AD用户命名 输出一些文字,然后在继续选择Y或N之前询问用户确认 如果管理员输入Y,请继续执行脚本。 如果管理员输入N,请重新启动脚本。 如果管理员输入其他内容,请再次提出问题。

我的问题: 如果管理员输入错误的AD用户进行复制,如何进行某种错误检查,哪些不存在并抛出错误代码?我希望控制台提醒他们这是错误的并重新进入。

以下代码。任何完善它的提示都会受到赞赏。

PS:我刚刚开始使用PowerShell和编码,所以我在做一些这么简单的事情上超过了顶层,但我学到了很多东西。我也计划清理代码。也许这段代码可能对那些寻找类似内容的人有用

Write-Host "****************************************************************"
Write-Host "**" -nonewline
Write-Host "               New User Creation Script                     " -ForegroundColor yellow -nonewline
Write-Host "**"
Write-Host "****************************************************************`n"



Do {


Write-Host "Enter an AD Username to copy: " -ForegroundColor Green -NoNewline

$InputUser = Read-Host
$User = Get-AdUser -Identity $InputUser -Properties OfficePhone, Title, Department, State, Streetaddress, City, Country,  Office, HomePage, Fax, Description, co, OfficePhone, PostalCode
$DN = $User.distinguishedName
$OldUser = [ADSI]"LDAP://$DN"
$Parent = $OldUser.Parent
$OU = [ADSI]$Parent
$OUDN = $OU.distinguishedName

Write-Host "Enter New Username: " -ForegroundColor Green -NoNewline
$NewUser = Read-Host

Write-Host "Enter First Name: " -ForegroundColor Green -NoNewline
$FirstName = Read-Host

Write-Host "Last Name: " -ForegroundColor Green -NoNewline
$LastName = Read-Host

$NewName = "$firstname $lastname"

Write-Host "Domain Name such as `"" -ForegroundColor Green -NoNewline
Write-Host "Datamartinc.net" -ForegroundColor Yellow -NoNewline
Write-Host "`": " -ForegroundColor Green -NoNewline
$Domain = Read-Host

$upn = $NewUser+"@$Domain"


Write-Host "`n---------------------------------------------------------------`n"
Write-Host "`Username:    " -ForegroundColor Yellow -NoNewline
Write-Host "$NewUser" 
Write-Host "First Name:  " -ForegroundColor Yellow -NoNewline
Write-Host "$FirstName"
Write-Host "Last Name:   " -ForegroundColor Yellow -NoNewline
Write-Host "$LastName"
Write-Host "UPN:         " -ForegroundColor Yellow -NoNewline
Write-Host "$upn"
Write-Host "Copied User: " -ForegroundColor Yellow -NoNewline
Write-Host "$InputUser"

Do {

Write-Host "`nPress " -NoNewline
Write-Host "Y " -ForegroundColor Yellow -NoNewline
Write-Host "to confirm and " -NoNewline
Write-Host "N " -ForegroundColor Yellow -NoNewline
Write-Host "to redo: " -NoNewline
$confirm = Read-Host

} until (($Confirm -eq 'y') -or ($Confirm -eq 'n'))





if($Confirm -eq 'y') 
{
New-ADUser -SamAccountName $NewUser -userPrincipalName $upn -Name $NewName -GivenName $firstname -Surname $lastname -Instance $DN -Path "$OUDN" -AccountPassword (Read-Host "New Password: " -AsSecureString) –ChangePasswordAtLogon $false
Get-ADUser -Identity $InputUser -Properties memberof | Select-Object -ExpandProperty memberof | Add-ADGroupMember -Members $NewUser
Set-ADUser -Identity "$NewUser" -CannotChangePassword:$true -PasswordNeverExpires:$True
Enable-ADAccount -Identity $NewUser

$Completed = "y"
; $Confirm ="n"}

   else {Clear-Host
   }


      }

Until ($Completed -eq "y")

Write-Host "AD User Creation Completed Successfully"

1 个答案:

答案 0 :(得分:0)

回答您的问题:

try
{
    get-aduser $InputUser -Properties * -ErrorAction Stop
}
catch
{
    write-host $_.Exception.Message
}

imho powershell脚本旨在节省时间并尽可能少地读取主机。