正如我在上一个问题中所提到的,我是PowerShell的新手,并且拼凑了一些用于创建新用户的Frankenstein脚本。它适用于创建用户(使用new-aduser),但是当我尝试添加' new-mailbox' cmdlet,它会创建一个' sytems.collections.hashtable'已创建,脚本错误。我认为它与邮箱cmdlet没有处理哈希表有关。有没有办法将哈希表传递给新邮箱和管道到set-aduser,或者我是否尝试过无法完成的事情?有没有更简单的方法来完成我想要做的事情?我目前正在使用Powershell ISE和我的Exchange服务器。任何建议/提示/批评都是受欢迎的,非常感谢。
Function Provision { PROCESS { CreateUser $_ } }
Function ProvisionInputCSV {
Param ([string]$filename)
$users = Import-CSV $filename
foreach ($user in $users) {
$ht = @{'givenName'=$user."First Name";
'sn'= $user."Last Name";
'displayName'= $user."Last Name" + " ," + " " + $user."First Name";
'samAccountName'= $user."Logon Name";
'title'= $user."title";
'description'= $user."Description";
'department'= $user."Department";
'company'= $user."Company";
'password'= ConvertTo-SecureString -AsPlainText $user.Password -force;
'userprincipalname'= $user.userprincipalname;
'employeeid'= $user.employeeid
}
Write-Output $ht
}
}
Function CreateUser { Param($user) New-Mailbox $user -Database "StaffDB1" -alias $user.samaccountname -password $user.password -UserPrincipalName $user.userprincipalname -OrganizationalUnit "ou=users,dc=mydomain,DC=com" -ResetPasswordOnNextLogon $true -DisplayName $user.displayname | Set-Mailbox -CustomAttribute5 "AA" | Set-ADUser -GivenName $user.givenname -Surname $user.surname -DisplayName $user.displayname -EmployeeID $user.employeeid -Description $user.description -Department $user.department -Company $user.company -UserPrincipalName $user.userprincipalname -Enabled $true}
ProvisionInputCSV c:\users\newusers.csv | Provision
答案 0 :(得分:1)
因此,在查看我的代码并使用set-aduser,new-mailbox,set-mailbox等之后,我终于能够解决我的问题了 - 创建一个具有特定AD属性的新用户并通过邮件启用它。下面是我的最终代码,似乎对任何感兴趣的人都有效。感谢Patrick对第二位的帮助。
#Create New User Accounts
Function Provision { PROCESS { CreateUser $_ } }
Function ProvisionInputCSV {
Param ([string]$filename)
$users = Import-CSV $filename
foreach ($user in $users) {
$ht = @{'givenName'=$user."First Name";
'sn'= $user."Last Name";
'displayName'= $user."Last Name" + " ," + " " + $user."First Name";
'samAccountName'= $user."Logon Name";
'title'= $user."title";
'description'= $user."Description";
'department'= $user."Department";
'company'= $user."Company";
'password'= ConvertTo-SecureString -AsPlainText $user.Password -force;
'upn'= $user.userprincipalname;
'employeeid'= $user.employeeid
}
Write-Output $ht
}
}
Function CreateUser { Param($user) New-ADUser –name ($user['sn'] + ',' + ' ' + $user['givenname']) -givenname $user.givenname -surname $user.sn -DisplayName $user.displayname -SamAccountName $user.samaccountname -UserPrincipalName $user.upn -employeeid $user.employeeid -Description $user.description -title $user.title -Department $user.department -Company $user.company -accountpassword $user.password -path 'ou=Students,ou=Business Users,DC=mydomain,DC=com' -ChangePasswordAtLogon $true -enabled $true}
ProvisionInputCSV c:\filepath\newusers.csv | Provision
#Enable mailboxes for Existing Users
$Mailboxes = Import-CSV c:\filepath\newusers.csv
ForEach ($Mailbox In $Mailboxes) { Enable-Mailbox -identity ($mailbox."Logon Name") -DisplayName ($Mailbox."Last Name" + " ," + " " + $Mailbox."First Name") -Database "MyDB1" -alias ($mailbox."Logon Name") | Set-Mailbox -CustomAttribute5 "AA" }
答案 1 :(得分:0)
您创建一个哈希表并将此管道传递给该命令。但是在命令中,您不使用哈希表中的属性($ _。givenname),而是使用csv($ user.givenname)中的数据属性。我认为这是一个真相。你能试试这个片段吗?我把代码分开了。
$users = Import-CSV $filename
foreach ($user in $users) {
$ht = @{
'givenName'=$user."First Name";
'sn'= $user."Last Name";
'displayName'= $user."Last Name" + " ," + " " + $user."First Name";
'samAccountName'= $user."Logon Name";
'title'= $user."title";
'description'= $user."Description";
'department'= $user."Department";
'company'= $user."Company";
'password'= ConvertTo-SecureString -AsPlainText $user.Password -force;
'userprincipalname'= $user.userprincipalname;
'employeeid'= $user.employeeid
}
#Do exchange things
New-Mailbox -Database "StaffDB1" -alias $ht.samaccountname -password $ht.password -UserPrincipalName $ht.userprincipalname`
-OrganizationalUnit "ou=users,dc=mydomain,DC=com" -ResetPasswordOnNextLogon $true -DisplayName $ht.displayname | Set-Mailbox -CustomAttribute5 "AA"
#Do ad things
Set-ADUser -GivenName $ht.givenname -Surname $ht.surname -DisplayName $ht.displayname`
-EmployeeID $ht.employeeid -Description $ht.description -Department $ht.department -Company $ht.company -UserPrincipalName $ht.userprincipalname -Enabled $true
}