CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Help Please!

  1. #1
    Join Date
    Jun 2016
    Posts
    2

    Help Please!

    I've been fighting this for 3 days and I'm about to explode. Any help would be greatly appreciated.
    I have a win form app that connects to an Exchange 2010 server via PS to create Active Directory accounts and their associated mailboxes.
    When the create sub gets its info from textboxes it works great. It will create account after account after account with no issues. I want to add a bulk user create function that will read the required info from a txt file and then create the accounts. The bulk create will not create more than 1 account, although it loops through the whole txt file just fine.
    I'm thinking maybe it has something to do with the PS connection to the exchange server, but like I said, it works perfectly if the accounts are created one at time, just not in bulk from the txt file.
    I have a built in check that checks to see if the account already exists, and if it does, just move on to the next line. The funny thing is, if I have 3 accounts in the text file and the 1st account already exists, it will skip it, create the second account and then fail to create the 3rd account (no it doesnt show any errors)
    A second set of eyes would be great. Thanks in advance.

    Code:
    createBulkUsersExchange()
    For Each line As String In File.ReadAllLines(LabelBulkUserImportPath.Text)
    Dim parts As String() = line.Split(",")
    
    'SET DISPLAY NAME
    sUserName = parts(0).Trim
    sLastName = parts(1).Trim
    sFirstName = parts(2).Trim
    sMiddleInitial = parts(3).Trim
    
    If sMiddleInitial = Nothing Then
    strDisplayName = Trim(sLastName + ", " + sFirstName)
    Else
    strDisplayName = Trim(sLastName + ", " + sFirstName + " " + sMiddleInitial)
    End If
    'END SET DISPLAY NAME
    
    
    createAccountThroughExchange()
    Next
    End sub
    
    
    createAccountThroughExchange()
    
    MessageBox.Show("START createAccountThroughExchange()" & vbCrLf & "UPN: " + strUPN & vbCrLf & "Name: " + strDisplayName & vbCrLf & "Alias: " + sUserName & vbCrLf & _
    "SamAccountName: " + sUserName & vbCrLf & "FirstName: " + sFirstName & vbCrLf & "LastName: " + sLastName & vbCrLf & _
    "Password: " + strDefaultPassword & vbCrLf & "OrganizationalUnit: " + strUsersOU & vbCrLf & "strUserDN: " & strUserDN)
    
    
    Dim Pass As New System.Security.SecureString
    Dim MyPassword As String = strAdminPassword
    For Each c As Char In MyPassword
    Pass.AppendChar(c)
    Next
    
    Dim credential As PSCredential = New PSCredential(strAdminUsername, Pass)
    
    Dim connectionInfo As New WSManConnectionInfo(New Uri("http://" + strExchangeServer + "/powershell?serializationLevel=Full"),
    http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential)
    
    connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos
    
    Dim ADPassword As String = strDefaultPassword
    
    Dim ssPassword As New SecureString
    For Each c As Char In ADPassword
    ssPassword.AppendChar(c)
    Next
    
    Dim RSpace As Runspace
    
    Try
    RSpace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo)
    Catch ex As Exception
    'Throw New Exception("RSPACE: " & ex.Message)
    'worker.CancelAsync()
    MessageBox.Show("Error connecting to Exchange Server - " + ex.Message)
    Logfile.WriteLine(ex.Message)
    Logfile.WriteLine("==================================")
    End Try
    
    Dim rpass As Boolean = True
    Dim Shell As PowerShell = PowerShell.Create()
    Dim PCommand As New PSCommand
    PCommand.AddCommand("New-Mailbox")
    PCommand.AddParameter("Name", strDisplayName)
    PCommand.AddParameter("UserPrincipalName", strUPN)
    PCommand.AddParameter("Alias", sUserName)
    PCommand.AddParameter("OrganizationalUnit", strUsersOU)
    PCommand.AddParameter("SamAccountName", sUserName)
    PCommand.AddParameter("FirstName", sFirstName)
    PCommand.AddParameter("Initials", sMiddleInitial)
    PCommand.AddParameter("LastName", sLastName)
    PCommand.AddParameter("Password", ssPassword)
    PCommand.AddParameter("ResetPasswordOnNextLogon", rpass)
    PCommand.AddParameter("Database", strExchangeDatabase)
    Shell.Commands = PCommand
    
    RSpace.Open()
    Shell.Runspace = RSpace
    
    Dim commandResults As Collection(Of PSObject)
    commandResults = Shell.Invoke()
    
    RSpace.Close()
    
    RSpace.Dispose()
    RSpace = Nothing
    Shell.Dispose()
    Shell = Nothing
    End Sub
    Last edited by DataMiser; June 16th, 2016 at 07:01 AM. Reason: added code tags

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Help Please!

    Have you checked the results in commandresults?

    Have you tried letting that first message box sit on screen for several seconds between records to see if it was a timing issue?
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Jun 2016
    Posts
    2

    Re: Help Please!

    Sorry..forgot the [code] tags.
    I figured the issue out. It wasn't a timing issue, it was a connection issue. PS didnt like the connection opening and closing for each user during the loop. I brought the connection string outside the loop, kept it open, created the accounts and then closed the connection. Works great now. Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured