简体   繁体   English

Powershell-禁用用户并将其移动到新的OU

[英]Powershell - Disable and move users to a new OU

brand new member here.. 全新会员在这里..

I'm trying to create a powershell script that can create a new OU with the current date (dd-MM-yyyy), disable users from a text file and then move them to the newly created OU. 我正在尝试创建一个Powershell脚本,该脚本可以使用当前日期(dd-MM-yyyy)创建一个新的OU,从文本文件中禁用用户,然后将其移动到新创建的OU中。

So far I have gotten everything but the move to work. 到目前为止,除了工作之外,我已经获得了一切。 I've read that it might be because the text file contains the users sAMAccountName , which doesn't work with Move-ADObject ? 我已经读到它可能是因为文本文件包含用户sAMAccountName ,而该用户不适用于Move-ADObject吗?

Just for the record I'm pretty new to scripting in general, and I know the last line is completely off. 就记录而言,我一般来说对脚本编写还不太陌生,而且我知道最后一行完全没有了。 Everything has been composed of stuff I have found online. 一切都由我在网上找到的东西组成。

Code: 码:

$OU = "$((get-date).toString('dd-MM-yyyy'))"
$PathOU = "OU=DEPARTURES,OU=IT,OU=USERS,OU=DK,DC=xxx"

New-ADOrganizationalUnit $OU -ProtectedFromAccidentalDeletion $false -Path $PathOU

$CN = get-content "\\Server\User Administration\User Deletion\UsersToBeDisabled.txt"
$CN |Foreach {
Get-ADUser $_ | Disable-ADAccount
Move-ADObject -Identity $_ -TargetPath $OU
}

Error: 错误:

Move-ADObject : Cannot find an object with identity: 'firstname.lastname' under: 'DC=xxx'. Move-ADObject:在“ DC = xxx”下找不到标识为“ firstname.lastname”的对象。 ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.MoveADObject** ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.MoveADObject **

I hope you have some ideas on how to get this to work, thanks! 希望您对如何使用它有所帮助,谢谢!

BR. BR。 Mik MIK

You could use the -PassThru switch with Disable-ADAccount to hand your user object along the pipline to Move-ADObject 您可以将-PassThru开关与Disable-ADAccount一起使用,以Move-ADObject用户对象沿画线Move-ADObject

Get-ADUser $_ | Disable-ADAccount -PassThru | Move-ADObject -TargetPath "OU=$OU,$PathOU"

Alternative this should work as well: 另外,这也应该起作用:

$user = Get-ADUser $_ 
$user | Disable-ADAccount 
$user | Move-ADObject -TargetPath "OU=$OU,$PathOU"

Couple mistakes here; 这里有几个错误;

1- you're trying to use the entire AD object for the -identity flag: instead of $_ try using $_.samaccountname. 1-您正在尝试将整个AD对象用于-identity标志:而不是$ _,请尝试使用$ _。samaccountname。

2- you're trying to move-adobject to $ou which is merely a string. 2-您正在尝试将adobject移至$ ou,它只是一个字符串。 I'm assuming your new-adOrganizationalUnit command works; 我假设您的new-adOrganizationalUnit命令有效; so add a line after that to say "$newOU = Get-ADorganizationalunit" -path [whatever] 因此,请在其后添加一行以表示“ $ newOU = Get-ADorganizationalunit” -path [whatever]

Sorry I don't have time to type out & test :( 抱歉,我没有时间输入和测试:(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将用户列表从一个 OU 移动到另一个 OU 并同时禁用它们 - Powershell - Move list of users from one OU to a different OU and disable them at the same time - Powershell 在PowerShell中将用户移动到另一个没有CSV的OU - Move Users in PowerShell to another OU without CSV 用于禁用 OU 中过期用户的 Powershell 脚本 - Powershell script to disable expired users within an OU Powershell根据用户主要组将计算机移至OU - Powershell move computer to OU depending on users primary group 按员工编号禁用csv文件中的用户,然后进入某个OU - Disable Users in a csv file by employee number and then move into certain OU 使用Powershell将计算机移至SCCM 2012任务序列中的新OU - Move Computer to new OU in SCCM 2012 Task Sequence with Powershell Powershell删除OU中的所有用户 - Powershell delete all users in OU 在Powershell中将用户移动到禁用的OU - Moving users to a disabled OU in Powershell Powershell - Active Directory - 根据员工 ID 和日期的 CSV 文件将用户移动到 OU - Powershell - Active Directory - Move users to an OU based on CSV file for employeeID and Date 如何使Powershell遍历用户列表,如果存在,则将其移动到提到的ou(如果未显示失败的用户名)。 - How to have powershell go through a list of users and if they exists move them to ou mentioned if not display the username with failed.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM