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

Visual Basic.NET

Avatar of undefined
Last Comment
wdosanjos

8/22/2022 - Mon
Bob Learned

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

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.
Bob Learned

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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
wdosanjos

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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!
wdosanjos

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

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