[英]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.