Powershell 如何在新aduser中跳过空值
您好,我编写的程序将从powershell gui创建用户,但我遇到问题: 有时并非所有字段都有值,并且它们都是空的,但当我尝试使用具有空值的参数创建用户时,我得到了一个错误Powershell 如何在新aduser中跳过空值,powershell,Powershell,您好,我编写的程序将从powershell gui创建用户,但我遇到问题: 有时并非所有字段都有值,并且它们都是空的,但当我尝试使用具有空值的参数创建用户时,我得到了一个错误 new-aduser server is unwilling to process the request 我可以为每个字段使用许多if-else块,但我认为这不是一个好的解决方案。请帮助我用空参数传递create user New-ADUser -AccountPassword (ConvertTo-Secur
new-aduser server is unwilling to process the request
我可以为每个字段使用许多if-else块,但我认为这不是一个好的解决方案。请帮助我用空参数传递create user
New-ADUser -AccountPassword (ConvertTo-SecureString -AsPlainText -Force -String ($Password.Text)) `
-GivenName $UserName.Text `
-Company $company.Text `
-EmployeeID $Tabel.Text `
-HomePhone $homephone.Text `
-MobilePhone $mobilephone.Text `
-Enabled $true `
-SamAccountName $Login.Text `
-StreetAddress $street.Text `
-Surname $Surname.Text `
-Title $JobPost.Text `
-UserPrincipalName ($Login.Text + "@domain.com") `
-OfficePhone $phone.Text `
-Office $office.Text `
-Description $SZnumb.Text `
-City $City.Text `
-Department $Departament.Text `
-Division $Division.Text `
-Name ($Surname.Text + " " + $UserName.Text + " " + $FatherName.Text) `
-DisplayName ($Surname.Text + " " + $UserName.Text + " " + $FatherName.Text) `
-Path $OUpicker.Text `
-PasswordNeverExpires $passneverexpires `
-ChangePasswordAtLogon $changepassatlogon `
-CannotChangePassword $cantchangepassword `
-OtherAttributes @{ 'employeeType' = $LevelRuler.Text; 'extensionAttribute4'=$Sektor.Text}
更新
参数-OtherAttributes有问题
如果这两个字段为空,则为抛出错误的建议听起来不错。
您可以使用类似单选按钮的功能来定义您正在处理的帐户类型,并在此基础上禁用或隐藏一些输入框
无论如何,我认为你真的应该考虑转换使用飞溅。这使得代码更具可读性和可维护性(不再有令人讨厌的反勾号)并且您可以根据特定条件添加或删除属性
比如:
# build a Hashtable object for splatting, at first with properties all account types have in common
# for instance:
$userParams = @{
AccountPassword = (ConvertTo-SecureString -AsPlainText -Force -String ($Password.Text))
GivenName = $UserName.Text
Company = $company.Text
EmployeeID = $Tabel.Text
HomePhone = $homephone.Text
MobilePhone = $mobilephone.Text
Enabled = $true
SamAccountName = $Login.Text
StreetAddress = $street.Text
Surname = $Surname.Text
Title = $JobPost.Text
UserPrincipalName = ($Login.Text + "@atbmarket.com")
Description = $SZnumb.Text
City = $City.Text
Department = $Departament.Text
Division = $Division.Text
Name = ($Surname.Text + " " + $UserName.Text + " " + $FatherName.Text)
DisplayName = ($Surname.Text + " " + $UserName.Text + " " + $FatherName.Text)
Path = $OUpicker.Text
PasswordNeverExpires = $passneverexpires
ChangePasswordAtLogon = $changepassatlogon
CannotChangePassword = $cantchangepassword
}
现在,您可以根据帐户类型添加某些额外属性。例如:
switch ($LevelRuler.Text) {
'Manager' {
$userParams['OtherAttributes'] = @{'employeeType' = 'Manager'; 'extensionAttribute4'=$Sektor.Text}
}
'FloorWalker' {
$userParams['OtherAttributes'] = @{'employeeType' = 'FloorWalker' }
}
# etc.
}
仅当其他常用属性不为空时,才添加它们:
if (!([string]::IsNullOrWhiteSpace($phone.Text))) {
$userParams['OfficePhone'] = $phone.Text.Trim()
}
if (!([string]::IsNullOrWhiteSpace($office.Text))) {
$userParams['Office'] = $office.Text.Trim()
}
# etc.
最后,创建新用户:
New-ADUser @userParams
这是打字错误吗<代码>$department.Text
p.S.2我不想做两次
($names.Text++$UserName.Text++$FatherName.Text)
,而是先创建一个变量$name,比如$name=('{0}{1}{2}'-f$names.Text、$UserName.Text、$FatherName.Text)。Trim()-替换'\S+','
,并将其用于属性name和DisplayName你考虑过吗?禁用“确定”按钮,并仅在填写所有必填字段后启用它。某些帐户类型没有必填字段数据。您必须定义可接受的用例。也许一个下拉列表或单选按钮会选择帐户类型,并根据帐户类型选择适当的输入验证选项。