WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

WMI
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Obtain the Name of the Domain to which the Computer Belongs

 Keywords: domain account computer machine member 

Question:

I need to get the domain name that my machine is a member of. How can this be accomplished?

Answer:

;Sample code to obtain the name of the domain to which the computer belongs
#DefineFunction GetDomainName(computername,username, password)
   Locator = ObjectCreate("WbemScripting.SWbemLocator")
   Service = Locator.ConnectServer(computername,"root/cimv2",username,password)
   Security = Service.Security_
   Security.ImpersonationLevel = 3
   Class = "Win32_ComputerSystem"
   Instance = Service.InstancesOf(Class)
   ForEach obj In Instance
     If Obj == 0 Then Break
     domain = Obj.Domain
   Next
   Instance = 0
   Security = 0
   Service = 0
   Locator = 0
   Return domain
#EndFunction

computername = "BData001"
dom = GetDomainName(computername,"", "")
Message("Name of the domain to which the computer belongs", dom)
Exit

Article ID:   W15789
File Created: 2010:04:02:08:52:42
Last Updated: 2010:04:02:08:52:42