Group computers base on their IP addresses and move them to specific OUs.

nav2567
nav2567 used Ask the Experts™
on
Hi,

I need to move all computers with the same first three octects in their IP addresses together from the computer OU to another OU.  

Is it possible to have a PS script to do it?   Or a way to display the IP addresses in ADUC and move it?

Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Sam Simon NasserIT Support Professional
Commented:
you can do this by using Power Shell, as described here
http://blogs.technet.com/b/heyscriptingguy/archive/2011/12/03/use-powershell-to-move-computers-based-on-ip-addresses.aspx

Now the fun part! Because will capture the IP information as a string and not as an integer, this makes it a bit more challenging to figure out what subnet we are in. This example has three subnets: 192.168.1.0/24, 192.168.2.0/24, and 192.168.3.0/24. I have chosen class C subnets for this script to match my structure, but you may have to get more creative if you have a more complex network configuration.

We will define our IP range variables as regular expressions (or Regex as they are commonly known), so that we can match the characters appropriately. Sorry kids, but it is goodbye GUI and hello Regex for this stuff.

$Site1IPRange = "\b(?:(?:192)\.)" + "\b(?:(?:168)\.)" + "\b(?:(?:1)\.)" + "\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))" # 192.168.1.0/24

$Site2IPRange = "\b(?:(?:192)\.)" + "\b(?:(?:168)\.)" + "\b(?:(?:2)\.)" + "\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))" # 192.168.2.0/24

$Site3IPRange = "\b(?:(?:192)\.)" + "\b(?:(?:168)\.)" + "\b(?:(?:3)\.)" + "\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))" # 192.168.3.0/24
Top Expert 2014

Commented:
While I'm a fan of regex for many things, I don't see a benefit in this case and it's easier to just use something like the following:
if ($ip -like "192.168.1.*")
{  #move the computer }
elseif ($ip -like "192.168.2.*")
{  #move the computer }
elseif ($ip -like "192.168.3.*")
{  #move the computer }

Open in new window

This isn't to say that the code mentioned before won't work though.

One note in particular - this relies on your DNS records being accurate, otherwise you will get some weird results.
Justin YeungSenior Systems Engineer
Commented:
$sourceOu = " your ou"
$destinationOu = "your destination OU'
$destinationOu1 = "your destination OU'
$destinationOu2 = "your destination OU'


$computers = get-adcomputer -filter * -searchbase $sourceou
foreach ($computer in $computers)
{
$objcomputer = test-connection -computername $computer -count 1
$ipaddress = $objcomputer.ipv4.addresss.ipaddresstostring
if ($ipaddress -like "192.168.x.x)
{
$computer | move-adobject target-path $destinationou
}
elseif ($ipaddress -like "192.168.x.x)
{
$computer | move-adobject target-path $destinationou1
}
elseif ($ipaddress -like "192.168.x.x)
{
$computer | move-adobject target-path $destinationou2
}
}
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Justin,

I try your script but something is missing and some error message appears.  The error shows and disappears fast.

I add "Import-Module activedirectory" but it does not help.

Is there a command (for ex PAUSE) I can add in the script to make the error screen paused so I can read?
Justin YeungSenior Systems Engineer

Commented:
Open notepad, save the script as .ps1

And then open powershell, the drag the script on it to run

Btw do you have ad tools installed?

Otherwise you will not have the ad cmdlet

Author

Commented:
If I run the .ps1 on a DC, do I still need the AD tool?  

If yes, I am not familiar.  Please advise a link that talks about it.  

Thanks.  The below are the errors I see:

Test-Connection : Testing connection to computer 'CN=COMPUTER1,OU=NY,OU=WIN7,OU=Workstations,DC=
XYZ,DC=com' failed: The requested name is valid, but no data of the requested type was found
At line:3 char:31
+ $objcomputer = test-connection <<<<  -computername $computer -count 1
    + CategoryInfo          : ResourceUnavailable: (CN=COMPUTER1...DC=XYZ,DC=com:String) [Test-Connection], PingE
   xception
    + FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand

Test-Connection : Testing connection to computer 'CN=COMPUTER2,OU=NY,OU=WIN7,OU=Workstations,DC=
XYZ,DC=com' failed: The requested name is valid, but no data of the requested type was found
At line:3 char:31
+ $objcomputer = test-connection <<<<  -computername $computer -count 1
    + CategoryInfo          : ResourceUnavailable: (CN=COMPUTER2..DC=XYZ,DC=com:String) [Test-Connection], PingE
   xception
    + FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand

Test-Connection : Testing connection to computer 'CN=COMPUTER3,OU=NY,OU=WIN7,OU=Workstations,DC=XYZ,D
C=com' failed: The requested name is valid, but no data of the requested type was found
At line:3 char:31
+ $objcomputer = test-connection <<<<  -computername $computer -count 1
    + CategoryInfo          : ResourceUnavailable: (CN=COMPUTER3,OU=N...DC=XYZ,DC=com:String) [Test-Connection], PingE
   xceptioN
    + FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand
Justin YeungSenior Systems Engineer

Commented:
On the line test-connection -computername $computer

Change it to $computer.name
Justin YeungSenior Systems Engineer

Commented:
Dc has ad tools installed by default
The correct script should be
Test-connection -computername $computer.name
Justin YeungSenior Systems Engineer

Commented:
provide the full updated script here

$sourceOu = " your ou"
$destinationOu = "your destination OU'
$destinationOu1 = "your destination OU'
$destinationOu2 = "your destination OU'


$computers = get-adcomputer -filter * -searchbase $sourceou
foreach ($computer in $computers)
{
$objcomputer = test-connection -computername $computer.name -count 1 -ErrorAction SilentContinue
$ipaddress = $objcomputer.ipv4.addresss.ipaddresstostring
if ($ipaddress -like "192.168.x.x)
{
$computer | move-adobject target-path $destinationou
}
elseif ($ipaddress -like "192.168.x.x)
{
$computer | move-adobject target-path $destinationou1
}
elseif ($ipaddress -like "192.168.x.x)
{
$computer | move-adobject target-path $destinationou2
}
}

Author

Commented:
USA close quote needed after the  IP addresses?
Justin YeungSenior Systems Engineer

Commented:
Typo again thanks for iPhone autocorrect

It should be

$ipaddess =$computerobj.ipv4address.ipaddr
Justin YeungSenior Systems Engineer

Commented:
Ipaddresstostring
Justin YeungSenior Systems Engineer

Commented:
$ipaddess = $objcomputer.ipv4address.ipaddresstostring
Justin YeungSenior Systems Engineer

Commented:
$ipaddress =$objcomputer.ipv4address.ipaddresstostring

So bad on typo lol

Author

Commented:
I have created the following and save it to test.ps1.  I open up a power shell window, drag the test.ps1 from windows explorer to the power shell window.  I do not  see any response.  

Can you check the script again?  Thanks.

Import-Module activedirectory
$sourceou = "OU=NY,OU=Workstations,OU=XYZ Computers,DC=AAA,DC=com"
$destou_V9 = "OU=Wireless 9,OU=WIN7,OU=Workstations,OU=XYZ Computers,DC=AAA,DC=com"
$destou_v75 = "OU=wireless 75,OU=WIN7,OU=Workstations,OU=XYZ Computers,DC=AAA,DC=com"
$computers = get-adcomputer -filter * -searchbase $sourceou
foreach ($computer in $computers)
{
$objcomputer = test-connection -computername $computer.name -count 1 -ErrorAction
SilentContinue
$ipaddress = $objcomputer.ipv4addresss.ipaddresstostring
if ($ip -like "10.111.9.*")
{
$computer | move-adobject target-path $destou_9fl
}
elseif ($ip -like "10.115.75.*")
{
$computer | move-adobject target-path $destou_v75
}
}
Justin YeungSenior Systems Engineer

Commented:
Set-ExecutionPolicy RemoteSigned
Import-Module activedirectory
$sourceou = "OU=NY,OU=Workstations,OU=XYZ Computers,DC=AAA,DC=com"
$destou_V9 = "OU=Wireless 9,OU=WIN7,OU=Workstations,OU=XYZ Computers,DC=AAA,DC=com"
$destou_v75 = "OU=wireless 75,OU=WIN7,OU=Workstations,OU=XYZ Computers,DC=AAA,DC=com"
$computers = get-adcomputer -filter * -searchbase $sourceou
foreach ($computer in $computers)
{
Write-host "Testing connection to " + $computer

$objcomputer = test-connection -computername $computer.name -count 1 -ErrorAction
SilentContinue
$ip = $objcomputer.ipv4addresss.ipaddresstostring
write-host $computer + "has ip address" + $ip

if ($ip -like "10.111.9.*")
{
$computer | move-adobject target-path $destou_V9
write-host $computer + "has been moved to OU" $destou_V9
}
elseif ($ip -like "10.115.75.*")
{
$computer | move-adobject target-path $destou_v75
write-host $computer + "has been moved to OU" $destou_75
}
}

added some write-host message
Top Expert 2014

Commented:
There's a syntax error in the above.
ErrorAction should be "SilentlyContinue", not "SilentContinue"

By the way, whenever posting more than a line of code, it's helpful to include it in a code block.  Just select the desired text, then click on the Code formatting item above the text entry box.
Justin YeungSenior Systems Engineer

Commented:
Yea - I will blame iPhone auto correct

Author

Commented:
Two lines have problem.  Please advise again.  Thanks.

PS C:\Users\edmin> $objcomputer = test-connection -computername $computer.name -count 1 -ErrorAction
Test-Connection : Missing an argument for parameter 'ErrorAction'. Specify a parameter of type 'System.Management.Autom
ation.ActionPreference' and try again.
At line:1 char:82
+ $objcomputer = test-connection -computername $computer.name -count 1 -ErrorAction <<<<
    + CategoryInfo          : InvalidArgument: (:) [Test-Connection], ParameterBindingException
    + FullyQualifiedErrorId : MissingArgument,Microsoft.PowerShell.Commands.TestConnectionCommand
 
PS C:\Users\edmin> SilentContinue
The term 'SilentContinue' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:15
+ SilentContinue <<<<
    + CategoryInfo          : ObjectNotFound: (SilentContinue:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Justin YeungSenior Systems Engineer

Commented:
It should be SilentlyContinue instead of silentcontinue after -ErrorAction
Top Expert 2014

Commented:
Even with the correction to "SilentlyContinue", the errors were because of a line-break after the -ErrorAction parameter.  I went ahead and cleaned up the rest of the syntax errors which would have prevented the script from working right.
Set-ExecutionPolicy RemoteSigned
Import-Module activedirectory
$sourceou = "OU=NY,OU=Workstations,OU=XYZ Computers,DC=AAA,DC=com"
$destou_V9 = "OU=Wireless 9,OU=WIN7,OU=Workstations,OU=XYZ Computers,DC=AAA,DC=com"
$destou_v75 = "OU=wireless 75,OU=WIN7,OU=Workstations,OU=XYZ Computers,DC=AAA,DC=com"
$computers = Get-ADComputer -filter * -searchbase $sourceou
foreach ($computer in $computers)
{
    $comp = $computer.name
    Write-host "Testing connection to $comp"

    $objcomputer = Test-Connection -ComputerName $comp -Count 1 -ErrorAction SilentlyContinue
    $ip = $objcomputer.ipv4addresss.ipaddresstostring
    Write-Host "$comp has ip address $ip"

    if ($ip -like "10.111.9.*")
    {
        $computer | Move-ADObject -TargetPath $destou_V9
        Write-Host "$comp has been moved to OU $destou_V9"
    }
    elseif ($ip -like "10.115.75.*")
    {
        $computer | Move-ADObject -TargetPath $destou_v75
        Write-Host "$comp has been moved to OU $destou_75"
    }
}

Open in new window

Justin YeungSenior Systems Engineer

Commented:
Set-ExecutionPolicy RemoteSigned
Import-Module activedirectory
$sourceou = "OU=NY,OU=Workstations,OU=XYZ Computers,DC=AAA,DC=com"
$destou_V9 = "OU=Wireless 9,OU=WIN7,OU=Workstations,OU=XYZ Computers,DC=AAA,DC=com"
$destou_v75 = "OU=wireless 75,OU=WIN7,OU=Workstations,OU=XYZ Computers,DC=AAA,DC=com"
$computers = get-adcomputer -filter * -searchbase $sourceou
foreach ($computer in $computers)
{
Write-host "Testing connection to " + $computer.name

$objcomputer = test-connection -computername $computer.name -count 1 -ErrorAction SilentlyContinue
$ip = $objcomputer.ipv4addresss.ipaddresstostring
write-host $computer + "has ip address" + $ip

if ($ip -like "10.111.9.*")
{
$computer | move-adobject target-path $destou_V9
write-host $computer + "has been moved to OU" $destou_V9
}
elseif ($ip -like "10.115.75.*")
{
$computer | move-adobject target-path $destou_v75
write-host $computer + "has been moved to OU" $destou_75
}
}
Top Expert 2014

Commented:
@Justin Yeung - that still has other syntax errors.
Justin YeungSenior Systems Engineer

Commented:
Thank you......... didn't work well by just typing it in manually.....

Set-ExecutionPolicy RemoteSigned
Import-Module activedirectory
$sourceou = "OU=NY,OU=Workstations,OU=XYZ Computers,DC=AAA,DC=com"
$destou_V9 = "OU=Wireless 9,OU=WIN7,OU=Workstations,OU=XYZ Computers,DC=AAA,DC=com"
$destou_v75 = "OU=wireless 75,OU=WIN7,OU=Workstations,OU=XYZ Computers,DC=AAA,DC=com"
$computers = get-adcomputer -filter * -searchbase $sourceou
foreach ($computer in $computers)
{
Write-host "Testing connection to" $computer.name

$objcomputer = test-connection -computername $computer.name -count 1 -ErrorAction SilentlyContinue
$ip = $objcomputer.ipv4addresss.ipaddresstostring
write-host $computer.Name "has ip address" $ip

if ($ip -like "10.111.9.*")
{
$computer | move-adobject target-path $destou_V9
write-host $computer.Name "has been moved to OU" $destou_V9
}
elseif ($ip -like "10.115.75.*")
{
$computer | move-adobject target-path $destou_v75
write-host $computer.Name "has been moved to OU" $destou_75
}
} 

Open in new window

Top Expert 2014

Commented:
Sorry, but that still has syntax errors as well.  Why not just use what I posted?
Justin YeungSenior Systems Engineer

Commented:
yes he can use yours but do you mind to correct me where is the syntax error?
since I don't see it...
Top Expert 2014

Commented:
On lines 17 and 22, the parameter for Move-ADObject should be referenced with "-targetpath", not "target-path".  Also, just noticed one that I didn't catch before (only affects the Write-Host output) - on line 23 it should reference the variable $destou_v75 instead of $destou_75.  And one more on line 12, one too many "s"es in the property "ipv4addresss".
Senior Systems Engineer
Commented:
yes..........thank you for indicated that.

I was typing it up directly but not using any autocomplete via an iphone............

anyway it is good with yours.
Set-ExecutionPolicy RemoteSigned
Import-Module activedirectory
$sourceou = "OU=NY,OU=Workstations,OU=XYZ Computers,DC=AAA,DC=com"
$destou_V9 = "OU=Wireless 9,OU=WIN7,OU=Workstations,OU=XYZ Computers,DC=AAA,DC=com"
$destou_v75 = "OU=wireless 75,OU=WIN7,OU=Workstations,OU=XYZ Computers,DC=AAA,DC=com"
$computers = get-adcomputer -filter * -searchbase $sourceou
foreach ($computer in $computers)
{
Write-host "Testing connection to" $computer.name

$objcomputer = test-connection -computername $computer.name -count 1 -ErrorAction SilentlyContinue
$ip = $objcomputer.ipv4address.ipaddresstostring
write-host $computer.Name "has ip address" $ip

if ($ip -like "10.111.9.*")
{
$computer | move-adobject -targetpath $destou_V9
write-host $computer.Name "has been moved to OU" $destou_V9
}
elseif ($ip -like "10.115.75.*")
{
$computer | move-adobject -targetpath $destou_v75
write-host $computer.Name "has been moved to OU" $destou_75
}
} 

Open in new window

Top Expert 2014

Commented:
Yes, typing code on a phone is no fun.

Author

Commented:
I still encounter errors when I run Justin's script.  But it is okay.

Thanks everyone for helping so much ; )  I have got an idea and will try that myself.  

Thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial