Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
powershell多用户输入分支_Powershell - Fatal编程技术网

powershell多用户输入分支

powershell多用户输入分支,powershell,Powershell,说到编程和powershell,我是个新手。我确实有很高的雄心壮志,所以我责成自己去创造一些比它本应该更困难的东西。到目前为止效果很好,但可能会更好 我的目标: 创建一个powershell脚本,该脚本将复制现有用户,以创建一个具有类似功能的新用户,该新用户具有一些类似的功能 脚本的功能如下: 询问管理员要复制哪个广告用户 请管理员命名新的广告用户 输出一些文字,然后在继续选择Y或N之前要求用户确认 如果管理员输入Y,则继续执行脚本。 如果管理员输入N,请重新启动脚本。 如果管理员输入了其他内容

说到编程和powershell,我是个新手。我确实有很高的雄心壮志,所以我责成自己去创造一些比它本应该更困难的东西。到目前为止效果很好,但可能会更好

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

脚本的功能如下:

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

我的问题: 如果管理员输入了错误的广告用户进行复制,而该用户不存在并抛出错误代码,我如何实现某种类型的错误检查?我希望控制台提醒他们这是错误的,并重新输入它

代码如下。如果您能提供任何完善建议,我们将不胜感激

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"

回答您的问题:

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

imho powershell脚本旨在节省时间,并拥有尽可能少的读取主机

没错。这对我来说主要是一个实验。它有一些用处,但它不会节省我很多时间,也不会提高我的广告用户复制率。