none
Adding home drive attributes to accounts created with New-Mailbox cmdlet RRS feed

  • Question

  • I would like to know the best way to add AD attributes to a script created using the New-Mailbox cmdlet? Would it be a case of running a new script after the script has run that creates the new Mailbox, or can I incorporate into the same script?

    I cannot see parameters like HomDirectory or HomeDrive in the New-Mailbox cmdlet which leads me to beleive this cannot be done from there and needs to be passed to another script.. The problem is I do not know how to go about this?

     

    Wednesday, October 19, 2011 8:58 AM

Answers

  • You're so close, I think you just need to add this one line (highlighted):

     

    Function ReadCSV {
        Param([string]$filename)
        $users = Import-Csv $filename
        foreach ($user in $users) {
        $ht = @{
           "givenName" = $user.firstname
           "sn" = $user.surname
           "displayName" = $user.displayname
           "alias" = $user.alias
           "sAMAccuntName" = $user.sam
           "userPrincipalName" = $user.upn
           "database" = $user.mailbox
           "organizationalUnit" = $user.ou
           "name" = ($user.firstname + " " + $user.surname)
           }
           Write-Output $ht
        }
     }
     Function CreateUser {
        Param($userInfo)
        $secureString = ConvertTo-SecureString "Password1" -AsPlainText -Force
        New-mailbox -name $userInfo["name"]`
      -Alias $userInfo["Alias"]`
      -userPrincipalName $userInfo["userPrincipalName"]`
      -sAMAccountName $userInfo["alias"]`
      -database $userInfo["Database"]`
      -FirstName $userInfo["givenName"]`
      -LastName $userInfo["surname"]`
      -OrganizationalUnit $userInfo["organizationalUnit"]`
      -DisplayName $userInfo["DisplayName"]`
      -Password $SecureString -ResetPasswordOnNextLogon $true
      Set-AdUser -identity $userInfo["sam"] -HomeDirectory "\\users\$($userInfo["alias"])" -homedrive "X:"   
    }
        
    Function CreateMailbox {
        PROCESS {CreateUser $_}
     }
      
     ReadCSV C:\Script\UserList.csv | CreateMailbox
    

     


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"


    • Edited by Bigteddy Tuesday, October 25, 2011 6:23 AM Changed code according to Richard's suggestion
    • Marked as answer by Beefstew123 Tuesday, October 25, 2011 12:45 PM
    Thursday, October 20, 2011 10:30 AM

All replies

  • you cannot user new-mailbox to do this, as you mentioned.

    but you can instead use set-aduser cmdlet (need ad modules to be loaded)

    this has two parameters homedrive and homedirectory.

    eg:

    set-aduser -identity "john" -HomeDirectory "\\users\john" -homedrive "X:"

    more over here:

    http://technet.microsoft.com/en-us/library/ee617215.aspx


    -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 19, 2011 10:15 AM
    Moderator
  • Do you mean import AD module from EMS as I have tried this and its not letting me do it

    Thanks

    Thursday, October 20, 2011 8:45 AM
  • are you getting errors?
    you can also load it part of your profile.

    or modify the shortcut like this to load the module when EMS opens up

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command “. ‘C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1′; Connect-ExchangeServer -auto ; Import-Module ActiveDirectory”


    -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.
    Thursday, October 20, 2011 9:18 AM
    Moderator
  • Its working now but I'm just having trouble working out how I would incorporate adding the home drive info into the following script:

    Function ReadCSV {
       Param([string]$filename)
       $users = Import-Csv $filename
       foreach ($user in $users) {
       $ht = @{
          "givenName" = $user.firstname
          "sn" = $user.surname
          "displayName" = $user.displayname
          "alias" = $user.alias
          "sAMAccuntName" = $user.sam
          "userPrincipalName" = $user.upn
          "database" = $user.mailbox
          "organizationalUnit" = $user.ou
          "name" = ($user.firstname + " " + $user.surname)
          }
          Write-Output $ht
       }
    }
    Function CreateUser {
       Param($userInfo)
       $secureString = ConvertTo-SecureString "Password1" -AsPlainText -Force
       New-mailbox -name $userInfo["name"]`
     -Alias $userInfo["Alias"]`
     -userPrincipalName $userInfo["userPrincipalName"]`
     -sAMAccountName $userInfo["alias"]`
     -database $userInfo["Database"]`
     -FirstName $userInfo["givenName"]`
     -LastName $userInfo["surname"]`
     -OrganizationalUnit $userInfo["organizationalUnit"]`
     -DisplayName $userInfo["DisplayName"]`
     -Password $SecureString -ResetPasswordOnNextLogon $true
      
    }
      
    Function CreateMailbox {
       PROCESS {CreateUser $_}
    }
     
    ReadCSV C:\Script\UserList.csv | CreateMailbox

    • Marked as answer by Beefstew123 Tuesday, October 25, 2011 12:45 PM
    • Unmarked as answer by Beefstew123 Tuesday, October 25, 2011 12:45 PM
    Thursday, October 20, 2011 10:04 AM
  • You're so close, I think you just need to add this one line (highlighted):

     

    Function ReadCSV {
        Param([string]$filename)
        $users = Import-Csv $filename
        foreach ($user in $users) {
        $ht = @{
           "givenName" = $user.firstname
           "sn" = $user.surname
           "displayName" = $user.displayname
           "alias" = $user.alias
           "sAMAccuntName" = $user.sam
           "userPrincipalName" = $user.upn
           "database" = $user.mailbox
           "organizationalUnit" = $user.ou
           "name" = ($user.firstname + " " + $user.surname)
           }
           Write-Output $ht
        }
     }
     Function CreateUser {
        Param($userInfo)
        $secureString = ConvertTo-SecureString "Password1" -AsPlainText -Force
        New-mailbox -name $userInfo["name"]`
      -Alias $userInfo["Alias"]`
      -userPrincipalName $userInfo["userPrincipalName"]`
      -sAMAccountName $userInfo["alias"]`
      -database $userInfo["Database"]`
      -FirstName $userInfo["givenName"]`
      -LastName $userInfo["surname"]`
      -OrganizationalUnit $userInfo["organizationalUnit"]`
      -DisplayName $userInfo["DisplayName"]`
      -Password $SecureString -ResetPasswordOnNextLogon $true
      Set-AdUser -identity $userInfo["sam"] -HomeDirectory "\\users\$($userInfo["alias"])" -homedrive "X:"   
    }
        
    Function CreateMailbox {
        PROCESS {CreateUser $_}
     }
      
     ReadCSV C:\Script\UserList.csv | CreateMailbox
    

     


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"


    • Edited by Bigteddy Tuesday, October 25, 2011 6:23 AM Changed code according to Richard's suggestion
    • Marked as answer by Beefstew123 Tuesday, October 25, 2011 12:45 PM
    Thursday, October 20, 2011 10:30 AM
  • Bigteddy, shouldn't the user be identified by sAMAccountName in the Set-ADUser cmdlet? For example;

     

    Set-AdUser -identity $userInfo["alias"] -HomeDirectory "\\users\$($userInfo["alias"])" -homedrive "X:"
    

    I notice $User.sam is never used. Maybe it should be:

    Set-AdUser -identity $userInfo["sam"] -HomeDirectory "\\users\$($userInfo["alias"])" -homedrive "X:"
    


    Richard Mueller - MVP Directory Services
    Monday, October 24, 2011 10:56 PM
    Moderator
  • Hi Richard, I didn't have Set-ADUser to play with so I'm sure you're right.  I have amended my code accordingly.
    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Tuesday, October 25, 2011 6:22 AM