Welcome to Sign in | Join | Help
in
Home Blog Forums

Powershell with Ex2007

Last post 09-03-2007, 3:35 AM by AndyJG247. 9 replies.

Sort Posts: Previous Next
  •  08-14-2007, 11:51 AM 1177

    Powershell with Ex2007

    Argh... I'm having to learn it now, not sure my brain can cope but here goes.

     


    http://www.msexchange.org/tutorials/Managing-mailboxes-Exchange-Server-2007-Part1.html

     

    shows how to bulk create users with PS with Ex2007 but I'm not sure how to change the csv file so it creates firstname, lastname as well for the users?  Does that make sense?

     

    From the page:

    1. First of all, we will have to create a *.csv file called recipients.csv on the root drive (C:\) and we will type the column names for the file in the first line. Those columns will be the Alias, the Name and UPN. in the following lines we will complete the user information.

    2. Once the user information is complete, we will have to create a variable in the Exchange Management Shell that will keep the initial password for all of the accounts on the recipients.csv file. To do so, we will type the following:

    $Password = Read-Host “Type the default password for the new accounts:” -AsSecureString

    3.  In the following step, we will run two cmdlets using a pipe to create the new users using the *.csv file. This is the syntax of our cmdlet:

    Import-Csv recipients.csv | foreach { New-Mailbox –alias $_.Alias –name $_.Name –UserPrincipalName $_.UPN –Database “mailbox database” –OrganizationalUnit Users –Password $Password –ResetPasswordOnNextLogon:$true

    The options are explained here:
    $_.<Name>: This is the name of each column of the recipients.csv file.
    Foreach: For each line of the file; Note: the first line is the header.
    $Password: variable that we just typed in the previous step.
    -ResetPasswordOnNextLogon:$true: If we set this parameter to true, all the users will have to change their password on the first logon.


    cheers
    Andy
  •  08-15-2007, 8:21 AM 1183 in reply to 1177

    Re: Powershell with Ex2007

    In the CSV add column headings for Firstname and Lastname.  Modify the PowerShell expression to use these fields:
     

    Import-Csv recipients.csv | foreach { New-Mailbox –alias $_.Alias –name $_.Name –UserPrincipalName $_.UPN –Database "mailbox database" –OrganizationalUnit Users –Password $Password –ResetPasswordOnNextLogon:$true -firstname $_.Firstname -lastname $_.Lastname }
     


    Jeffery Hicks
    Microsoft PowerShell MVP
    SAPIEN Technologies - Scripting, Simplified.

    "Those who forget to script it are doomed to repeat it."
  •  08-15-2007, 8:36 AM 1186 in reply to 1183

    Re: Powershell with Ex2007

    Ah fantastic.  I will give that a go.  Just going through the basics of learning it so could take some time!

    The end result is I want to use the Ex2007 to create 1500 users and assign them mailboxes on a cluster.  I have csv docs with all first/last and I (pending approval) will use first initial dot given name for the mail alias.  Just gotta learn how to do it first, cheers. 


    cheers
    Andy
  •  08-15-2007, 10:48 AM 1190 in reply to 1186

    Re: Powershell with Ex2007

    Hmm.  So can I take the two fields, first name and last name and maybe "create" a third field so I would have part of both, ie  first initial and surname?  %g1%s in exchange I think?

    cheers
    Andy
  •  08-15-2007, 1:21 PM 1199 in reply to 1190

    Re: Powershell with Ex2007

    I think you can things like:

     
    –alias ($_.Firstname + '.' + $_.Lastname)

     or

    –alias (($_.Firstname).SubString(0,1) + '.' + $_.Lastname)
     


    Jeffery Hicks
    Microsoft PowerShell MVP
    SAPIEN Technologies - Scripting, Simplified.

    "Those who forget to script it are doomed to repeat it."
  •  08-15-2007, 5:57 PM 1200 in reply to 1199

    Re: Powershell with Ex2007

    Will give this a go when I get back to the office tomorrow. - Really appreciate your help

     

    cheers 


    cheers
    Andy
  •  08-16-2007, 9:01 AM 1202 in reply to 1200

    Re: Powershell with Ex2007

    On a side note I have this to create a user (mostly from Dan's article).  Does anyone know if I need to create them this way before I create mailboxes or does the mailbox thing create the user as well? 

    $domain = [ADSI] "LDAP://TESTSERVER:389/dc=test,dc=local"
    $TestAccounts = [ADSI] "LDAP://OU=Test Accounts,DC=test,DC=local"
    $newUser = $TestAccounts.Create("user","cn=Test User")
    $newUser.put("title", "Powershell Created User")
    $newUser.put("Description", "Test Descr")
    $newUser.put("givenName", "TestGivenName")
    $newUser.put("sn", "TestSuName")
    $newUser.put("Department", "Sales")
    $newUser.put("userPrincipalName", "Account01@test.local")
    $newUser.put("sAMAccountName", "Account01")
    $newUser.put("userPassword", "Password1")
    $newUser.SetInfo()
    $newUser.put("useraccountcontrol",$newUser.useraccountcontrol.value -band (-bnot 2))
    $newUser.SetInfo()

     

    EDIT:  The password seems to be blank rather than the one I set though. 


    cheers
    Andy
  •  08-16-2007, 4:14 PM 1203 in reply to 1202

    Re: Powershell with Ex2007

    The New-Mailbox cmdlet should create the user and the mailbox. You might have to take another pass at the user object to add more information that New-Mailbox might not.  Another possibility is to use the Quest AD cmdlets. Import your CSV, pipe it to the Quest new user cmdlet and pipe that to New-Mailbox. I have not tried this myself and this may not work that simply.  It depends on what type of objects are outputted and what each cmdlet is expecting as an input object. Still, the Quest cmdlets would be easier to use. And they're free.

    Jeffery Hicks
    Microsoft PowerShell MVP
    SAPIEN Technologies - Scripting, Simplified.

    "Those who forget to script it are doomed to repeat it."
  •  08-31-2007, 10:53 AM 1280 in reply to 1203

    Re: Powershell with Ex2007

    Hmm

    Have tried

    I think you can things like:

    –alias ($_.Firstname + '.' + $_.Lastname)

    –alias (($_.Firstname).SubString(0,1) + '.' + $_.Lastname)

     

    Neither worked but I will have a play again next week - cheers for your help so far though :) 

     


    cheers
    Andy
  •  09-03-2007, 3:35 AM 1285 in reply to 1280

    Re: Powershell with Ex2007

    Of course, if anyone is interested, this is one of those occasions that thinking out of the box helps!  Rather than using Powershell to re-create the UPN or change the Alias because the csv file isn't adequate how about just using excel.  Seems so simple now....  You can concatenate the first name and last name fields by adding  a row of "." and a row of "@joebloggs.com" and then get whatever you want from that.

    cheers
    Andy
View as RSS news feed in XML


All postings are provided "AS IS" with no warranties, and confer no rights.
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.