Solved

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

Posted on 2014-03-01
30
331 Views
Last Modified: 2014-04-08
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.
0
Comment
Question by:nav2567
  • 16
  • 7
  • 6
  • +1
30 Comments
 
LVL 10

Assisted Solution

by:Sam Simon Nasser
Sam Simon Nasser earned 200 total points
ID: 39897165
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
 
LVL 40

Expert Comment

by:footech
ID: 39897975
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
 
LVL 14

Assisted Solution

by:Justin Yeung
Justin Yeung earned 300 total points
ID: 39907034
$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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:nav2567
ID: 39911467
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
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39911476
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
 

Author Comment

by:nav2567
ID: 39911528
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
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39911561
On the line test-connection -computername $computer

Change it to $computer.name
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39911563
Dc has ad tools installed by default
The correct script should be
Test-connection -computername $computer.name
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39911570
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
 

Author Comment

by:nav2567
ID: 39912211
USA close quote needed after the  IP addresses?
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39912288
Typo again thanks for iPhone autocorrect

It should be

$ipaddess =$computerobj.ipv4address.ipaddr
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39912289
Ipaddresstostring
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39912293
$ipaddess = $objcomputer.ipv4address.ipaddresstostring
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39912301
$ipaddress =$objcomputer.ipv4address.ipaddresstostring

So bad on typo lol
0
 

Author Comment

by:nav2567
ID: 39925432
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
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39926275
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
 
LVL 40

Expert Comment

by:footech
ID: 39926929
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
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39926937
Yea - I will blame iPhone auto correct
0
 

Author Comment

by:nav2567
ID: 39948117
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
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39948154
It should be SilentlyContinue instead of silentcontinue after -ErrorAction
0
 
LVL 40

Expert Comment

by:footech
ID: 39948273
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
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39950302
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
 
LVL 40

Expert Comment

by:footech
ID: 39950881
@Justin Yeung - that still has other syntax errors.
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39950997
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
 
LVL 40

Expert Comment

by:footech
ID: 39951089
Sorry, but that still has syntax errors as well.  Why not just use what I posted?
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39951098
yes he can use yours but do you mind to correct me where is the syntax error?
since I don't see it...
0
 
LVL 40

Expert Comment

by:footech
ID: 39951217
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
 
LVL 14

Accepted Solution

by:
Justin Yeung earned 300 total points
ID: 39951248
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
 
LVL 40

Expert Comment

by:footech
ID: 39951312
Yes, typing code on a phone is no fun.
0
 

Author Closing Comment

by:nav2567
ID: 39987545
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

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains the steps required to use the default Photos screensaver to display branding/corporate images
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question