Link to home
Start Free TrialLog in
Avatar of bpl5000
bpl5000

asked on

Need to convert VB Script code to VB.NET

I have been using vb scripts and now I want to start using VB.NET.  I have used VB6, but VB.NET is new to me.  Can someone help me convert this vb script program to vb.net?

strInput = InputBox("Enter Group Name:")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("members.txt", True)
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") = 9999
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.CommandText = "SELECT Name, ADSPath FROM 'LDAP://dc=bcsd,dc=local' WHERE objectCategory='group' " 
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Set WshShell = WScript.CreateObject("WScript.Shell")

bFound = False

Do Until objRecordSet.EOF
	If Trim(LCase(strInput)) = Trim(LCase(objRecordSet.Fields("Name").Value)) Then
		bFound = True
		Set oGroup = GetObject(objRecordSet.Fields("ADSPath").Value)
		arrMemberOf = oGroup.GetEx("member")
		For Each member In arrMemberOf
			Set oUser = GetObject("LDAP://" & member)
			objFile.WriteLine oUser.cN
		Next
		Exit Do
	End If
	objRecordSet.MoveNext
Loop
If bFound Then
	objFile.Close
	Call WshShell.Run("%comspec% /c sort.exe members.txt /o members.txt",0)
	Call WshShell.Run("members.txt")
Else
	MsgBox "Group """ & strInput & """ not found"
End If

Open in new window

Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Since this site is about knowledge and learning, how may I help you?  What kind of problems are you trying to solve?  I see an LDAP lookup for common name.

The System.DirectoryServices is useful when working with LDAP.

Sample:

Accessing LDAP User list using VB.NET
http://www.codeproject.com/KB/IP/LDAP_Using_VBnet.aspx

Public Function GetAllUsers(ByVal ldapServerName As String) As Hashtable 

 'To retrieve list of all  LDAP users 
 'This function returns HashTable

 _ldapServerName = ldapServerName

 Dim sServerName As String = "mail"

 Dim oRoot As DirectoryEntry = New DirectoryEntry("LDAP://" & ldapServerName & _
       "/ou=People,dc=mydomainname,dc=com")
 
 Dim oSearcher As DirectorySearcher = New DirectorySearcher(oRoot)
 Dim oResults As SearchResultCollection
 Dim oResult As SearchResult
 Dim RetArray As New Hashtable()

  oSearcher.PropertiesToLoad.Add("uid")
  oSearcher.PropertiesToLoad.Add("givenname")
  oSearcher.PropertiesToLoad.Add("cn")
  oResults = oSearcher.FindAll     

  For Each oResult In oResults

   If Not oResult.GetDirectoryEntry().Properties("cn").Value = "" Then
    RetArray.Add( oResult.GetDirectoryEntry().Properties("uid").Value, _
      oResult.GetDirectoryEntry().Properties("cn").Value)
   End If

  Next

  Return RetArray

 End Function

Open in new window

Avatar of bpl5000
bpl5000

ASKER

I was just looking to reuse the code I have.  I added the reference for ADODB and I have the following code already converted...

        Dim strInput As String = TextBox1.Text
        Dim objWriter As New System.IO.StreamWriter("members.txt")
        Dim objConnection As New ADODB.Connection
        Dim objCommand As New ADODB.Command
        objConnection.Provider = "ADsDSOObject"

        Const ADS_SCOPE_SUBTREE = 2

        objConnection.Open("Active Directory Provider")
        objCommand.ActiveConnection = objConnection

Open in new window


This seems to be okay, but when I try to add the following lines...

        objCommand.Properties("Page Size") = 9999
        objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

These statements produce a "Property item is ReadOnly" error.  I'm sure there is just a subtle difference to the way this statement needs to be done in vb.net, but I can't seem to find the correct way.
If you want to reuse old VB6/VBScript code, in place of System.DirectoryServices code, then I don't believe that I am in a position to help you.
ASKER CERTIFIED SOLUTION
Avatar of wdosanjos
wdosanjos
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bpl5000

ASKER

That's awesome!  I'm new to vb.net so can you explain what is meant by "Add a reference to namespace Microsoft.VisualBasic (Microsoft.VisualBasic.dll)"?

Thanks for the help!
Under Project Properties / References make sure that Microsoft.VisualBasic is checked as indicated below.

(Right click on the project and select Preferences)
User generated image