Need to convert VB Script code to VB.NET

bpl5000
bpl5000 used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2008

Commented:
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

Author

Commented:
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.
Most Valuable Expert 2012
Top Expert 2008

Commented:
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.
Top Expert 2011
Commented:
Here is the VB.NET version of the code you posted.  It compiles, but I have not tested it:

' Add a reference to namespace Microsoft.VisualBasic (Microsoft.VisualBasic.dll)
Dim strInput As String
strInput = InputBox("Enter Group Name:")
Dim objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFile = objFSO.CreateTextFile("members.txt", True)
Const ADS_SCOPE_SUBTREE = 2
Dim objConnection = CreateObject("ADODB.Connection")
Dim objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open("Active Directory Provider")
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' " 
Dim objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
'Dim WshShell = WScript.CreateObject("WScript.Shell")

Dim bFound As Boolean = False

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

Open in new window

Author

Commented:
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!
Top Expert 2011

Commented:
Under Project Properties / References make sure that Microsoft.VisualBasic is checked as indicated below.

(Right click on the project and select Preferences)
ProjectProperties-References

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial