-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAD-ChangeOUbySubnet.ps1
More file actions
70 lines (57 loc) · 2.48 KB
/
Copy pathAD-ChangeOUbySubnet.ps1
File metadata and controls
70 lines (57 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Change OU of computer by IP
# using IS-InSubnet function by Srinivasa Tumarada https://gallery.technet.microsoft.com/scriptcenter/Validate-an-Ipaddress-is-03481731
#
# Settings
$output_ou = "" # destination OU
$subnets_array = ('') # array of subnets where the computers are located
#function for detection if computer's IP is in the subnet
Function IS-InSubnet()
{
[CmdletBinding()]
[OutputType([bool])]
Param(
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[validatescript({([System.Net.IPAddress]$_).AddressFamily -match 'InterNetwork'})]
[string]$ipaddress="",
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=1)]
[validatescript({(([system.net.ipaddress]($_ -split '/'|select -first 1)).AddressFamily -match 'InterNetwork') -and (0..32 -contains ([int]($_ -split '/'|select -last 1) )) })]
[string]$Cidr=""
)
Begin{
[int]$BaseAddress=[System.BitConverter]::ToInt32((([System.Net.IPAddress]::Parse(($cidr -split '/'|select -first 1))).GetAddressBytes()),0)
[int]$Address=[System.BitConverter]::ToInt32(([System.Net.IPAddress]::Parse($ipaddress).GetAddressBytes()),0)
[int]$mask=[System.Net.IPAddress]::HostToNetworkOrder(-1 -shl (32 - [int]($cidr -split '/' |select -last 1)))
}
Process{
if( ($BaseAddress -band $mask) -eq ($Address -band $mask))
{
$status=$True
}else {
$status=$False
}
}
end { return $status }
}
$ADcomputers = Get-ADComputer -Filter * -Properties ipv4Address #get all AD computers with IPs
foreach ($ADcomputer in $ADcomputers) #check every computer if they in the subnets, then move to the OU
{
if ($ADcomputer.IPv4Address)
{
foreach($subnet in $subnets_array)
{
$address = $ADcomputer.IPv4Address.ToString()
$cidr = $subnet
$check_subnet = IS-InSubnet -ipaddress $address -Cidr $cidr
if ($check_subnet)
{
Move-ADObject -Identity $ADcomputer.objectGUID -TargetPath $output_ou
$output_res = "Changed OU for " + $ADcomputer.ipv4Address + " " + $ADcomputer.Name
Write-Host ($output_res)
}
}
}
}