简体   繁体   中英

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.

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

The following will collect all your computer which match WEB230* and assign them to variable $c

$c =  Get-AdComputer -Filter { CN -like "WEB230*"}

Moves all found computers to target 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.

This would be the command I use for moves, from a collection of machines already built in a CSV file:

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. 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. Then I told it to move the computer to that 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM