Jump to content

Check out our Community Blogs


Member Since 12 Dec 2011
Offline Last Active Mar 22 2012 05:02 AM

Topics I've Started

Help adding users to AD using LDAP

21 March 2012 - 06:17 AM

Hey all,

I'm writing an application that will add users to Active Directory. I'm trying to use this code to connect to the "Users" shared folder in AD


However it adds the user in with the shared folders, instead of within the "Users" shared folder. Shouldn't CN=Users mean it will add it to the "Users" folder?


Can't figure out what to put for argument

08 March 2012 - 10:32 AM

Hey guys,

I'm using a snippet of code that can be found here


It is a sub that is meant to add a user to a group in Active Directory. However, I can't seem to figure out how to use it. Here is the code:

''' <summary>
        ''' Method to add a user to a group
        ''' </summary>
        ''' <param name="de">DirectoryEntry to use</param>
        ''' <param name="deUser">DirectoryEntry (User) to use</param>
        ''' <param name="GroupName">Group name (in string formation) to search and ultimately add user to</param>
        Public Shared Sub AddUserToGroup(ByVal de As DirectoryEntry, ByVal deUser As DirectoryEntry, ByVal GroupName As String)
            Dim deSearch As DirectorySearcher = New DirectorySearcher()
            deSearch.SearchRoot = de
            deSearch.Filter = "(&(objectClass=group) (cn=" & GroupName & "))"
            Dim results As SearchResultCollection = deSearch.FindAll()
            Dim isGroupMember As Boolean = False
            If results.Count > 0 Then
                Dim group As New DirectoryEntry(results(0).Path)
                Dim members As Object = group.Invoke("Members", Nothing)
                For Each member As Object In CType(members, IEnumerable)
                    Dim x As DirectoryEntry = New DirectoryEntry(member)
                    Dim name As String = x.Name
                    If name <> deUser.Name Then
                        isGroupMember = False
                        isGroupMember = True
                        Exit For
                    End If
                Next member
                If (Not isGroupMember) Then
                    group.Invoke("Add", New Object() {deUser.Path.ToString()})
                End If
            End If
        End Sub

I'm not sure what to put for the first argument when calling it. For example:

AddUserToGroup(?, User, Group)


Load events not starting when running as another user

15 February 2012 - 08:50 AM

Hey all,

I've created an application that is supposed to map a network drive as soon as it starts using this basic sub:

Private Sub DesktopAssistant_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        MapDrive("B", "\\fs\diskfarm\downloads")

    End Sub

Here is the code for the MapDrive function:

Imports System.Runtime.InteropServices

Module map_unmapdrive

    Public Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" _
(ByRef lpNetResource As NETRESOURCE, ByVal lpPassword As String, _
ByVal lpUserName As String, ByVal dwFlags As Integer) As Integer

    Public Declare Function WNetCancelConnection2 Lib "mpr" Alias "WNetCancelConnection2A" _
  (ByVal lpName As String, ByVal dwFlags As Integer, ByVal fForce As Integer) As Integer

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure NETRESOURCE
        Public dwScope As Integer
        Public dwType As Integer
        Public dwDisplayType As Integer
        Public dwUsage As Integer
        Public lpLocalName As String
        Public lpRemoteName As String
        Public lpComment As String
        Public lpProvider As String
    End Structure

    Public Const ForceDisconnect As Integer = 1
    Public Const RESOURCETYPE_DISK As Long = &H1

    Public Function MapDrive(ByVal DriveLetter As String, ByVal UNCPath As String) As Boolean

        Dim nr As NETRESOURCE
        Dim strUsername As String
        Dim strPassword As String

        nr = New NETRESOURCE
        nr.lpRemoteName = UNCPath
        nr.lpLocalName = DriveLetter & ":"
        strUsername = Nothing '(add parameters to pass this if necessary)
        strPassword = Nothing '(add parameters to pass this if necessary)
        nr.dwType = RESOURCETYPE_DISK

        Dim result As Integer
        result = WNetAddConnection2(nr, strPassword, strUsername, 0)

        If result = 0 Then
            Return True
            Return False
        End If
    End Function

    Public Function UnMapDrive(ByVal DriveLetter As String) As Boolean
        Dim rc As Integer
        rc = WNetCancelConnection2(DriveLetter & ":", 0, ForceDisconnect)

        If rc = 0 Then
            Return True
            Return False
        End If

    End Function

End Module

It works perfectly fine when I start the application, but when I do RunAs and start the application as another user, the MapDrive function does not seem to execute. Everything else in the application works when running as another user, just not on-load events. Does anybody know why?

Prompting for user credentials when starting application

13 February 2012 - 10:34 AM

Hey guys,

I'm developing an application for our IT Helpdesk to do some common tasks like installs, drive mapping, etc...

However this application needs to be run as administrator. Is there a way to get the application to bring up the RunAs prompt as soon as it runs, so the user doesn't have to do a Shift + Right click > Runas?


Adding an appointment to a shared calendar in Outlook?

17 January 2012 - 11:39 AM

Hey all,

I'm trying to write a program that will take input and create an appointment in a shared calendar. The code I have will allow me to create an appointment in my own calendar, but I'm not sure what line of code I need to change it to the shared one.


Private Sub btnEnterDate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnterDate.Click  


03        Dim olApp As Outlook.Application  

04        olApp = CreateObject("Outlook.Application")  


06        Dim olNs As Outlook.NameSpace  

07        olNs = olApp.GetNamespace("MAPI")  

08        olNs.Logon()  


10        Dim strUserName As String 

11        Dim strBlackberryNumber As String 

12        Dim strAssignmentDate As String 

13        Dim strExpectedReturnDate As String 


15        strUserName = Me.txtUserName.Text  

16        strBlackberryNumber = Me.txtBlackberryNumber.Text  

17        strAssignmentDate = Me.txtAssignmentDate.Text  

18        strExpectedReturnDate = Me.txtExpectedReturnDate.Text  


20        ' Create an Outlook application.  

21        Dim oApp As Outlook.Application = New Outlook.Application()  


23        ' Create a new AppointmentItem.  

24        Dim oAppt As Outlook.AppointmentItem = oApp.CreateItem(Outlook.OlItemType.olAppointmentItem)  


26        ' Set some common properties.  

27        oAppt.Subject = strUserName  

28        oAppt.Body = strUserName  

29        oAppt.Location = strBlackberryNumber  


31        oAppt.Start = Convert.ToDateTime(strAssignmentDate)  

32        oAppt.End = Convert.ToDateTime(strExpectedReturnDate)  

33        oAppt.ReminderSet = True 

34        oAppt.ReminderMinutesBeforeStart = 5  

35        oAppt.BusyStatus = Outlook.OlBusyStatus.olBusy  

36        oAppt.IsOnlineMeeting = False 


38        ' Save to Calendar.  

39        oAppt.Save()  


41        ' Clean up.  

42        oApp = Nothing 

43        oAppt = Nothing 


45    End Sub 

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download