Import-Module ActiveDirectory<\/p>\n
$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 #trap<\/span> [System.Net.Sockets.SocketException] { continue; }<\/p>\n $ComputerName = $_.Name $IP = $NULL if ($IP -match $Site1IPRange) { $DestinationDN = $ComputerContainer if ($IP -ne $NULL) { I’m trying to move multiple computers to their correct OUs using this powershell script. The scripts maps IP address to a certain OU in AD then looks at the computer’s IP address in the Computers OU. It moves the computers to their perspective OUs based upon their IP address via DNS. However, I cannot get it to run correctly. The error I continue to get has to do with the line \"$ComputerContainer = $ComputerDN.replace( “CN=$ComputerName,” , “”)<\/p>\n Any suggestions?<\/p>","upvoteCount":3,"answerCount":5,"datePublished":"2020-02-21T19:01:49.000Z","author":{"@type":"Person","name":"spiceuser-ygh6j","url":"https://community.spiceworks.com/u/spiceuser-ygh6j"},"suggestedAnswer":[{"@type":"Answer","text":" Import-Module ActiveDirectory<\/p>\n $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 #trap<\/span> [System.Net.Sockets.SocketException] { continue; }<\/p>\n $ComputerName = $_.Name $IP = $NULL if ($IP -match $Site1IPRange) { $DestinationDN = $ComputerContainer if ($IP -ne $NULL) { I’m trying to move multiple computers to their correct OUs using this powershell script. The scripts maps IP address to a certain OU in AD then looks at the computer’s IP address in the Computers OU. It moves the computers to their perspective OUs based upon their IP address via DNS. However, I cannot get it to run correctly. The error I continue to get has to do with the line \"$ComputerContainer = $ComputerDN.replace( “CN=$ComputerName,” , “”)<\/p>\n Any suggestions?<\/p>","upvoteCount":3,"datePublished":"2020-02-21T19:01:49.000Z","url":"https://community.spiceworks.com/t/move-computers-in-ad/752410/1","author":{"@type":"Person","name":"spiceuser-ygh6j","url":"https://community.spiceworks.com/u/spiceuser-ygh6j"}},{"@type":"Answer","text":" If you post code, please use the ‘Insert Code’ button. Please and thank you!<\/p>\n
\n$Site1DN = “OU=Desktops,OU=TownsendPark,OU=Ethica,DC=ad,DC=local”
\n$Site56IPRange = “\\b(?:(?:192).)” + “\\b(?:(?:168).)” + “\\b(?:(?:91).)” + “\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))” # 192.168.91.0/24
\n$Site56DN = “OU=Desktops,OU=Scott,OU=Ethica,DC=ad,DC=local”
\n#$computers =
\nGet-ADComputer -filter ‘Name -like “MXL*”’ -SearchBase ‘CN=Computers,DC=ad,DC=local’
\n#Export-Csv<\/span> -Path “C:\\scripts\\Test”<\/p>\n<\/a>Ignore Error Messages and continue on<\/h1>\n
<\/a>Set variables for Name and current OU<\/h1>\n
\n$ComputerDN = Get-ADObject -Filter * -Properties Distinguishedname
\n$ComputerContainer = $ComputerDN.replace( “CN=$ComputerName,” , “”) | Write-Host<\/p>\n<\/a>Query DNS for IP<\/h1>\n
<\/a>First we clear the previous IP. If the lookup fails it will retain the previous IP and incorrectly identify the subnet<\/h1>\n
\n$IP = [System.Net.Dns]::GetHostAddresses(“$ComputerName”)<\/p>\n<\/a>Use the $IPLocation to determine the computer’s destination network location<\/h1>\n
\n$DestinationDN = $Site1DN
\n}
\nElseIf ($IP -match $Site56IPRange) {
\n$DestinationDN = $Site56DN
\n}
\nElse {<\/p>\n<\/a>If the subnet does not match we should not move the computer so we do Nothing<\/h1>\n
\n}<\/p>\n
\nMove-ADObject -Identity $ComputerDN -NewParentContainer $DestinationDN
\n}<\/p>\n
\n$Site1DN = “OU=Desktops,OU=TownsendPark,OU=Ethica,DC=ad,DC=local”
\n$Site56IPRange = “\\b(?:(?:192).)” + “\\b(?:(?:168).)” + “\\b(?:(?:91).)” + “\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))” # 192.168.91.0/24
\n$Site56DN = “OU=Desktops,OU=Scott,OU=Ethica,DC=ad,DC=local”
\n#$computers =
\nGet-ADComputer -filter ‘Name -like “MXL*”’ -SearchBase ‘CN=Computers,DC=ad,DC=local’
\n#Export-Csv<\/span> -Path “C:\\scripts\\Test”<\/p>\n<\/a>Ignore Error Messages and continue on<\/h1>\n
<\/a>Set variables for Name and current OU<\/h1>\n
\n$ComputerDN = Get-ADObject -Filter * -Properties Distinguishedname
\n$ComputerContainer = $ComputerDN.replace( “CN=$ComputerName,” , “”) | Write-Host<\/p>\n<\/a>Query DNS for IP<\/h1>\n
<\/a>First we clear the previous IP. If the lookup fails it will retain the previous IP and incorrectly identify the subnet<\/h1>\n
\n$IP = [System.Net.Dns]::GetHostAddresses(“$ComputerName”)<\/p>\n<\/a>Use the $IPLocation to determine the computer’s destination network location<\/h1>\n
\n$DestinationDN = $Site1DN
\n}
\nElseIf ($IP -match $Site56IPRange) {
\n$DestinationDN = $Site56DN
\n}
\nElse {<\/p>\n<\/a>If the subnet does not match we should not move the computer so we do Nothing<\/h1>\n
\n}<\/p>\n
\nMove-ADObject -Identity $ComputerDN -NewParentContainer $DestinationDN
\n}<\/p>\n