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

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.
nav2567Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sam Simon NasserIT Support ProfessionalCommented:
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
0
footechCommented:
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.
0
Justin YeungSenior Systems EngineerCommented:
$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
}
}
0
Challenges in Government Cyber Security

Has cyber security been a challenge in your government organization? Are you looking to improve your government's network security? Learn more about how to improve your government organization's security by viewing our on-demand webinar!

nav2567Author 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?
0
Justin YeungSenior Systems EngineerCommented:
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
0
nav2567Author 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
0
Justin YeungSenior Systems EngineerCommented:
On the line test-connection -computername $computer

Change it to $computer.name
0
Justin YeungSenior Systems EngineerCommented:
Dc has ad tools installed by default
The correct script should be
Test-connection -computername $computer.name
0
Justin YeungSenior Systems EngineerCommented:
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
}
}
0
nav2567Author Commented:
USA close quote needed after the  IP addresses?
0
Justin YeungSenior Systems EngineerCommented:
Typo again thanks for iPhone autocorrect

It should be

$ipaddess =$computerobj.ipv4address.ipaddr
0
Justin YeungSenior Systems EngineerCommented:
Ipaddresstostring
0
Justin YeungSenior Systems EngineerCommented:
$ipaddess = $objcomputer.ipv4address.ipaddresstostring
0
Justin YeungSenior Systems EngineerCommented:
$ipaddress =$objcomputer.ipv4address.ipaddresstostring

So bad on typo lol
0
nav2567Author 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
}
}
0
Justin YeungSenior Systems EngineerCommented:
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
0
footechCommented:
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.
0
Justin YeungSenior Systems EngineerCommented:
Yea - I will blame iPhone auto correct
0
nav2567Author 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
0
Justin YeungSenior Systems EngineerCommented:
It should be SilentlyContinue instead of silentcontinue after -ErrorAction
0
footechCommented:
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

0
Justin YeungSenior Systems EngineerCommented:
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
}
}
0
footechCommented:
@Justin Yeung - that still has other syntax errors.
0
Justin YeungSenior Systems EngineerCommented:
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

0
footechCommented:
Sorry, but that still has syntax errors as well.  Why not just use what I posted?
0
Justin YeungSenior Systems EngineerCommented:
yes he can use yours but do you mind to correct me where is the syntax error?
since I don't see it...
0
footechCommented:
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".
0
Justin YeungSenior Systems EngineerCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
footechCommented:
Yes, typing code on a phone is no fun.
0
nav2567Author 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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Server 2008

From novice to tech pro — start learning today.