daily pastebin goal
10%
SHARE
TWEET

Untitled

a guest Nov 22nd, 2016 17 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #
  2. #I used hash tables in this way as I feel it is more intuative for configuration than a multi-array.
  3. # Define Site Name, and OU computers from that site should go.
  4. $Org_List = @{"Office1" = "ou=site1,dc=test,dc=local";
  5.               "Office2" = "ou=site2,dc=test,dc=local"}
  6.  
  7. #Configure Computer search and limitations
  8. $Computer_List = get-adcomputer -filter { Enabled -eq $true } -searchbase "CN=computers,DC=test,dc=local" | select DnsHostName, DistinguishedName
  9.              
  10. #OutPut the max Range for the provide ip and CIDR 192.168.0.0/24 is the expected format
  11. function find-ipcidr() {
  12.     Param(  [Parameter(Mandatory = $true)]$IP_Cidr )
  13.  
  14. $Ip_Cidr = $IP_Cidr.Split("/")
  15. $Ip_Bin = ($IP_Cidr[0] -split '\.' | ForEach-Object {[System.Convert]::ToString($_,2).PadLeft(8,'0')}).ToCharArray()
  16.     for($i=0;$i -lt $Ip_Bin.length;$i++){
  17.         if($i -ge $Ip_Cidr[1]){
  18.         $Ip_Bin[$i] = "1"
  19.         }
  20.     }
  21.  
  22. [string[]]$IP_Int = @()
  23.     for($i = 0;$i -lt $Ip_Bin.length;$i++) {
  24.     $PartIpBin += $Ip_Bin[$i]
  25.         if(($i+1)%8 -eq 0){
  26.         $PartIpBin = $PartIpBin -join ""
  27.         $IP_Int += [Convert]::ToInt32($PartIpBin -join "",2)
  28.         $PartIpBin = ""
  29.         }
  30.     }
  31.  
  32. $IP_Int = $IP_Int -join "."
  33. return $IP_Int
  34. }
  35.  
  36. #convert the provided IP into an 32 bit integer in order to easier work with in it.
  37. function ip_to_int32(){
  38.     Param(  [Parameter(Mandatory = $true)]$IP_int32 )
  39. $IP_int32_arr = $IP_int32.split(".")
  40. $return_int32 = ([Convert]::ToInt32($IP_int32_arr[0])*16777216 +[Convert]::ToInt32($IP_int32_arr[1])*65536 +[Convert]::ToInt32($IP_int32_arr[2])*256 +[Convert]::ToInt32($IP_int32_arr[3]))
  41. return $return_int32
  42. }
  43.  
  44. #Get site info
  45. $site_arr = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Sites | where { $_.subnets -ne "" } | select Subnets
  46.  
  47.  
  48. #Go through the list of computers gathered at the top of the script, get the ip address via DNS.
  49. #Find the max range for each value in the hash table and if the ip falls into that range move the computer object to corresponding OU.
  50. foreach($computer in $computer_list) {
  51. $Comp_Dns = $computer.DnsHostName
  52.     try {
  53.     $Comp_IP = [System.Net.Dns]::GetHostAddresses("$Comp_Dns") | where-object { $_.AddressFamily -eq 'InterNetwork'} | select -expandproperty IPAddressToString
  54.     } catch {
  55.     echo "Error On lookup of $Comp_Dns :  $_"
  56.     $Comp_IP = $FALSE
  57.     }
  58.    
  59.     if($Comp_IP) {
  60.         foreach($site_info in $site_arr) {
  61.        
  62.         [array] $site_subnet_arr = $site_info.subnets.Name
  63.             if($site_subnet_arr.count -gt 1) {
  64.             [string] $site_name = $site_info.subnets.Site.name[0]
  65.             } else {
  66.             [string] $site_name = $site_info.subnets.Site.name
  67.             }
  68.            
  69.             foreach ($ip_info in $site_subnet_arr) {
  70.            
  71.             $start_of_range_arr = $ip_info.Split("/")
  72.             [string] $start_of_range_value = $start_of_range_arr[0]
  73.             $start_of_range_int32 = ip_to_int32 $start_of_range_value
  74.  
  75.             $end_of_range_value = find-ipcidr $ip_info
  76.             $end_of_range_int32 = ip_to_int32 $end_of_range_value
  77.            
  78.             $search_ip_int32 = ip_to_int32 $Comp_IP
  79.            
  80.                 if(($search_ip_int32 -ge $start_of_range_int32) -and ($search_ip_int32 -le $end_of_range_int32)) {
  81.                 $DistinguishedName = $computer.DistinguishedName
  82.                     try {
  83.                     Move-ADObject -identity "$DistinguishedName" -TargetPath $Org_List."$site_name"        
  84.                     } catch {
  85.                     echo "Error On moving $Comp_Dns :  $_"
  86.                     }
  87.                 }
  88.             }
  89.         }
  90.     }
  91. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top