Solved

Covert this vbs to .net

Posted on 2014-03-11
4
236 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

Technology Partners: 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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

718 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