【问题标题】:Move AD User using powershell使用 powershell 移动 AD 用户
【发布时间】:2016-11-11 14:45:08
【问题描述】:

我知道我可以使用 powershell 移动 AD 用户。我想要完成的是根据他们的描述移动一群用户。我有一个 csv 文件,在那个 csv 中,他们是一年的毕业专栏。我希望所有在 2016 年至 2022 年期间获得 YOG 的用户都转移到高中 OU。

我还没有尝试编写代码。我在 powershell 中成功地根据部门而不是描述来获取用户帐户。这是一些相同的数据

"ID","FNAME","LNAME","BDATE","GRD","SCHID"
"111111","TEst","student1","19980601","2016","1480"
"222222","test","Student2","19980522","2017","1480"
"333333","test","Student3","19970813","2025","1479"

我已将学校代码添加到 csv 文件中。我认为根据这个文件将学生移动到正确的 ou 会容易得多。 1480 是元素,1479 小时。这也是我用来创建 AD 帐户的代码。

# Import active directory module for running AD cmdlets
Import-Module activedirectory

#Store the data from ADUsers.csv in the $ADUsers variable
$ADUsers = Import-csv userimport.csv
#Store report in log file in the $log variable
$log = "log.txt"

#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

$Username   = $User.ID
$Password   = $User.BDATE
$Firstname  = $User.FNAME
$Lastname   = $User.LNAME
$Department = $User.GRD
$Company    = $User.SCHID #This field refers to the OU the user account is to be moved to

#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
{
    #User does not exist then proceed to create the new user account

    "Processing started (on " + $date + "): " | Out-File $log -append
    "--------------------------------------------" | Out-File $log -append

    #Account will be created in the OU provided by the $OU variable read from the CSV file
    New-ADUser `
        -SamAccountName $Username `
        -UserPrincipalName "$Username@clasd.net" `
        -Name "$Firstname $Lastname" `
        -GivenName $Firstname `
        -Department "$Department" `
        -Company "$Company" `
        -EmailAddress "$Username@clasd.net" `
        -Surname $Lastname `
        -Enabled $True `
        -Scriptpath "login.vbs" `
        -DisplayName "$Firstname $Lastname" `
        -Path "ou=users,ou=hs,dc=clasd,dc=net" `
        -AccountPassword (convertto-securestring $Password -AsPlainText -Force) `
        -ChangePasswordAtLogon $true

 # Add User to Groups
 #Add-ADPrincipalGroupMembership -Identity $Username -MemberOf "Elem","Elem Students"
  Start-Sleep 3

 # Move Users to appropiate OU based on School Code

  $usr = import-csv userimport.csv

  foreach ($User in $usr) {
  if ($user.grd -in 2016){
        Get-ADUser $User.ID | Move-ADObject -TargetPath    'OU=users,ou=hs,dc=clasd,dc=net'
    }
  }
 }
}

【问题讨论】:

  • 您好,能否提供一些您尝试编写的代码和一个示例 csv 文件?
  • 我不担心同名学生。如果他们确实有相同的名字,我只需查看他们用于登录帐户的 ID 号。所有用户名都是他们的身份证号码。我只想在创建学生时将他们移动到正确的 OU 中。
  • AD 用户名的格式是什么?它们与 CSV 中的数据有何对应关系? YOG 是否已存储在 AD 属性中?
  • YOG 存储在 AD 用户的描述中。

标签: powershell active-directory


【解决方案1】:

由于他们的 AD 用户名是唯一的并且已包含在您的 CSV 中,因此只需检查 GRD 字段是否在 2016-2022 范围内,然后使用 ID 字段移动帐户:

$filepath = "C:\path\to\data.csv"

$csv = Import-CSV $filepath
foreach ($user in $csv) {
    if ($user.GRD -in 2016..2022) {
        Get-ADUser $user.ID | Move-ADObject -TargetPath 'OU=High School,DC=domain,Dc=com'
    }
 }

编辑:没有看到您关于 YOG 是描述字段的评论,而我使用了 GRD,如果这不正确,请告诉我?


EDIT2:我上面的答案将在创建每个帐户后而不是在您现有的脚本期间运行,在创建时将帐户放在正确的 OU 中更有效,如下所示:

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

    $Username   = $User.ID
    $Password   = $User.BDATE
    $Firstname  = $User.FNAME
    $Lastname   = $User.LNAME
    $Department = $User.GRD
    $Company    = $User.SCHID #This field refers to the OU the user account is to be moved to

    # Choose OU
    Switch ($Department)
    {
        "2016" {$OU = 'OU=users,ou=hs,dc=clasd,dc=net'}
        "2017" {$OU = 'OU=2017,OU=users,ou=hs,dc=clasd,dc=net'}
    }

    #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
    {
        #User does not exist then proceed to create the new user account
        "Processing started (on " + $date + "): " | Out-File $log -append
        "--------------------------------------------" | Out-File $log -append

        #Account will be created in the OU provided by the $OU variable read from the CSV file
        New-ADUser `
            -SamAccountName $Username `
            -UserPrincipalName "$Username@clasd.net" `
            -Name "$Firstname $Lastname" `
            -GivenName $Firstname `
            -Department "$Department" `
            -Company "$Company" `
            -EmailAddress "$Username@clasd.net" `
            -Surname $Lastname `
            -Enabled $True `
            -Scriptpath "login.vbs" `
            -DisplayName "$Firstname $Lastname" `
            -Path $OU `
            -AccountPassword (convertto-securestring $Password -AsPlainText -Force) `
            -ChangePasswordAtLogon $true

        # Add User to Groups
        #Add-ADPrincipalGroupMembership -Identity $Username -MemberOf "Elem","Elem Students"
        Start-Sleep 3
    }
}

【讨论】:

  • 我只是想在创建学生时将他们移动到正确的 OU 中我认为尚未创建用户帐户。
  • 遗憾的是,我们没有用于创建帐户的代码,当我们有事情要做时更容易为您提供帮助。
  • +James C 您的代码似乎不起作用。它不会移动用户。我修改它以查看 user.schid 并根据该值移动。我更新了原始帖子,以便您可以看到我的创建用户脚本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-29
  • 2020-04-02
  • 2015-08-10
  • 1970-01-01
相关资源
最近更新 更多