Solved

Covert this vbs to .net

Posted on 2014-03-11
4
225 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
  • 2
  • 2
4 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
That worked!  Is there a better way i should be doing this?
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
Perfect!  Thanks for the help!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now