?
Solved

Covert this vbs to .net

Posted on 2014-03-11
4
Medium Priority
?
237 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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month10 days, 16 hours left to enroll

770 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