AD Creation Script via New-Mailbox

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 9 months ago.

  • Author
    Posts
  • #6238

    by brycspain at 2012-12-20 08:23:23

    I'm scipting some new account creations via the new-mailbox cmdlet and this works fine however, when I try to connect to the AD account to add the remaining properties (these don't exist in New-Mailbox) my script merely creates the account and the email portion and ends. I receive no error messages. I thought feeding get-aduser the pipeline variable might be the issue however I was able to replicate this in a small test script and it worked fine. Any help would be greatly appreciated.


    $Date = Get-Date
    $Logname = "{0:yyyyMMdd}.txt" -f (Get-Date)
    $log = New-Item -path c:\temp -name $Logname -type file -force

    $OurOUPath = "myou"
    $Domain = "mydomain.com"
    $CSVPath = "C:\temp\lausers.csv"
    $DC = "mydc.mydomain.com"

    $Session = New-PSSession -configurationName Microsoft.Exchange -ConnectionUri http://exmb01.mydomain.com/powershell
    Import-PSSession $Session -allowclobber

    Import-Csv $CSVPath | foreach {
    $Error.Clear()

    $a = $_.givenname.ToUpper().substring(0,1)
    $b = $_.sn.ToUpper().substring(0,1)
    $C = $_.givenname.substring(0,1)
    $userprinciple = $a + $_.sn + "@" + $Domain
    $Commonname = $_.sn + ", " + $C
    $password = $a + $b + "new" + $Date.year
    $SecPassword = ConvertTo-SecureString $password -AsPlainText -force
    $EmailAddress = $_.givenname + "." + $_.sn + "@" + $Domain
    $DisplayName = $_.sn + ", " + $_.givenName
    $name = $DisplayName

    New-Mailbox -name $DisplayName -UserPrincipalName $userprinciple -Alias $_.samaccountname -Database $_.database `
    -OrganizationalUnit $OurOUPath -Password $SecPassword -FirstName $_.givenname -LastName $_.sn `
    -DisplayName $DisplayName -SamAccountName $_.Samaccountname -Domaincontroller $DC -ResetPasswordOnNextLogon $false

    $newuser = Get-ADUser $_.samaccountname
    $newuser.Title = $_.Title
    $newuser.st = $_.st
    $newuser.Phonenumber = $_.PhoneNumber
    $newuser.City = $_.City
    $newuser.Company = $_.Company
    $newuser.Department = $_.Department
    $newuser.Homedirectory = $Homepath
    $newuser.Description = $_.Description
    $newuser.PostalCode = $_.PostalCode
    $newuser.StreetAddress = $_.StreetAddress
    $newuser.EmployeeID = $_.EmployeeID

    Set-ADUser -instance $newuser

    if($error -ne $null) {
    Add-Content $log "ERROR: Some attribute was not changed."
    $error.clear()}
    Else {
    Add-Content $log "Attributes were changed."
    }

    }
    Get-PSSession | Remove-PSSession

    by DonJ at 2012-12-20 14:01:29

    I've never seen anyone use that exact approach before... to me, it would make more sense to just run Set-ADUser with the appropriate parameters:

    Set-ADUser -Identity $_.samaccountname -Title whatever -EmployeeID whatever

    Etc. You might also put some code in there to make sure $_ contains what you think it does by that point.

    by brycspain at 2012-12-21 06:34:38

    Thanks for your attention on this Don. You book writing guys play chess when you're scripting and guys like me play checkers =) Regardless, my approach should still obtain the results I need. I went ahead and changed up the script per your suggestion (nice use of set-aduser) and I received the exact same results. AD Account and Email get created and nothing else...no error messages, etc. Could there be some disconnect with the Activedirectory module since I am importing the new-mailbox cmdlet with this snippet?


    $Session = New-PSSession -configurationName Microsoft.Exchange -ConnectionUri http://exmb01.mydomain.com/powershell
    Import-PSSession $Session -allowclobber

    The pipeline variable $_.samaccountname contains the correct information.

    Is there maybe a better way of creating the email and AD account at the same time perhaps?

    by RichardSiddaway at 2013-01-03 10:37:36

    I've found in the past that creating the account and then the mailbox can cause problems if Exchange uses a different DC to the one you are connected to. If the new account hasn't replicated you will get problems. Ensure you are connected to the DC that Exchange is using or put a delay in your script (start-sleep) between the account creation and the mailbox creation

You must be logged in to reply to this topic.