Link to home
Start Free TrialLog in
Avatar of wally_davis
wally_davisFlag for United States of America

asked on

Retrieve Domain Name for a list of Servers

I can write a script to get the top level domain name, i.e. "bisco.food.cor".
But, I want to get the Domain name that you have to log into that is part of the Domain above.
Example, when I log in, I login with my Username and Password and then I have to pick the Domain Name of "market1" that is part of "bisco.food.cor". I hope its clearer then mud because that's about the best way I can explain. See code below.
Thanks Experts,
Wallace
Option Explicit

'On Error Resume Next

Dim strServer, strServers, strServerDomain
Dim x, iRow
Dim objExcel, objFSO, objInputFile, objLocator, objService, objItem
Dim strScriptPath, strFilePath, strDomain, strUsername, strPassword
Dim colItems

x = 2

'Create Excel Worksheet
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.Workbooks.Add

objExcel.Cells(1, 1).Value = "Server Name"
objExcel.Cells(1, 2).Value = "Domain Membership"
objExcel.Cells(1, 3).Value = "Errors?"

objExcel.Range("A1:C1").Select
objExcel.Selection.Interior.ColorIndex = 19
objExcel.Selection.Font.ColorIndex = 11
objExcel.Selection.Font.Bold = True
objExcel.Cells.EntireColumn.AutoFit

Set objFSO = CreateObject("Scripting.FileSystemObject")
strScriptPath = objFSO.GetParentFolderName(WScript.ScriptFullName)
strServers = WScript.Arguments.Item(0)
strFilePath = strScriptPath & "\" & strServers

Set objInputFile = objFSO.OpenTextFile(strFilePath, 1)

If IsEmpty(strServers)Then
	WScript.Echo "#################################################################"
	WScript.Echo "#     Usage: GetSDM.vbs ServerList.txt                          #"
	WScript.Echo "#                                                               #"
	WScript.Echo "#     Important: You need to run this VBScript and              #"
	WScript.Echo "#                your serverlist.txt file from the              #"
	WScript.Echo "#                the same Directory path.                       #"
	WScript.Echo "#                                                               #"
	WScript.Echo "#################################################################"
	WScript.Quit
End If

strDomain = InputBox("Please enter your Domain name: ")
strUsername = InputBox("Please enter your Username: ")
strPassword = InputBox("Please enter your Password: ")

Do While objInputFile.AtEndOfLine <> True
	strServer = ""
	strServer = objInputFile.ReadLine
	
		'Check if Server is available
	If Ping(strServer) = True Then
		iRow = x
		
		objExcel.Cells(iRow, 1).Value = strServer
		'Call to Retrieve Domain Membership
		
		GetDomainMembership
		
		If Err.Number <> 0 Or Err.Number = 80070005 Or Err.Number = 2147024891 Or Err.Number = 2147023174 Or Err.Number = 2147024891 Then
			objExcel.Cells(iRow, 2).Value = "N/A"
			objExcel.Cells(iRow, 3).Value = "Unable to make WMI Server connection; " & "Error Number: " & _
											  Err.Number & "; " & "Error Description: " & Err.Description
			Err.Clear			
		End If
		x = x + 1
	Else
		'Failed ping
		iRow = x
		objExcel.Cells(iRow, 1).Value = strServer
		objExcel.Cells(iRow, 2).Value = "N/A"
		objExcel.Cells(iRow, 3).Value = "Ping Failed"
		x = x + 1
	End If 
Loop

Sub GetDomainMembership

	Set objLocator = CreateObject("WbemScripting.SWbemLocator")
	Set objService = objLocator.ConnectServer(strServer, "\root\cimv2", strDomain & "\" & strUsername, strPassword)
	objService.Security_.ImpersonationLevel = 3
	objService.Security_.AuthenticationLevel = 6
	Set colItems = objService.ExecQuery("Select * From Win32_ComputerSystem", , 48)
	For Each objItem in colItems
	    strServerDomain = objItem.Domain
	    WScript.Echo objItem.Description
	    If objItem.PartOfDomain Then
	    	iRow = x
		    objExcel.Cells(iRow, 2).Value = strServerDomain
		    objExcel.Cells(iRow, 3).Value = "N/A"
		    x = x + 1
	        'WScript.Echo "Computer Domain: " & strServerDomain
	    Else
	    	iRow = x
	    	objExcel.Cells(iRow, 2).Value = strServerDomain
	    	objExcel.Cells(iRow, 3).Value = "N/A"
	        'WScript.Echo "Workgroup: " & strServerDomain
	        x = x + 1
	    End If
	Next
End Sub

'Ping Results
Function Ping(strServer)
	Dim objShell, boolResults
	Set objShell = CreateObject("WScript.Shell")
	boolResults = objShell.Run("Ping -n 2 -w 1000 " & strServer, 0, True)
	
	If boolResults = 0 Then
		Ping = True
	Else
		Ping = False
	End If
	
	Set objShell = Nothing
End Function

Open in new window

Avatar of wally_davis
wally_davis
Flag of United States of America image

ASKER

Just to elaborate, when I go to the Server, go to Cmd and type "set" it shows the "UserDnsDomain" and the "UserDomain". I don't necessarily want my "User" information. But, I do want the DnsDomain, i.e. "sco.food.cor" and also the Domain name "market1" that is part of the "sco.food.cor" Domain that the Server is a member of and not me, the user.
Avatar of Darren Collins
Hi,

I wrote a function a while ago to get this I called fGetComputerDomain.

Replace "Server1" with the name of your server and run the code below.  IT gets the domain (or workgroup) the computer is in, not the user that is using it.

Hope this helps,
Daz.

strComputerName = "Server1"

strDomain = fGetComputerDomain(strComputerName)

MsgBox strDomain,,strComputer

Function fGetComputerDomain(sComputer)
    Dim objWMI, colItems, objItem, sDomain
    Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}\\" & sComputer & "\root\cimv2")
    Set colItems = objWMI.ExecQuery("Select Domain from Win32_ComputerSystem",,48)
    For Each objItem In colItems
        sDomain = objItem.Domain
    Next
    fGetComputerDomain = sDomain
    Set colItems = Nothing
    Set objWMI = Nothing
End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of markdmac
markdmac
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
MarkDMac, this is esentially what I need. However, what I need to be able to do is pipe a bunch of servernames in and run a WMI Remote connection to do this. I'm not sure this this Class Provider "Set objAdSys = CreateObject("ADSystemInfo"), objAdSys.DomainShortName  or "Set oNetwork = WScript.Network, oNetwork.UserDomain" allows me to get the Domain Short name if I run it on a single computer.
Is it possible to access one of these two classes using the "WbemScripting.SWbemLocator" Class or "winmgmt" moniker so I can pass my credentials in like this ? --> Set objLocator = CreateObject("WbemScripting.SWbemLocator")
      Set objService = objLocator.ConnectServer(strServer, "\root\default", strDomain & "\" & strUsername, strPassword) OR
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}\\" & sComputer & "\root\cimv2")
OK, I see what you are looking for now.  Here is how you can get that.

On Error Resume Next

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery("Select * from Win32_NTDomain")

For Each objItem in colItems
    Wscript.Echo "Domain Name: " & objItem.DomainName
Next

Open in new window

I tried the Win32_NTDomain Class yesterday and it returns nothing. I tried that on my local PC and a remote server and it returns no data. That and I've also noticed that accessing this object to return data is somewhat slow. Any other recommendations?
Sorry Mark, I meant that the .DomainName property specfically returns nothing and that's probably the one I need.
Haven't seen code snippets.

What about using a dsquery as part of the scripts?
How about this?
On Error Resume Next

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery("Select * from Win32_NTDomain")

For Each objItem in colItems
    Wscript.Echo "Domain Name: " & objItem.Name
Next

Open in new window

Mark, it both the Win32_NTDomain and the Win32_ComputerSystem "Name" property pulls out the Server name and not the "User Domain" as you can see if you go to CMD Shell, type Set, and then look for the UserDomain environment variable. That's the Domain name I need.
Another to look at for a cmd is the dsquery commands.
dsquery computer
dsquery contact
dsquery subnet
dsquery group
dsquery ou
dsquery site
dsquery server
dsquery user
dsquery quota
dsquery partition
dsquery *

Open in new window

When I run the script provided I get two answers, one is the PC name, the other is the domain name.  Do you not get that too?
Mark, I've tried DomainName, Domain, Caption, Description under Win32_NTDomain and they all give me the name of the Server. If I use the Win32_ComputerSystem's Class properties of "Domain" I get Root Domain of "wrldwd.boa.bank.corp" and for the Name property I get the name of the Server again.
However, I went back and looked at this code you presented --> -- Dim sys --
-- Set sys = CreateObject("ADSystemInfo") --
-- WScript.Echo "Computer: " & sys.ComputerName --
-- WScript.Echo "Domain: " & sys.DomainShortName -- <-- THIS IS WHAT I NEED HERE...
Is it possible to somehow perform a Query against "sys", i.e. sys = CreateObject("ADSystemInfo") to pull out the "DomainShortName" property within the code shown below?

Sub GetDomainMembership

	Set objLocator = CreateObject("WbemScripting.SWbemLocator")
	Set objService = objLocator.ConnectServer(strServer, "\root\cimv2", strDomain & "\" & strUsername, strPassword)
	objService.Security_.ImpersonationLevel = 3
	objService.Security_.AuthenticationLevel = 6
	Set colItems = objService.ExecQuery("Select * From Win32_ComputerSystem") <-- IS IT POSSIBLE TO QUERY THE ADSYSTEMINFO Class or can you only Query the Win32 Classes?

	For Each objItem in colItems
	    strServerDomain = objItem.Domain
	    WScript.Echo objItem.Name
	    If objItem.PartOfDomain Then
	    	iRow = x
		    objExcel.Cells(iRow, 2).Value = strServerDomain
		    objExcel.Cells(iRow, 3).Value = "N/A"
		    x = x + 1
	        'WScript.Echo "Computer Domain: " & strServerDomain
	    Else
	    	iRow = x
	    	objExcel.Cells(iRow, 2).Value = strServerDomain
	    	objExcel.Cells(iRow, 3).Value = "N/A"
	        'WScript.Echo "Workgroup: " & strServerDomain
	        x = x + 1
	    End If
	Next
End Sub

Open in new window

ADSystemInfo doesn't work on remote calls.  Have you considered doing a query via AD instead?
It appears I'm going to have to try AD next. It's just unfortunate that with all of these different Classes you would think they would all allow you to access these Properties with higher priveleges (but some don't) and the one's you need don't quite provide all the Properties you need either. You did provide the ADSystemInfo class and I will give you credit for that. I'll just have to dig into all the AD stuff for now. I don't know it all that well but now is as good as time as any. Thanks Mark!