AD Creation Script via New-Mailbox

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 <br>-OrganizationalUnit $OurOUPath -Password $SecPassword -FirstName $_&#46;givenname -LastName $_&#46;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