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
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
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
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;"