daily pastebin goal
10%
SHARE
TWEET

Untitled

a guest Nov 22nd, 2016 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. #I used hash tables in this way as I feel it is more intuative for configuration than a multi-array.
  4. $Org_List = @{"192.168.0.0/28" = "ou=site1,dc=test,dc=local";
  5.               "192.168.3.0/24" = "ou=site2,dc=test,dc=local";
  6.               "172.16.0.0/24" = "ou=site2,dc=test,dc=local"}
  7.  
  8.  
  9. #Configure
  10. $Computer_List = get-adcomputer -filter { Enabled -eq $true } -searchbase "CN=computers,DC=test,dc=local" | select DnsHostName, DistinguishedName
  11.              
  12. #OutPut the max Range for the provide ip and CIDR 192.168.0.0/24 is the expected format
  13. function find-ipcidr() {
  14.     Param(  [Parameter(Mandatory = $true)]$IP_Cidr )
  15.  
  16. $Ip_Cidr = $IP_Cidr.Split("/")
  17. $Ip_Bin = ($IP_Cidr[0] -split '\.' | ForEach-Object {[System.Convert]::ToString($_,2).PadLeft(8,'0')}).ToCharArray()
  18.     for($i=0;$i -lt $Ip_Bin.length;$i++){
  19.         if($i -ge $Ip_Cidr[1]){
  20.         $Ip_Bin[$i] = "1"
  21.         }
  22.     }
  23.  
  24. [string[]]$IP_Int = @()
  25.     for($i = 0;$i -lt $Ip_Bin.length;$i++) {
  26.     $PartIpBin += $Ip_Bin[$i]
  27.         if(($i+1)%8 -eq 0){
  28.         $PartIpBin = $PartIpBin -join ""
  29.         $IP_Int += [Convert]::ToInt32($PartIpBin -join "",2)
  30.         $PartIpBin = ""
  31.         }
  32.     }
  33.  
  34. $IP_Int = $IP_Int -join "."
  35. return $IP_Int
  36. }
  37.  
  38. #convert the provided IP into an 32 bit integer in order to easier work with in it.
  39. function ip_to_int32(){
  40.     Param(  [Parameter(Mandatory = $true)]$IP_int32 )
  41. $IP_int32_arr = $IP_int32.split(".")
  42. $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]))
  43. return $return_int32
  44. }
  45.  
  46. #Go through the list of computers gathered at the top of the script, get the ip address via DNS.
  47. #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.
  48. foreach($computer in $computer_list) {
  49. $Comp_Dns = $computer.DnsHostName
  50.     try {
  51.     $Comp_IP = [System.Net.Dns]::GetHostAddresses("$Comp_Dns") | where-object { $_.AddressFamily -eq 'InterNetwork'} | select -expandproperty IPAddressToString
  52.     } catch {
  53.     echo "Error On lookup of $Comp_Dns :  $_"
  54.     $Comp_IP = $FALSE
  55.     }
  56.    
  57.     if($Comp_IP) {
  58.         foreach($ip_info in $Org_List.GetEnumerator().Name) {
  59.         $start_of_range_arr = $ip_info.Split("/")
  60.         [string] $start_of_range_value = $start_of_range_arr[0]
  61.         $start_of_range_int32 = ip_to_int32 $start_of_range_value
  62.  
  63.         $end_of_range_value = find-ipcidr $ip_info
  64.         $end_of_range_int32 = ip_to_int32 $end_of_range_value
  65.        
  66.         $search_ip_int32 = ip_to_int32 $Comp_IP
  67.        
  68.         echo "Start: $start_of_range_value End: $end_of_range_value Looking for: $comp_ip"
  69.             if(($search_ip_int32 -ge $start_of_range_int32) -and ($search_ip_int32 -le $end_of_range_int32)) {
  70.             echo "Found $comp_ip"
  71.             $DistinguishedName = $computer.DistinguishedName
  72.             try {
  73.             Move-ADObject -identity "$DistinguishedName" -TargetPath $Org_List."$ip_info"          
  74.             } catch {
  75.             echo "Error On moving $Comp_Dns :  $_"
  76.             }
  77.             }
  78.         }
  79.     }
  80. }
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