none
Adding mailbox to users created with powershell script RRS feed

  • Question

  • Hi, I would like to add a mailbox for each user created with the following script. Could anyone please tell me the easiest way to incorporate it into this script as there is alot of contradictory stuff out there... Many thanks guys!

     

    $users = import-csv "C:\script\list.csv"
    $objADSI = [ADSI]"LDAP://ou=*Scripted Users,dc=corp,dc=we"
    $users | foreach {

       $UserName = $_.UserName
       $givenName = $_.givenName
       $sn = $_.sn
       $displayName = $_.displayName
       $path = "\\DC01\Home$\$displayName"
       $homeDrive = "H:"
     
       $objUser = $objADSI.Create("User", "cn=$displayName")
       $objUser.Put("sAMAccountName", $userName)
       $objUser.Put("userPrincipalName", $userName + "@corp.we")
       $objUser.Put("homeDrive", $homeDrive)
       $objUser.Put("givenName", $givenName)
       $objUser.Put("sn", $sn)
       $objUser.Put("displayName", $displayName)
       $objUser.Put("homeDirectory", $path)
       $objUser.setInfo()
       $objUser.SetPassword("Password1")
       $objUser.psbase.InvokeSet('AccountDisabled', $false)
       $objUser.Put("pwdlastSet", -0)
       $objUser.setInfo()
      
       if (-not (Test-Path $path)) {
        $acl = (md $path).GetAccessControl()
        $perm = ($userName + "@corp.we"),"Modify","ContainerInherit,ObjectInherit","None","Allow"
        $accessRule = new-object System.Security.AccessControl.FileSystemAccessRule $perm
     $acl.SetAccessRule($accessRule)
     $acl | Set-Acl -Path $Path

       }

    }

    Wednesday, October 12, 2011 7:20 AM

Answers

  • This will set all attributes except the home directory, which will have to be done separately:

     

    New-Mailbox -sAMAccounName $username -UserPrincipalName $userName + '@corp.we' -name $userName -database "Storage Group 1\Mailbox Database 1" -Organizational Unit *Users -FirstName $givenName -LastName $sn -DisplayName $displayname -Password "Password1" 
    

     


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    • Edited by Bigteddy Wednesday, October 12, 2011 10:02 AM
    • Proposed as answer by Kazun Wednesday, October 12, 2011 10:39 AM
    • Marked as answer by Tiger LiMicrosoft employee Thursday, October 13, 2011 2:06 AM
    Wednesday, October 12, 2011 10:01 AM
  • i have setup a bulk mailbox create script , you can look it up here.

    http://www.myexchangeworld.com/2010/02/powershell-provisioning-users/

    EMS is the way to go, itwill automatically create the ad user for you.

     


    -join("74686979616775313440686F746D61696C2E636F6D"-split"(?<=\G.{2})",21|%{[char][int]"0x$_"})
    http://www.myExchangeWorld.com
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Wednesday, October 12, 2011 10:35 AM

All replies

  • If you are going to use the Exchange Management Shell (EMS), you can use the New-Mailbox cmdlet.  This will also create the user, so your code would have to be modified significantly.  See the following help excerpt:

    [PS] C:\Windows\system32>help New-Mailbox

    NAME
        New-Mailbox

    SYNOPSIS
        Use the New-Mailbox cmdlet to create a new user in the Active Directory dir
        ectory service and mailbox-enable this new user.


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Wednesday, October 12, 2011 7:42 AM
  • So am I right in thinking it can be done by both Powershell, and EMS? and if so what is the easiest and most practical way to do it?

    Cheers

    Wednesday, October 12, 2011 7:57 AM
  • It's a bit long to paste here, but I could.  The results from

    Get-Help New-Mailbox -full

    ...will show you that you can create a user, complete with all the attributes you are currently applying with you present script.

    That's why I say, it's a completely new ball game when using the EMS. 

    So, what I'm saying is, are you prepared to re-write your script for the EMS?


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Wednesday, October 12, 2011 8:05 AM
  • I am, yes but just want to be sure that re-writing the script and doing it from EMS is the best and most practical way to go?
    Wednesday, October 12, 2011 8:15 AM
  • As you know, Kazun is the super-expert here, and maybe you should wait for his input.  But if it were me, I would have done this in the first place in EMS.  Up until now, I didn't realise this was where you were heading.

     


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Wednesday, October 12, 2011 8:21 AM
  • This will set all attributes except the home directory, which will have to be done separately:

     

    New-Mailbox -sAMAccounName $username -UserPrincipalName $userName + '@corp.we' -name $userName -database "Storage Group 1\Mailbox Database 1" -Organizational Unit *Users -FirstName $givenName -LastName $sn -DisplayName $displayname -Password "Password1" 
    

     


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    • Edited by Bigteddy Wednesday, October 12, 2011 10:02 AM
    • Proposed as answer by Kazun Wednesday, October 12, 2011 10:39 AM
    • Marked as answer by Tiger LiMicrosoft employee Thursday, October 13, 2011 2:06 AM
    Wednesday, October 12, 2011 10:01 AM
  • i have setup a bulk mailbox create script , you can look it up here.

    http://www.myexchangeworld.com/2010/02/powershell-provisioning-users/

    EMS is the way to go, itwill automatically create the ad user for you.

     


    -join("74686979616775313440686F746D61696C2E636F6D"-split"(?<=\G.{2})",21|%{[char][int]"0x$_"})
    http://www.myExchangeWorld.com
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Wednesday, October 12, 2011 10:35 AM
  • Hi, I have copied your script and edited to I think how it should be for me but its coming back with the following errror:

    Missing expression after unary operator '-'.
    At C:\Script\addmailbox.ps1:27 char:5
    + - <<<< DisplayName $userInfo["DisplayName"] -Password $SecureString -ResetPasswordOnNextLogon $tru
    + CategoryInfo : ParserError: (-:String) [], ParseException
    + FullyQualifiedErrorId : MissingExpressionAfterOperator

    Do you have any ideas on what I might change in the script to resolve this please

    Thanks

    Tuesday, October 18, 2011 3:01 PM
  • the $tru is a typo

    It is actually $true

    Tuesday, October 18, 2011 3:02 PM