【问题标题】:Create Active Directory users with certain attributes in a script在脚本中创建具有特定属性的 Active Directory 用户
【发布时间】:2014-11-19 08:37:48
【问题描述】:

我正在编写一个 PowerShell 脚本,以便更轻松地创建包含某些属性的 Active Directory 用户。现在我需要处理“经理”字段。我们有4个部门,4个经理。我想使用类似“如果部门是 4 个部门中的 1 个,则给 4 个经理中的 1 个”这样的逻辑来创建它。

我该如何处理?

Import-Module activedirectory

#Store the data from ADUsers.csv in the $ADUsers variable
$ADUsers = Import-csv "c:\temp\scripts\ADUsers.csv"

#Loop through each row containing user details in the CSV file 
foreach ($User in $ADUsers)
{
    #Read user data from each field in each row and assign the data to a variable as below

    $Firstname  = $User.firstname
    $Lastname   = $User.lastname
    $Username   = $User.username
    $Password   = $User.password
    $OU         = $User.ou #This field refers to the OU the user account is to be created in
    $userprincipalname = $user.userprincipalname
    $displayname = $user.displayname
    $title = $user.jobtitle
    $department = $user.department

    #Check to see if the user already exists in AD
    if (Get-ADUser -F {SamAccountName -eq $Username})
    {
         #If user does exist, give a warning
         Write-Warning "A user account with username $Username already exist in Active Directory."
    }
    else
    {
        Write-host 
        #User does not exist then proceed to create the new user account
        #Account will be created in the OU provided by the $OU variable read from the CSV file
    New-ADUser -Name $displayname -SamAccountName $Username -UserPrincipalName $userprincipalname  -GivenName $Firstname -Surname $Lastname -Enabled $True -DisplayName $displayname -AccountPassword (convertto-securestring $Password -AsPlainText -Force) 
    }
} #end function

【问题讨论】:

    标签: if-statement active-directory powershell-ise


    【解决方案1】:

    这样的?

    if ($department -eq "A")
    {
        $manager = "ManagerA"
    }
    else if ($department -eq "B")
    {
        $manager = "ManagerB"
    }
    else if ($department -eq "C")
    {
        $manager = "ManagerC"
    }
    else if ($department -eq "D")
    {
        $manager = "ManagerD"
    }
    
    New-ADUser -Name $displayname -SamAccountName $Username -UserPrincipalName $userprincipalname  -GivenName $Firstname -Surname $Lastname -Enabled $True -DisplayName $displayname -AccountPassword (convertto-securestring $Password -AsPlainText -Force) -Manager $manager
    

    “ManagerA/B/C/D”是管理员用户对象的SAM账户名。

    【讨论】:

      【解决方案2】:

      我已经用下面的 Switch 解决了这个问题。

      Switch ($department) {
          "dept 1" {$manager = "cn=jim smith,ou=west,dc=domain,dc=com"}
          "dept 2" {$manager = "cn=sally wilson,ou=east,dc=domain,dc=com"}
          "dept 3" {$manager = "cn=frank johnson,ou=east,dc=domain,dc=com"}
          "dept 4" {$manager = "cn=mary tutle,ou=west,dc=domain,dc=com"}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多