Sign in to follow this  
Followers 0
richietheprogrammer

AD Get computer names + IP Addresses

15 posts in this topic

Hello community, I am trying to retrieve computer names from AD and list their IP addresses. By searching the forum, and trying to come up with my code, below is what I have. It lists all the names properly, but I am unable to list their corresponding IP addresses. I am not sure if I am using TCPNameToIP correctly. Any help is greatly appreciated! :)

#include <ADfunctions.au3>

Global $aComputers2
$sOU = $strDNSDomain
_ADGetObjectsInOU($aComputers2,$sOU,"(objectclass=computer)",2,"name,operatingSystem")
Global $bak1
TCPStartup()
For $i = 1 to $aComputers2[0]
;Msgbox(0,"",$aComputers2[$i])
         $bak1 = TCPNameToIP($aComputers2[$i])
     Next
_ArrayDisplay($bak1)

Share this post


Link to post
Share on other sites



Success: Returns string containing IP address corresponding to the name.

not array

1 person likes this

Share this post


Link to post
Share on other sites

Thank you for your reply. I understand it is not an array now, but I am trying to display an array with both the name and the corresponding IP next to each other. How can I do so?

Share this post


Link to post
Share on other sites

you can get ad.au3 from here

#include <AD.au3>

Global $aComputers2

_AD_Open()

If @error Then
    MsgBox(0, "", "unable to connect")
    Exit
EndIf

$aComputers2 = _AD_GetObjectsInOU("", "(objectclass=computer)", 2, "name,operatingSystem")

_AD_Close()

Local $results[1][1]

TCPStartup()

For $i = 1 To UBound($aComputers2) - 1
    $ip_address = TCPNameToIP($aComputers2[$i][0])

    ReDim $results[UBound($results) + 1][3]

    $results[UBound($results) - 1][0] = $aComputers2[$i][0]
    $results[UBound($results) - 1][1] = $aComputers2[$i][1]
    $results[UBound($results) - 1][2] = $ip_address
Next

TCPShutdown()

_ArrayDisplay($results)
1 person likes this

Share this post


Link to post
Share on other sites

Not sure why it is taking a very long time. There are quite a few computers that it is working on, but it appears to be stuck somewhere :(

Any fast way to do it?

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

just the way its gonna work - do you need the OS? might be a little faster if you dont

code below shows results as it gets them (need to run uncompiled - using F5 in scite)

#include <AD.au3>

_AD_Open()

If @error Then
MsgBox(0, "", "unable to connect")
Exit
EndIf

$aComputers2 = _AD_GetObjectsInOU("", "(objectclass=computer)", 2, "name,operatingSystem")

_AD_Close()

Local $results[1][1]

TCPStartup()

For $i = 1 To UBound($aComputers2) - 1
$ip_address = TCPNameToIP($aComputers2[$i][0])

ReDim $results[UBound($results) + 1][3]

$results[UBound($results) - 1][0] = $aComputers2[$i][0]
$results[UBound($results) - 1][1] = $aComputers2[$i][1]
$results[UBound($results) - 1][2] = $ip_address

ConsoleWrite($aComputers2[$i][0] & @CRLF)
ConsoleWrite($aComputers2[$i][1] & @CRLF)
ConsoleWrite($ip_address & @CRLF)
ConsoleWrite(@CRLF)
Next

TCPShutdown()

_ArrayDisplay($results)
Edited by gcue

Share this post


Link to post
Share on other sites

Yes I see the results now, Thank you! Each item is taking about 5 seconds, and I have a couple thousand results.. I actually do not need the OS, but how would the array change if we take that out? Sorry I am not very confident with arrays.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

actually doesnt seem to save too much time but here it is:

#include <AD.au3>

_AD_Open()

If @error Then
MsgBox(0, "", "unable to connect")
Exit
EndIf

$aComputers2 = _AD_GetObjectsInOU("", "(objectclass=computer)")

_AD_Close()

Local $results[1][1]

TCPStartup()

For $i = 1 To UBound($aComputers2) - 1
$ip_address = TCPNameToIP($aComputers2[$i])

ReDim $results[UBound($results) + 1][2]

$results[UBound($results) - 1][0] = $aComputers2[$i]
$results[UBound($results) - 1][1] = $ip_address

ConsoleWrite($aComputers2[$i] & @CRLF)
ConsoleWrite($ip_address & @CRLF)
ConsoleWrite(@CRLF)
Next

TCPShutdown()

_ArrayDisplay($results)
Edited by gcue
1 person likes this

Share this post


Link to post
Share on other sites

Hmm, I supposed there would be no way to make this happen in a reasonable amount of time. Well thank you for your help at any rate :)

Share this post


Link to post
Share on other sites

dont think so, i think the way tcpnametoip works is that it does a lookup on your dns server - not sure if theres a way to do multiple at once

unfortunately - one at a time it looks like

Share this post


Link to post
Share on other sites

Fair enough. I do have another question, however, since this process is not going to be feasible. Using the AD library, is there anyway I can input the name (or part of the name) of a machine on the network, and have the script look it up and run a "tcpnametoip " and return the IP address? This method might be more feasible because it will only go through that function once. Any idea if that would be possible?

Share this post


Link to post
Share on other sites

look at inputbox

Share this post


Link to post
Share on other sites

I am very familiar with inputbox and the process. However, I am not sure how I would store/search, maybe Reg Expressions?

Share this post


Link to post
Share on other sites

If you only want to retrieve the name if a single computer use _AD_GetObjectAttribute.

$sComputer = InputBox(...)
$sName = _AD_GetObjectAttribute($sComputer & "$", "name")
"$" is needed because the samaccountname of a computer has a $ at the end.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0