Friday, September 19, 2003
GetFQDN
I recently had the need to return the Fully Qualified Domain name for a Windows 2000 domain. It turns out that this is available via the RootDSE class's defaultNamingContext and rootDomainNamingContext properties.
Took me a little while to figure out where to find this information, but once I started looking into LDAP and the Active Directory, the solution was pretty simplistic.
|
Took me a little while to figure out where to find this information, but once I started looking into LDAP and the Active Directory, the solution was pretty simplistic.
'----------------------------------------+---------------------------------------- ' GetFQDN.VBS - Script to get a Fully Qualified Domain Name for a W2K Domain ' ' Copyright© 2003, Christopher G. Lewis - HTTP://www.ChristopherLewis.com ' ' Date Name Comment ' ------ ------- ----------------------------------------------------------------- ' 030918 CGL Created '----------------------------------------+---------------------------------------- Option Explicit Dim objRootDSE Dim strNamingContext Dim strDomain Dim strQuery Dim strFQDN 'Parse command line If Wscript.arguments.Count = 0 Then strDomain = "" Else strDomain = Wscript.arguments(0) End If 'If command line is blank, process current domain If strDomain = "" Then Wscript.echo "Getting FQDN current domain..." strQuery = "LDAP://RootDSE" Else Wscript.echo "Getting FQDN for " & strDomain & "..." strQuery = "LDAP://" & strDomain & "/RootDSE" End If On Error Resume Next 'Open up a query to the RootDSE object. 'From the SDK: The RootDSE class provides information about the capabilities of an LDAP server. Set objRootDSE = GetObject(strQuery) If Err = 0 Then 'Domain FQDN strNamingContext = objRootDSE.Get("defaultNamingContext") strFQDN = GetFQDNFromNamingContext(strNamingContext) Wscript.echo "Domain FQDN: " & strFQDN 'Root Domain FQDN strNamingContext = objRootDSE.Get("rootDomainNamingContext") strFQDN = GetFQDNFromNamingContext(strNamingContext) Wscript.echo "Root Domain: " & strFQDN Else Wscript.echo "Looks like Domain " & strDomain & " doesn't exist." End If '----------------------------------------+---------------------------------------- ' GetFQDNFromNamingContext - Function to convert a Naming Context into a DNS name ' Parms : strNamingContext i.e. DC=Domain,DC=Company,DC=com ' Returns: FQDN for strNamingContext ie.e Domain.Company.com ' ' Notes : LDAP allows for commas in strings, as long as they are escaped with ' a \ character. (i.e. "CN=Lewis\, Chris") Sinces commas are not ' allowed in domain names, there is no parsing for this is left out. ' An implimentation of this would check to see if the last char was a ' "\", and combine the two array elements. ' ' Date Name Comment ' ------ ------- ----------------------------------------------------------------- ' 030919 CGL Created '----------------------------------------+---------------------------------------- Function GetFQDNFromNamingContext(strNamingContext) Dim aDomain Dim iCount Dim strTemp 'Parse the NC by creating an array with the comma as an array boundry aDomain = Split(strNamingContext, ",") For iCount = 0 To UBound(aDomain) 'Add a "." if needed If strTemp <> "" Then strTemp = strTemp & "." End If 'Remove the "DC=" and add this item to the temp string strTemp = strTemp & Mid(aDomain(iCount), 4) Next 'Return the FQDN GetFQDNFromNamingContext = strTemp End Function '----------------------------------------+---------------------------------------- ' End of GetFQDN.VBS '----------------------------------------+----------------------------------------Copyright © 2003, Christopher G. Lewis
|
Wednesday, September 17, 2003
Script to parse a URL
Here's a little script that parses a URL and returns the Protocol, host and path. It uses Regular Expressions and the Match/SubMatch collections.
|
<?xml version="1.0" ?> <package> <comment> '----------------------------------------+---------------------------------------- ' ParseURL.wsf - Script to parse a URL in the form ' prot://Host/path/file ' ' Copyright© 2003, Christopher G. Lewis - HTTP://www.ChristopherLewis.com ' ' Date Name Comment ' ------ ------- ----------------------------------------------------------------- ' 030916 CGL Created '----------------------------------------+---------------------------------------- </comment> <job> <runtime> <description> ParseURL.wsf (Version 1.0.00) Script to parse a URL in the form prot://Host/path/file </description> <!-- Command line arguments --> <unnamed name="URL" helpstring="" type="boolean" required="true" /> <example> ParseURL.wsf http://slashdot.org - or - ParseURL.wsf ftp://ftp.sunsite.dk/projects/wget/windows/wget20030915b.zip </example> </runtime> <script language="VBScript"> <![CDATA[ Option Explicit 'Check for CScript If Not IsCScript() Then WScript.Echo WScript.ScriptName & " must be run with CScript." WScript.Quit 1 End If 'Start main processing Call Main '----------------------------------------+---------------------------------------- ' Main - Main sub ' ' Date Name Comment ' ------ ------- ----------------------------------------------------------------- ' 030729 CGL Created '----------------------------------------+---------------------------------------- Sub Main() Dim strURL Dim objRegEx Dim colMatches Dim objMatch Dim strProtocol Dim strHost Dim strPath 'Parse Command Line If WScript.Arguments.Unnamed.Count = 0 Then PrintSyntax() Wscript.Echo " URL is required." Else strURL = Trim(WScript.Arguments.Unnamed(0)) Wscript.Echo "Parsing URL: " & strURL End if 'Parse URL Set objRegEx = New RegExp objRegEx.pattern = "^([\w]*://)([\w\-\.]*)([\w\.\-/]*)" objRegEx.Global = true If objRegEx.Test(strURL) Then Set colMatches = objRegEx.Execute(strURL) Set objMatch = colMatches(0) If objMatch.SubMatches.Count > 0 Then strProtocol = objMatch.SubMatches(0) End if If objMatch.SubMatches.Count > 1 Then strHost = objMatch.SubMatches(1) End if If objMatch.SubMatches.Count > 2 Then strPath = objMatch.SubMatches(2) End if Wscript.Echo "Prot: " & strProtocol Wscript.Echo "Host: " & strHost Wscript.Echo "Path: " & strPath Else wscript.echo "Input is an invalid URL" End if End Sub '----------------------------------------+---------------------------------------- ' IsCscript - Checks CScript vs. WScript ' ' Date Name Comment ' ------ ------- ----------------------------------------------------------------- ' 020507 CGL Created '----------------------------------------+---------------------------------------- Function IsCScript() Dim objRegExp Set objRegExp = New RegExp objRegExp.IgnoreCase = True objRegExp.Pattern = "cscript.exe$" IsCScript = objRegExp.Test(WScript.FullName) Set objRegExp = Nothing End Function '----------------------------------------+---------------------------------------- ' PrintSyntax - PrintSyntax ' ' Date Name Comment ' ------ ------- ----------------------------------------------------------------- ' 030606 CGL Created '----------------------------------------+---------------------------------------- Function PrintSyntax() WScript.Arguments.ShowUsage() End Function '----------------------------------------+---------------------------------------- ' End of ParseURL.wsf '----------------------------------------+---------------------------------------- ]]> </script> </job> </package>Copyright © 2003, Christopher G. Lewis
|