Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Move Computer to another OU in AD based on IP address

Status
Not open for further replies.

Big10uk

Technical User
Feb 10, 2004
18
GB
Hi

I am new to scripting and would like be able to move computer objects within AD based on there IP address?


Example:
192.168.1.1 -> 1st Floor OU
192.168.2.1 -> 2nd Floor OU
Etc...


I have already got a moving script which works great, but this needs me to manually enter the computer name?

Set objNewOU = GetObject("LDAP://OU=2nd Floor,OU=Workstations,DC=Domain,DC=co,DC=uk")

Set objMoveComputer = objNewOU.MoveHere ("LDAP://CN=PC00001,CN=Computers,DC=Domain,DC=co,DC=uk", vbNullString)


Is there away that I could run the script locally on pc and use the %computername% variable?

Many thanks

James
 
move based on IP address?? sounds like a strange notion..

have you had a look at Sites and Subnets?
you can get the .SiteName of where the local machine 'currently' is located based on the Subnet that is assigned to the AD Site, this might give you the functionality you are after or might provide another advantage...
 
Thanks mrmoive,

Where in Sites and services can I get this information?

 
Just found it..

running "gpresult" will give that information.

Thanks
 
Set objADSysInfo = CreateObject("ADSystemInfo")

WScript.Echo "Current site name: " & objADSysInfo.SiteName
 
Thanks

How can I find out the local pcs ip address, without doing an ipconfig?
 
Winsock way:
'Get current ip address
Dim WshSock, strIPAddress
Set WshSock = WScript.CreateObject("MSWinsock.Winsock")
strIPAddress = WshSock.LocalIP
Set WshSock = Nothing


WMI way:

Option Explicit

Dim objWMIService, colAdapters
Dim intCounter, objAdapter, i

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colAdapters = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")

intCounter = 1
WScript.Echo

For Each objAdapter in colAdapters

WScript.Echo "Network Adapter " & intCounter
WScript.Echo "================="
WScript.Echo " Description: " & objAdapter.Description
WScript.Echo " Physical (MAC) address: " & objAdapter.MACAddress
WScript.Echo " Host name: " & objAdapter.DNSHostName

If Not IsNull(objAdapter.IPAddress) Then
For i = 0 To UBound(objAdapter.IPAddress)
WScript.Echo " IP address: " & objAdapter.IPAddress(i)
Next
End If

If Not IsNull(objAdapter.IPSubnet) Then
For i = 0 To UBound(objAdapter.IPSubnet)
WScript.Echo " Subnet: " & objAdapter.IPSubnet(i)
Next
End If

If Not IsNull(objAdapter.DefaultIPGateway) Then
For i = 0 To UBound(objAdapter.DefaultIPGateway)
WScript.Echo " Default gateway: " & objAdapter.DefaultIPGateway(i)
Next
End If

WScript.Echo
WScript.Echo " DNS"
WScript.Echo " ---"
WScript.Echo " DNS servers in search order:"

If Not IsNull(objAdapter.DNSServerSearchOrder) Then
For i = 0 To UBound(objAdapter.DNSServerSearchOrder)
WScript.Echo " " & objAdapter.DNSServerSearchOrder(i)
Next
End If

WScript.Echo " DNS domain: " & objAdapter.DNSDomain

If Not IsNull(objAdapter.DNSDomainSuffixSearchOrder) Then
For i = 0 To UBound(objAdapter.DNSDomainSuffixSearchOrder)
WScript.Echo " DNS suffix search list: " & objAdapter.DNSDomainSuffixSearchOrder(i)
Next
End If

WScript.Echo
WScript.Echo " DHCP"
WScript.Echo " ----"
WScript.Echo " DHCP enabled: " & objAdapter.DHCPEnabled
WScript.Echo " DHCP server: " & objAdapter.DHCPServer

If Not IsNull(objAdapter.DHCPLeaseObtained) Then
utcLeaseObtained = objAdapter.DHCPLeaseObtained
strLeaseObtained = WMIDateStringToDate(utcLeaseObtained)
Else
strLeaseObtained = ""
End If
WScript.Echo " DHCP lease obtained: " & strLeaseObtained

If Not IsNull(objAdapter.DHCPLeaseExpires) Then
utcLeaseExpires = objAdapter.DHCPLeaseExpires
strLeaseExpires = WMIDateStringToDate(utcLeaseExpires)
Else
strLeaseExpires = ""
End If
WScript.Echo " DHCP lease expires: " & strLeaseExpires

WScript.Echo
WScript.Echo " WINS"
WScript.Echo " ----"
WScript.Echo " Primary WINS server: " & objAdapter.WINSPrimaryServer
WScript.Echo " Secondary WINS server: " & objAdapter.WINSSecondaryServer
WScript.Echo

intCounter = intCounter + 1
Next


Set colAdapters = Nothing
Set objWMIService = Nothing


Function WMIDateStringToDate(utcDate)
WMIDateStringToDate = CDate(Mid(utcDate, 5, 2) & "/" & _
Mid(utcDate, 7, 2) & "/" & _
Left(utcDate, 4) & " " & _
Mid (utcDate, 9, 2) & ":" & _
Mid(utcDate, 11, 2) & ":" & _
Mid(utcDate, 13, 2))
End Function
 
look about 4 posts down in the forum and you will find the shortenned WMI ip code you need.. "Display IP aadress in HTA
 
Thanks again,

I will now try and put all this together.

James
 
Here's another WMI based call... Runs on XP and higher. It will determing the active NIC on a system with multiple NICs (think VMWare). Also throw errors for systems with multiple gateways and multiple IP's bound to one NIC. Function returns the actual subnet based on the subnet mask.

Code:
WScript.Echo GetIP ' Sample only, your code here

Function GetIP()
' This function returns the correct IP subnet of the system based on the
' subnet mask.  Function first filters out any disconnected NICs or NICs
' that don't have IP protocol enabled.  If the NIC doesn't have a Default
' gateway, it is ignored.  If there are no default gateways defined, Then
' the function returns 0.0.0.0 as the subnet.  If configuration errors are
' found, like multiple active gateways or multiple IP's on one NIC, then the 
' function returns 0.0.0.0 as the subnet.
	Dim objWMI, objNICInfo, objIPInfo
	Dim strActiveAdapter, strIPInfo, strAddress, strGateway, strMask
	Dim strSubnet, ctr

	Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
	
	Set objNICInfo = objWMI.ExecQuery("Select * from Win32_NetworkAdapter" _
		& " Where NetConnectionStatus = 2")
	strGateway = Empty

	For Each strActiveAdapter In objNICInfo
		' Connect to specific adapter by Index number
		Set objIPInfo = objWMI.ExecQuery("Select * from " & _
			"Win32_NetworkAdapterConfiguration Where Index =" & _
			strActiveAdapter.Index)

		' If IP is enabled, check for configuration errors, then get IPSubnet
		For Each strIPInfo In objIPInfo
			If strIPInfo.IPEnabled = True Then
				strAddress = Join(strIPInfo.IPAddress, ",")
				If IsNull(strIPInfo.DefaultIPGateway) Then
					Exit For
				Else
					strGateway = Join(strIPInfo.DefaultIPGateway, ",")
					If InStr(strGateway, ",") Then
						Exit For
					End If
				End If
				strMask = Join(strIPInfo.IPSubnet, ",")
				If strSubnet = "" Then
					strSubnet = GetSubnet(strAddress, strMask)
				Else
					strSubnet = strSubnet & "," & GetSubnet(strAddress, strMask)
				End if
			End If
		Next
	Next

	If strGateway = "" Then
		MsgBox("No Gateways defined on any NIC")
		GetIP = "0.0.0.0"
		Exit Function
	End If

	' Check if multiple subnets were identified
	If InStr(strSubnet, ",") Then
		MsgBox("Network Misconfiguration! Multiple gateways detected.")
		GetIP = "0.0.0.0"
		Exit Function
	Else
		GetIP = strSubnet
	End If
End Function

Function GetSubnet(Addr, Mask)
' Get the subnet by "ANDing" the IP address and subnet mask
	Dim strNet, Counter
	Dim arrIP, arrMask

	' Quit function if multiple IP's are bound to one NIC
	If InStr(Addr, ",") <> 0 Then
		MsgBox("More than one IP bound to NIC.")
		GetSubnet = "0.0.0.0"
		Exit Function
	Else
		arrIP = Split(Addr, ".")
	End If

	arrMask = Split(Mask, ".")

	For Counter = 0 To 3
		If strNet = "" Then
			strNet = arrIP(Counter) And arrMask(Counter)
		Else
			strNet = strNet & "." & (arrIP(Counter) And arrMask(Counter))
		End If
	Next

	GetSubnet = strNet

End Function

Good Luck!


PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
So far i have done this:

On Error Resume Next

Const ForAppending = 8
Const ForReading = 1
Const ForWriting = 2
strComputer = "."

Set objSysInfo = CreateObject("ADSystemInfo")
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set IPConfigSet = objWMIService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshNetwork = CreateObject("WScript.Network")
Set objTextFile = objFSO.OpenTextFile ("C:\" & WshNetwork.ComputerName & ".txt", ForAppending, True)



'objTextFile.WriteLine( "User name: " & objSysInfo.UserName)
objTextFile.WriteLine( "Computer name: " & objSysInfo.ComputerName)
objTextFile.WriteLine ( "Site name: " & objSysInfo.SiteName)

For Each IPConfig in IPConfigSet
If Not IsNull(IPConfig.IPAddress) Then
For i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
objTextFile.WriteLine ( "IP address: " & IPConfig.IPAddress(i))
Next
End If
Next
objTextFile.Close

'Remove Duplicates
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile ("C:\" & WshNetwork.ComputerName & ".txt", ForReading)

Do Until objFile.AtEndOfStream
strName = objFile.ReadLine
If Not objDictionary.Exists(strName) Then
objDictionary.Add strName, strName
End If
Loop

objFile.Close

Set objFile = objFSO.OpenTextFile ("C:\" & WshNetwork.ComputerName & ".txt", ForWriting)

For Each strKey in objDictionary.Keys
objFile.WriteLine strKey
Next

objFile.Close


This displays the following information on a text file on teh C:\


PCNAME.TXT

Computer name: CN=PCNAME,CN=Computers,DC=domain,DC=co,DC=uk
Site name: 000-2nd-Floor
IP address: 192.168.2.1


I am now trying to sort all the these text files into seperate folders now based on the site name....

Will post more when i get more

Ta

James




 
create a folder in your script based on the Sitename and create your pc.txt in there
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top