I have a group of computers in AD that I need to get the IP addresses from. Not concerned about script speed. It can run for a few hours if needed and can skip the machines that are offline if necessary. I've got something close, but I think some changes to the WMI connections are needed. I apologize as I have found numerous similar threads, but none that match this issue or merge with other threads to match it.
I initially used this code to get the computer names into Excel, then added the OS column, and now need to add the IP addresses. The section under "'INSERT IP CODE HERE" is where the IP related code sits. I'm sure I either need to move the IP loop or adjust the WMI connection, but so far, nothing has worked and I'm getting into the weeds.
Most machines are using DHCP, but some may be static. If that causes a problem, the static IP's can be skipped. I'm concerned with getting as many IP's as possible. It doesn't need to be 100%
Any help or suggestions are appreciated.
I initially used this code to get the computer names into Excel, then added the OS column, and now need to add the IP addresses. The section under "'INSERT IP CODE HERE" is where the IP related code sits. I'm sure I either need to move the IP loop or adjust the WMI connection, but so far, nothing has worked and I'm getting into the weeds.
Most machines are using DHCP, but some may be static. If that causes a problem, the static IP's can be skipped. I'm concerned with getting as many IP's as possible. It doesn't need to be 100%
Any help or suggestions are appreciated.
Code:
Dim objGroup, objExcel, iRow, strUser
Set objGroup = GetObject("LDAP://CN=GROUP NAME,OU=Groups,OU=AD,DC=DOMAIN,DC=com") ' create the group object
Set objExcel = CreateObject("Excel.Application")
With objExcel
.SheetsInNewWorkbook = 1
.Workbooks.Add
.Visible = True
.Worksheets.Item(1).Name = "GROUP NAME" 'set Worksheet name to that of the group
irow=1
For Each Member in objGroup.Members ' for each member of the group
on error resume next
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & Member.cn & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
strOScaption = objOperatingSystem.Caption
strOSversion = objOperatingSystem.Version
strDN = Member.distinguishedName
on error resume next
strOU = mid(strDN, Len(strDN)-42, 7) ' The actual length of the DN in our originzation requires these lengths
If strOU = "NonPOLICY" Then
'WScript.echo Member.cn & " ; " & Member.displayName ' echo the common name and displayname
.Cells(iRow,1) = "NonPOLICY"
.Cells(iRow,2) = Member.cn
.Cells(iRow,4) = strOScaption
.Cells(iRow,5) = Member.distinguishedName
irow=irow + 1
Else
.Cells(iRow,1) = "POLICY"
.Cells(iRow,2) = Member.cn
.Cells(iRow,3) = ActiveDHCPIPAddress
.Cells(iRow,4) = strOScaption
'.Cells(iRow,5) = strOSversion
.Cells(iRow,5) = Member.distinguishedName
irow=irow + 1
End If
Next ' Close the second (OS) loop
'INSERT IP CODE HERE and write to Excel a second time??? ..this almost worked, but returns the same IP. The loop is wrong.
Set colItems = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
strCount = 1
For Each objitem in colitems
If strCount = 1 Then
ActiveDHCPIPAddress = objitem.IPAddress(0)
'strIPAddress = Join(objitem.IPAddress, ",")
IP = stripaddress
strCount = strCount + 1
wscript.echo ActiveDHCPIPAddress
.Cells(iRow,3) = ActiveDHCPIPAddress
Else
End If
Next ' Close the third (IP) loop
Next ' Close the first loop
.Columns(1).entirecolumn.autofit
End With
Set objGroup = Nothing ' delete the group object and clear memory
Set objExcel = Nothing