Active Directory Site and Subnet List VBScript

January 4th, 2011

This will list Active Directory sites and subnets and display in a MsgBox. Formatting is ugly but that’s easy to change as required.

'Get list of AD subnets

Set oRootDSE = GetObject("LDAP://RootDSE")
sConfigurationNC = oRootDSE.Get("configurationNamingContext")
Set oRootDSE = Nothing 

sSubnetsContainer = "LDAP://cn=Subnets,cn=Sites" & "," & sConfigurationNC
Set oSubnetsContainer = GetObject(sSubnetsContainer)

For Each sResult In oSubnetsContainer

	aSNInfo = Split(sResult.cn, "/")
	If Instr(sResult.siteObject, ",") = 0 Then
		sSN = sSN & aSNInfo(0) & vbTab & vbTab
	Else
		sSN = sSN & aSNInfo(0) & ": " & _
			Mid(Left(sResult.siteObject, Instr(sResult.siteObject, ",") - 1), 4) & _
			vbTab & vbTab
	End if
Next
wscript.echo sSN

To display your computer’s site:

Set adsSystemInfo = CreateObject("ADSystemInfo")
WScript.echo "Your site is: " & adsSystemInfo.SiteName
Set adsSystemInfo = Nothing

Popularity: 12% [?]

HTTP Get Using MSXML2.XMLHTTP Object VBScript

December 23rd, 2010

Use the XMLHTTP object to issue an HTTP Get on a remote web server.

Set objHTTP = CreateObject("MSXML2.XMLHTTP")
objHTTP.open "GET", "http://localhost/default.htm, False
objHTTP.send
strResponse = objHTTP.responseText

Ref: http://msdn.microsoft.com/en-us/library/ms757849(VS.85).aspx

Popularity: 6% [?]

Active Directory LDAP / SQL Query Syntax VBScript

December 23rd, 2010

Sample VBscript to query Active Directory for certain object properties.

Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000
objCommand.Properties("SearchScope") = ADS_SCOPE_SUBTREE
objCommand.Properties("Sort On") = "sn"

'.::LDAP Query Syntax::.
strBase = "<LDAP://dc=contoso,dc=com>" ' Update with your domain
strFilter = "(&(objectCategory=person)(objectClass=user))"
strReturnAttribs = "sn,givenName,mail,sAMAccountName"
strQuery = strBase & ";" & strFilter & ";" & strReturnAttribs & ";subtree"
objCommand.CommandText = strQuery
Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst
Do Until objRecordSet.EOF
   ' Loop through the record set results (each row could be written to a file or echo'd as below or ...)
    Wscript.Echo objRecordSet.Fields("sn").Value & _
"," & objRecordSet.Fields("givenName").Value & _
"," & objRecordSet.Fields("mail").Value & _
"," & objRecordSet.Fields("sAMAccountName").Value & _
objRecordSet.MoveNext
Loop

To use SQL query syntax to return properties for a user, the query might look like:

'.::SQL Syntax Query::.
sUser = "bob"
objCommand.CommandText = _
    "SELECT givenName, sn, mail FROM 'LDAP://dc=contoso,dc=com' WHERE objectCategory='user' AND sAMAccountName='" & sUser & "'"

Popularity: 17% [?]

FileSystemObject Create A Text File VBScript

December 23rd, 2010

Creates a new text file and writes a line to it. That is all.

TargetFile = "myfile.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile(TargetFile, True)      'The 'True' parameter means overwrite existing file if it exists
File.WriteLine "my line inside the file"
objFile.Close
Set objFSO = Nothing

Popularity: 3% [?]

Active Directory Bind VBScript

December 23rd, 2010
'Bind to Active Directory
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("DefaultNamingContext")

Popularity: 3% [?]