
[英]How can i Filter “OU=Service account” from column DistinguishedName from a CSV file attached : powershell
[英]How can I use powershell to retrieve AD distinguishedName from the employeeID only?
我要执行的操作是运行一个脚本,该脚本将员工ID从CSV文件与AD进行比较,如果它们不在CSV中但在AD中,则应:-禁用-将终止日期注释添加到说明-移至其他OU
我在下面使用的脚本禁用了该帐户并添加了注释,但是当它尝试移动到其他OU时出现错误。 错误是:Move-ADObject:找不到标识为“ name1test”的对象...
我已经尝试了很多事情来调整脚本,以仅使用employeeID来获取samAccountName或distinguishedName,但是我没有运气。 有任何想法吗?
Import-Module ActiveDirectory
$TargetOU = "ou=Term,ou=Logins,dc=domain,dc=com"
$Date = Get-Date -Format MM-dd-yyyy
$Users = Import-Csv c:\ADTerm.csv | Select-object -ExpandProperty EmployeeID
$Terms = Get-ADUser -filter * -SearchBase "ou=Test,ou=Logins,dc=domain,dc=com" -Properties EmployeeID | Where-Object{$_.EmployeeID -and ($Users -notcontains $_.EmployeeID)}
ForEach ($Term in $Terms)
{
# Retrieve user sAMAccountName.
$Name = $Term.sAMAccountName
# Disable the user.
Set-ADUser -Identity $Name -Enabled $False -Description "Terminated - $Date"
# Move the user.
Move-ADObject -Identity $Name -TargetPath $TargetOU
}
可分辨名称是获得AD用户时会自动检索的默认属性之一,因此只需替换以下内容即可:
$Name = $Term.sAMAccountName
有了这个:
$Name = $Term.DistinguishedName
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.