[英]How do i search AD for names like the ones in the “Computers” OU and then move those computers over to their proper location?
I would like to search Active Directory for a computer with a name like WEB2309 that is currently in "Computers" OU then move it to where other computers have like names. 我想在Active Directory中搜索名称为WEB2309的计算机,该计算机当前位于“计算机” OU中,然后将其移至其他计算机具有相同名称的位置。
This what i have currently: 这是我目前拥有的:
Takes First member of Computers group 成为计算机组的第一位成员
$strFirstMember = get-adgroupmember "Computers"
Cuts off the last number of the first member 切断第一个成员的最后一个号码
$strFirstMember-1 = $strFirstMember.Substring(0,$strFirstMember.Length-1)
Searches for that on AD with a wildcard on the end to find like names 在广告末尾搜索通配符以查找类似名称
Get-ADObject -Filter { CN -like "$strFirstMember-1*" ObjectClass = "Computer"}
Now i need to move firstmember to the location that is found 现在我需要将firstmember移到找到的位置
The following will collect all your computer which match WEB230* and assign them to variable $c 以下内容将收集与WEB230 *匹配的所有计算机,并将它们分配给变量$ c
$c = Get-AdComputer -Filter { CN -like "WEB230*"}
Moves all found computers to target OU 将找到的所有计算机移动到目标OU
$c | Move-ADObject -TargetPath "ou=YourOu,dc=domain,dc=com"
and this will move just the first one, I'm not sure if this is what you mean by "move firstmember.." 并且这将仅移动第一个成员,我不确定这是否就是您所说的“移动第一成员”。
$c[0] | Move-ADObject -TargetPath "ou=YourOu,dc=domain,dc=com"
I personally prefer Quest Active Directory Cmdlets for AD scripting in Powershell. 我个人更喜欢Quest Active Directory Cmdlet在Powershell中进行AD脚本编写。
This would be the command I use for moves, from a collection of machines already built in a CSV file: 这将是我用于移动的命令,来自一组已内置CSV文件的机器:
Import-Csv "C:\toMove.csv" |`
ForEach-Object {
Get-QADComputer $_.Name |`
Move-QADObject -NewParentContainer "OU=Computers,OU=Locked Computers,DC=com"
}
In one step, you should be able to do something like this too: 第一步,您也应该可以执行以下操作:
$comparrisonPC = get-qadComputer "computerToUse"
get-qadComputers -Name "nameStructure*" |`
Where-Object {$_.CN -not $comparrisonPC.ParentContainerDN} |`
Move-QADObject -NewParentContainer "$comparrissonPC.ParentContainerDN"
I don't have a test domain to test it on, but it should work... 我没有要对其进行测试的测试域,但是它应该可以工作...
Building off what you already have, we get the computer, we strip the last character off of it, and then we get a list of all computers with a similar name. 利用已有的资源,获得计算机,从中删除最后一个字符,然后获得所有具有相似名称的计算机的列表。 Once we have that list I took a substring of it based off the DistinguishedName property, stripping the name out of it just leaving the OU path. 一旦有了该列表,我就根据DistinguishedName属性获取了它的子字符串,将其剥离出来只是留下了OU路径。 Then I grouped that, sorted the groupings by count to find the OU with the most computers in it with that similar name, and selected the first one. 然后,我将其分组,按计数对分组进行排序,以找到名称相同的计算机中拥有最多计算机的OU,然后选择第一个。 Then I told it to move the computer to that OU. 然后,我告诉它将计算机移至该OU。
$strFirstMember = get-adgroupmember "Computers"
$strFirstMember-1 = $strFirstMember.Substring(0,$strFirstMember.Length-1)
$Complist = Get-ADComputer -Filter { CN -like "$strFirstMember-1*" }
$NewOU = ($complist|%{$_.DistinguishedName.substring($_.DistinguishedName.indexof(",")+1,$_.DistinguishedName.length-$_.DistinguishedName.indexof(",")-1)}|group|sort count -Descending|select -first 1).name
Move-ADObject $strFirstMember -TargetPath $NewOU
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.