Solved

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

Posted on 2014-03-01
30
322 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
Comment Utility
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 39

Expert Comment

by:footech
Comment Utility
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
Comment Utility
$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
 

Author Comment

by:nav2567
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
On the line test-connection -computername $computer

Change it to $computer.name
0
 
LVL 14

Expert Comment

by:Justin Yeung
Comment Utility
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
Comment Utility
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
Comment Utility
USA close quote needed after the  IP addresses?
0
 
LVL 14

Expert Comment

by:Justin Yeung
Comment Utility
Typo again thanks for iPhone autocorrect

It should be

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

Expert Comment

by:Justin Yeung
Comment Utility
Ipaddresstostring
0
 
LVL 14

Expert Comment

by:Justin Yeung
Comment Utility
$ipaddess = $objcomputer.ipv4address.ipaddresstostring
0
 
LVL 14

Expert Comment

by:Justin Yeung
Comment Utility
$ipaddress =$objcomputer.ipv4address.ipaddresstostring

So bad on typo lol
0
 

Author Comment

by:nav2567
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 14

Expert Comment

by:Justin Yeung
Comment Utility
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 39

Expert Comment

by:footech
Comment Utility
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
Comment Utility
Yea - I will blame iPhone auto correct
0
 

Author Comment

by:nav2567
Comment Utility
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
Comment Utility
It should be SilentlyContinue instead of silentcontinue after -ErrorAction
0
 
LVL 39

Expert Comment

by:footech
Comment Utility
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
Comment Utility
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 39

Expert Comment

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

Expert Comment

by:Justin Yeung
Comment Utility
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 39

Expert Comment

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

Expert Comment

by:Justin Yeung
Comment Utility
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 39

Expert Comment

by:footech
Comment Utility
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
Comment Utility
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 39

Expert Comment

by:footech
Comment Utility
Yes, typing code on a phone is no fun.
0
 

Author Closing Comment

by:nav2567
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…
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 …

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now