如何在powershell中组合名字和姓氏



大家好,我目前正在学习PowerShell,我有一个脚本,我可以创建一个Active Directory用户,我正在尝试组合名字和姓氏(firstname.lastname(,并将其设置为$logonname,这样用户就不必输入该信息。是否使用了某种类型的命令来实现这一点?

import-module activedirectory
#Define variable
$OU ="OU=mine,DC=mine,DC=mine"

$firstname = Read-Host 'Enter New Users First Name'

$lastname = Read-Host 'Enter New Users Last Name'
$logonname = Read-Host 'Enter New Users Windows Logon Name'
$EmailAddress = "$logonname@mine.com"
$Description = Read-Host 'Job Title?'
$Landline = Read-Host 'Enter the Users Landline Phone (if applicable)'
$Mobile = Read-Host 'Mobile Phone? (If Applicable)'
$Password = Read-Host 'Please enter a secure password'


New-ADUser -Name "$firstname $lastname" -EmailAddress $EmailAddress -DisplayName "$firstname $lastname" -SamAccountName $logonname -Title $Description -UserPrincipalName "$logonname@mine.com" -GivenName $firstname -Surname $lastname -Description $Description -OfficePhone $Landline -MobilePhone $Mobile

Write-Host 'Setting Account Details...'

Write-Host 'Setting Password...'
Set-ADAccountPassword -Identity $logonname -NewPassword (ConvertTo-SecureString -AsPlainText "$Password" -Force)
Set-ADUser -Identity $logonname -PasswordNeverExpires $true

Write-Host 'Setting Home Directory to \networklocationHome'
Set-ADUser -Identity $logonname -HomeDirectory \networklocationhome -HomeDrive H

Write-Host 'Enabling Account in Active Directory..
**NOTE**
The Account Will Take 30 Seconds To Enable, It Is Safe To Close The Script As Long as you See the "DONE" Prompt.'
Enable-ADAccount -Identity $logonname

Write-Host 'Done'

如果您想更新脚本,使$logonname使用$firstname$lastname中的值,那么您可以通过几种不同的方式来执行此操作。

我也在评论中看到,你想要一个介于两者之间的.

我会这样做:

$logonname = "$firstname.$lastname";

顺便说一句,我个人没有AD的经验,然而,允许用户键入他们想要的任何内容,然后使用这些信息可能会导致问题。

如果用户在用户名或电子邮件地址中键入了不允许的符号,该怎么办?

如果你是唯一一个使用这个脚本的人,而你写这个脚本只是为了让你的工作更容易,那么这可能没关系。但我会对允许其他人运行它持谨慎态度

如果这些依赖于上一个命令的命令中的任何一个失败,会发生什么?

我知道你说过你在学习。因此,这是一个学习错误处理、检查输入并验证其有效性等的好机会。

最新更新