Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 29-06-2009
Member
 
Join Date: Jun 2009
Posts: 18
need query to get container name of server

hi everyone,

i need query to get the container name of server or computer names or users(whatever you call)

for example
i have server name as USABHSMONAM04;

i need to query AD to get the container name which will look like this;
?CN=USABHSM0NAM04,OU=ABH,OU=Servers,OU=Technology,OU=GMOL,DC=nam,DC=corp,DC=gm,DC=com?.
is it possible?
pls post ASAP
Reply With Quote
  #2  
Old 29-06-2009
Member
 
Join Date: Oct 2008
Posts: 116
Re: need query to get container name of server

public class Server
extends HandlerWrapper
implements Attributes

Jetty HTTP Servlet Server. This class is the main class for the Jetty HTTP Servlet server. It aggregates Connectors (HTTP request receivers) and request Handlers. The server is itself a handler and a ThreadPool. Connectors use the ThreadPool methods to run jobs that will eventually call the handle method.

I found the following source code to get the server name from Active directory.

Code:
import java.util.Hashtable;
import javax.naming.*;
import javax.naming.ldap.*;
import javax.naming.directory.*;
 
public class pdcfsmo	{
	public static void main (String[] args)	{
	
		Hashtable env = new Hashtable();
		String adminName = "CN=Administrator,CN=Users,DC=antipodes,DC=com";
		String adminPassword = "XXXXXX";
		String ldapURL = "ldap://mydc.antipodes.com:389";
					
		env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
	
		//set security credentials, note using simple cleartext authentication
		env.put(Context.SECURITY_AUTHENTICATION,"simple");
		env.put(Context.SECURITY_PRINCIPAL,adminName);
		env.put(Context.SECURITY_CREDENTIALS,adminPassword);
				
		//connect to my domain controller
		env.put(Context.PROVIDER_URL, ldapURL);
 
		try {
 
			// Create the initial directory context
			LdapContext ctx = new InitialLdapContext(env,null);
 
                            	// get the NC's from RootDSE which is a search for objectClass=* at the null root
			Attributes attrs = ctx.getAttributes("");
			String ConfigurationNamingContext = attrs.get("configurationNamingContext").get().toString();
			String SchemaNamingContext = attrs.get("schemaNamingContext").get().toString();
			String RootDomainNamingContext = attrs.get("rootDomainNamingContext").get().toString();
			String DefaultDomainNamingContext = attrs.get("defaultNamingContext").get().toString();
 
			System.out.println("Configuration NC: " + ConfigurationNamingContext);
			System.out.println("Schema NC: " + SchemaNamingContext);
			System.out.println("Root Domain NC: " + RootDomainNamingContext);
			System.out.println("Domain NC: " + DefaultDomainNamingContext);
			System.out.println();
	    
			// Retrieve all the attributes of the Domain NC 
			attrs = ctx.getAttributes(DefaultDomainNamingContext);
 
			//Display the domain wide FSMO role owners	
			//PDC FSMO - on each domain naming context		
			System.out.println("PDC FSMO");
			String FSMORoleOwner = attrs.get("fsmoRoleOwner").get().toString();
			System.out.println("PDC FSMO Roleowner: " + FSMORoleOwner);
			FindServer(ctx,FSMORoleOwner);
 
			//RID FSMO - on the RID Manager Reference on each domain naming context
			System.out.println("RID FSMO");
			String RIDManagerReference = attrs.get("RIDManagerReference").get().toString();
			attrs = ctx.getAttributes(RIDManagerReference);
			FSMORoleOwner = attrs.get("fsmoRoleOwner").get().toString();
			System.out.println("RID FSMO Roleowner: " + FSMORoleOwner);
			FindServer(ctx,FSMORoleOwner);
 
			//Infrastructure FSMO - on the infrastructure container of each domain naming context
			System.out.println("Infastructure FSMO");
			attrs = ctx.getAttributes("CN=Infrastructure," + DefaultDomainNamingContext);
			FSMORoleOwner = attrs.get("fsmoRoleOwner").get().toString();
			System.out.println("RID FSMO Roleowner: " + FSMORoleOwner);
			FindServer(ctx,FSMORoleOwner);	
	
 
			//Schema FSMO- on the schema naming context
			System.out.println("Schema FSMO");
			attrs = ctx.getAttributes(SchemaNamingContext);
			FSMORoleOwner = attrs.get("fsmoRoleOwner").get().toString();
			System.out.println("Schema FSMO Roleowner: " + FSMORoleOwner);
			FindServer(ctx,FSMORoleOwner);
 
			//Naming FSMO - on the partitions container of the configuration naming context
			System.out.println("Naming FSMO");
			attrs = ctx.getAttributes("CN=Partitions," + ConfigurationNamingContext);
			FSMORoleOwner = attrs.get("fsmoRoleOwner").get().toString();
			System.out.println("Naming FSMO Roleowner: " + FSMORoleOwner);
			FindServer(ctx,FSMORoleOwner);	
 
			ctx.close();
 
		}
		catch (NamingException e) {
			System.out.println("Problem retrieving object: " + e);
		}
	}
	public static void FindServer(LdapContext context, String dn) {
 
		try {
 
			//Traverse the directory to obtain the DNS Name of the PDC FSMO
			//All of the FSMO roles point to the NTDS Settings object for each Domain Controller
			Attributes attrs = context.getAttributes(dn);
			String NTDSSettings = attrs.get("distinguishedName").get().toString();
 
			//go up to the parent
			String Settings = "CN=NTDS Settings,";
			String ServerName = NTDSSettings.substring(Settings.length());
			attrs = context.getAttributes(ServerName);
 
			//print out the DNS host name etc.
			System.out.println("Domain Controller: " + attrs.get("serverReference").get());
			System.out.println("DNS Name: " + attrs.get("dnsHostName").get());	
		} 
		catch (NamingException e) {
			System.out.println("Error retrieving attributes: " + e);
		}
		System.out.println();
	}
 
 
}
source: sun.forums
Reply With Quote
  #3  
Old 29-06-2009
Member
 
Join Date: Jun 2009
Posts: 18
Post Re: need query to get container name of server

hi zombi,

i am glad with your reply,but what i need is i have server name with me, i need the container name (fully qulified name of the server) in vbscript not in java
Reply With Quote
  #4  
Old 29-06-2009
Member
 
Join Date: Oct 2008
Posts: 116
Re: need query to get container name of server

VBscript makes this fairly easy. You?ll need to have Windows Script and the Oracle ODBC driver (included with the Oracle client, I believe) installed. It would be somewhat similar to your requirement but you need to change as per your requirement.

Code:
Option Explicit

'* Define Constants / Variables
'SQL command to retrieve date and time from server
Const DateTimeSQL = "SELECT TO_CHAR(SYSDATE, 'MM-DD-YYYY HH24:MI:SS') _
   AS DateTime FROM DUAL" 
Dim connectionString: connectionString = "DRIVER={Microsoft ODBC for Oracle}; _ 
   SERVER=servername;User Id=userID;Password=password;"
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "need query to get container name of server"
Thread Thread Starter Forum Replies Last Post
Query specific DNS server? Sachit Technology & Internet 4 02-12-2011 02:06 AM
SQL Server - Update Query Syntax Error rmadler Software Development 2 02-02-2010 05:10 AM
Server not responding to query Franscisco Technology & Internet 3 26-11-2009 04:40 PM
query the last login time of the computer/server Lupin Active Directory 1 23-10-2009 05:50 PM
Server 2008 query wolfie99 Operating Systems 1 05-03-2009 08:02 AM


All times are GMT +5.5. The time now is 03:45 PM.