Solved

Covert this vbs to .net

Posted on 2014-03-11
4
235 Views
Last Modified: 2014-03-11
I'm trying to convert this from vbs to .net and not haveing much luck.

dim objRecordSet
dim objConnection
dim objCommand
dim user
dim strUser
dim strUserList
Set objFSO = CreateObject("Scripting.FileSystemObject")

'======== Get User ID 
user= InputBox("Enter the ID you want to check","Enter User ID")

'======== Set up ADODB Connection 
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "LDAP Provider;"
Set objCommand =   CreateObject("ADODB.Command")
Set objCommand.ActiveConnection = objConnection


'======== Query LDAP
Titleinfo = getTitle(user)


'======== Function to query LDAP and get the title info.
Function getTitle (userid)
 objCommand.CommandText =  "SELECT TITLE FROM 'LDAP://ldapapi/ou=people,o=my domain' WHERE unixid='" & userid & "'"
 Set objRecordSet = objCommand.Execute
 On Error Resume Next
 While Not objRecordSet.EOF
   For Each i in objRecordSet.fields
      For Each v in i.value

        WScript.Echo(v)	'the info I need
        
      Next
   Next
   objRecordSet.MoveNext
 Wend
 objConnection.close
End Function

wscript.quit

Open in new window

0
Comment
Question by:chadmanvb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39920601
If you want to keep all of the late binding stuff, then a quick-and-dirty conversion could be:

Module Module1

    Sub Main()
        Dim user
        Dim strUser
        Dim strUserList
        Dim objFSO = CreateObject("Scripting.FileSystemObject")

        '======== Get User ID 
        user = InputBox("Enter the ID you want to check", "Enter User ID")

        '======== Query LDAP
        Dim Titleinfo = getTitle(user)
    End Sub

    '======== Function to query LDAP and get the title info.
    Function getTitle(userid)
        Dim objConnection
        Dim objCommand
        Dim objRecordSet

        '======== Set up ADODB Connection 
        objConnection = CreateObject("ADODB.Connection")
        objConnection.Provider = "ADsDSOObject"
        objConnection.Open("LDAP Provider;")
        objCommand = CreateObject("ADODB.Command")
        objCommand.ActiveConnection = objConnection


        objCommand.CommandText = "SELECT TITLE FROM 'LDAP://ldapapi/ou=people,o=my domain' WHERE unixid='" & userid & "'"
        objRecordSet = objCommand.Execute

        On Error Resume Next

        While Not objRecordSet.EOF
            For Each i In objRecordSet.fields
                For Each v In i.value

                    Console.WriteLine(v) 'the info I need

                Next
            Next
            objRecordSet.MoveNext()
        End While

        objConnection.close()

    End Function

End Module

Open in new window


The code could certainly be cleaned up, though.
0
 

Author Comment

by:chadmanvb
ID: 39920639
That worked!  Is there a better way i should be doing this?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39920694
We usually try to avoid late-binding (e.g. CreateObject) in .NET. There is nothing technically wrong with using it, but code is usually less error-prone with early-binding--that is, having the actual types of your objects known at compile time. You might look into some of the classes .NET has available for working with LDAP. Here are a few tutorials:

http://www.codeproject.com/Articles/4237/Querying-Active-Directory-using-NET-classes-and-LD
http://www.codeproject.com/Articles/9570/Querying-Microsoft-Active-Directory-Using-Microsof
0
 

Author Closing Comment

by:chadmanvb
ID: 39920747
Perfect!  Thanks for the help!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Write to a printer using vb.net 9 61
DataGridView / get bound table name? 8 38
Need syntax help building hyperlink 2 42
Create a button class for use in all vb.net apps 5 20
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question