Monday, January 14, 2013

WIN DOMAIN BLOG

http://blog.msresource.net/


http://www.msresource.net/paulw/finding_the_virtual_server_hosts_in_your_domain.html

HomeFinding the Virtual Server Hosts in your domainA customer asked me in passing how does one go about finding the Virtual Server hosts in a domain or an enterprise? Well, it just so happened that I'd been playing with Virtual Server 2005 R2 Service Pack 1 (SP1) and noticed that, due to me being offline, my computer (the host in this case) was having some problems registering a service connection point (SCP) object. The reason for that is nice and easy, I was sitting in my office in the house and I didn't have a VPN established, but it did make me realise that there's the answer to this persons question –query Active Directory for the Virtual Server 2005 SCPs... In summary, service connection points are active directory objects that a service creates so that clients of that service can locate the service, and have information on how to connect and authenticate to the service. You can read up on SCPs on MSDN here. Anyway, as of Virtual Server 2005 R2 SP1, Virtual Server now creates an SCP object for each host. Therefore, if we want to find Virtual Server hosts in a given domain, all we have to do is search for them, e.g. (&(objectCategory=serviceConnectionPoint)(cn=MS Virtual Server)).SCPs are child objects of the computer that the service is running on, therefore the above query will return all SCPs within scope. Now that's not very helpful for us, as we want to know what the computer name is. Well, you can obviously connect to the SCP and then connect to the parent object (e.g., using IADs::Parent) however that would be woefully inefficient. The answer is in the serviceBindingInformation attribute of the SCP. In the case of Virtual Server, this attribute contains two values: the remote control URL and the FQDN of the host, e.g. vmrc://host.domain-name.com and host.domain-name.com. The important part that we're after is the second value, the FQDN.I've written a VB Script that will output all Virtual Server 2005 R2 SP1 hosts in the default domain. Here's the script.Option explicit' constantsPrivate Const APPNAME = "FindVS2K5"Private Const APPVERSION = "V01.00.00vbs"Private Const APPAUTHOR = "Paul Williams ( paul@msresource.net)"Private Const APPDATE = "Feb. 2008"' variantsdim dse, dnc, dns, item, virsvrs' output normal blurbprint(APPNAME & " " & APPVERSION & " " & APPAUTHOR & " " & APPDATE & vbCrLf)' print descriptions of what's happeningecho "Locating a domain controller and querying Active Directory " & _ "Domain Services (AD DS) for Service Connection Point (SCP) objects..."set dse=getObject("LDAP://RootDSE")dnc = dse.get("defaultNamingContext")echo "done." & vbCrLfecho "Using domain: " & dnc & vbCrLfdns = dse.get("dNSHostName")echo "Using domain controller: " & lcase(dns) & vbCrLfecho "Finding all [registered] Virtual Server 2005 R2 SP1 instances in domain..."virsvrs = getVirtualServerSCPs(dnc)echo "done." & vbCrLf' we've found the servers, so display in a (tabbed) listecho vbCrLf & "Virtual Server 2005 R2 SP1 instances:" & vbCrLffor each item in virsvrs echo vbTab & lcase(item) & vbCrLfnext' all done, output [very] basic statsecho vbCrLf & vbCrLf & "Script completed. " & ubound(virsvrs) + 1 & _ " hostnames returned." & vbCrLf & vbCrLf' ***********************************************' Function::getVirtualServerSCPs() As String' ' Function returns an array of strings containing' the hostname of each object that matches the ' LDAP query against the default domain.' ' The query is for all serviceConnectionPoints with' a name of "MS Virtual Server"' ' ***********************************************Private Function getVirtualServerSCPs(defaultNamingContext) ' As String() dim adoCommand, adoConnection, adoRecordSet dim ldapBase, ldapFilter, ldapAttributes, ldapScope, ldapQuery ' As String dim serviceBindingInformation ' As String dim returnVal(), i : i = 0 ldapBase = "LDAP://" & defaultNamingContext ldapFilter = "(&(objectCategory=serviceConnectionPoint)(cn=MS Virtual Server))" ldapAttributes = "serviceBindingInformation" ldapScope = "subtree" ldapQuery = "<" & ldapBase & ">;" & ldapFilter & ";" & _ ldapAttributes & ";" & ldapScope Set adoConnection = CreateObject("ADODB.Connection") Set adoCommand = CreateObject("ADODB.Command") adoConnection.provider = "ADsDSOObject" adoConnection.open "Active Directory Provider" adoCommand.activeConnection = adoConnection adoCommand.CommandText = ldapQuery adoCommand.properties("Page Size") = 100 adoCommand.properties("Size Limit") = 10000 adoCommand.properties("Cache Results") = False ' execute the command Set adoRecordSet = adoCommand.execute do until adoRecordSet.EOF serviceBindingInformation = adoRecordSet.fields("serviceBindingInformation") if(isArray(serviceBindingInformation) AND _ uBound(serviceBindingInformation) = 1)then redim preserve returnArr(i) returnArr(i) = serviceBindingInformation(1) i = i + 1 end if adoRecordSet.MoveNext loop getVirtualServerSCPs = returnArrEnd Function' ***********************************************' Echo(string output)' ' Sub prints the passed string to the console (or' a message box, depending on the script's calling' process).' ' Note. This uses WRITE not WRITELN' ' ***********************************************private sub Echo(outputString) wscript.stdout.write outputStringend subprivate sub Print(outputString) wscript.echo outputStringend sub Note that this script uses the Windows locator to locate the closest domain controller in the default domain (the domain of the security context that initiates the script) so the script must be run from an NT 5.x or NT 6.x computer that is a member of an Active Directory domain. The script is only querying for Service Connection Point objects too, therefore if there are Virtual Server hosts that haven't registered SCPs these obviously won't show up. Lastly, the script should be run using CSCRIPT and is provided without warranty and confers no rights, etc. etc.To use this script copy the script body into a text editor, e.g. NOTEPAD, and save with the .VBS extension. Please note that for some reason my blog always drops the line Private Const APPAUTHOR = "Paul Williams (paul@msresource.net)" onto two lines, this is one line.Here's how I'd run it from my workstaion:C:\dev\lang\vbs\ds>cscript ds-FindAllVirtualServers.vbs

No comments:

Post a Comment