[英]How to move multiple AD Users into multiple OUs with powershell script?
I want to move multiple users from input file to multiple OU's.我想将多个用户从输入文件移动到多个 OU。 But the code below keeps giving me error:
但是下面的代码不断给我错误:
$i=0
$tempOUsArray = @{}
$tempOUsArray = Import-CSV -path "c:\temp\OUs.txt" #this file contains Full DN of OUs
$UsersToBeMoved = Import-CSV -path "c:\temp\Users.txt #this file contain SamAccountName field of users
$UsersToBeMoved | ForEach-Object{
$tempUsers = (get-aduser -identity $_.Name).distinguishedName
#assign each OU to temp holder
$TempOU = $tempOUsArray[$i]
Move-ADObject -Identity $tempUsers -TargetPath $TempOU
$i++ #inclement position of array
}
(See edits for previous answer attempts, clearing to reduce answer size) (请参阅先前答案尝试的编辑,清除以减小答案大小)
See the below.见下文。 I've tested this on a setup I have and this successfully moved the objects without an issue.
我已经在我拥有的设置上对此进行了测试,并且成功地移动了对象而没有问题。
Users.txt (CSV format, matching what I believe yours is) Users.txt(CSV 格式,与我认为的匹配)
samaccountname,name
tst-user1,user1
tst-user2,user2
tst-user3,user3
tst-user4,user4
# (From OP: My users input file is)
# john.doe
# ann.bill
# jose.love
OUs.txt (no quotation marks in txt file, though may need if there's any spaces in the DN) OUs.txt(txt 文件中没有引号,但如果 DN 中有空格可能需要)
OU=test,OU=Accounts,DC=domain,DC=int
OU=test,OU=Accounts,DC=domain,DC=int
OU=test,OU=Accounts,DC=domain,DC=int
OU=test,OU=Accounts,DC=domain,DC=int
PS script PS脚本
$i = 0
$OUs = @()
$OUs = Get-Content -Path C:\temp\OUs.txt
$Users = Import-Csv -Path C:\temp\Users.txt
$users | ForEach-Object {
$tempUser = (Get-ADUser -Identity $($_.samaccountname)).DistinguishedName
$tempOU = $OUs[$i]
Write-Host "Moving User: [$tempUser]"
Write-Host " to OU: [$tempOU]"
Move-ADObject -Identity $tempUser -TargetPath $tempOU
$i++
}
Output Output
Moving User: [CN=user1,OU=source,OU=test,OU=Accounts,DC=domain,DC=int]
to OU: [OU=test,OU=Accounts,DC=domain,DC=int]
Moving User: [CN=user2,OU=source,OU=test,OU=Accounts,DC=domain,DC=int]
to OU: [OU=test,OU=Accounts,DC=domain,DC=int]
Moving User: [CN=user3,OU=source,OU=test,OU=Accounts,DC=domain,DC=int]
to OU: [OU=test,OU=Accounts,DC=domain,DC=int]
Moving User: [CN=user4,OU=source,OU=test,OU=Accounts,DC=domain,DC=int]
to OU: [OU=test,OU=Accounts,DC=domain,DC=int]
And all worked properly, no errors.一切正常,没有错误。 Can you try the above code, and ensure your source files are in a similar format?
你能试试上面的代码,并确保你的源文件是类似的格式吗?
With the provided edit / update to the "users.txt" file, the below script should work as expected.通过提供对“users.txt”文件的编辑/更新,下面的脚本应该可以按预期工作。
New users.txt file (note: no column header)新 users.txt 文件(注意:没有列标题)
john.doe
ann.bill
jose.love
PS script PS脚本
$i = 0
$OUs = @() # May as well clear, in case old data exists
$Users = @() # May as well clear, in case old data exists
$OUs = Get-Content -Path C:\temp\OUs.txt
$Users = Get-Content -Path C:\temp\Users.txt
$users | ForEach-Object {
$tempUser = (Get-ADUser -Identity $($_)).DistinguishedName
$tempOU = $OUs[$i]
Write-Host "Moving User: [$tempUser]"
Write-Host " to OU: [$tempOU]"
Move-ADObject -Identity $tempUser -TargetPath $tempOU
$i++
}
It is with the Move-ADObject
.它与
Move-ADObject
一起使用。 This function will NOT work with moving parts like multiple OUs assignment.此 function 不适用于多个 OU 分配等移动部件。 My revised code is as below:
我修改后的代码如下:
$i=0
$tempOUsArray = @{}
$tempOUsArray = get-content -path "c:\temp\OUs.txt" #this file contains Full DN of
OUs
$UsersToBeMoved = get-content -path "c:\temp\Users.txt" #this file contain
SamAccountName field of users
ForEach ($tuser in $UsersToBeMoved){
$tempUser = dsquery user -samid $tuser
#assign each OU to temp holder
$TempOU = $tempOUsArray[$i]
dsmove $tempUser -newparent $TempOU
$i++ #inclement position of array
}
$tempOUArray.Clear()
$TempOU=''
$tempUser=''
$tuser=''
Note: Remove All Header from input files.注意:从输入文件中删除所有 Header。 OUs.txt with full DN in double-quotes.
OUs.txt 带有双引号中的完整 DN。
Users.txt
file contains only the SamAccountName without quotes. Users.txt
文件仅包含不带引号的 SamAccountName。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.