Link to home
Start Free TrialLog in
Avatar of The_Spaz
The_Spaz

asked on

Power shell script and Exchange 2007 help, please

We have a very different environment where our login is lastname.first (lastname up to a maximum of seven characters and then the first initial)  but I am not sure how to set this up in a powershell script.  The following is what I have so far and it works but it creates the full lastname.firstname   and not what I need which is the lastname up to a maximum of seven characters.first initial of the firstname.  So if the name is  mr Goodlooking the name standard would goodloo.m  for the UPN .  Can someone tell me if this can be done or not and if so what ti would be.  Thanks!
## Run BulkImport.ps1 c:\temp\importusers.csv
## Import data from csv and store it in variable 'data' 
 
$data = import-csv $args[0] 
 
## Function to convert password into a secure string 
 
function New-SecureString([string] $plainText) 
{
   $secureString = new-object System.Security.SecureString 
 
   foreach($char in $plainText.ToCharArray())
   {
      $secureString.AppendChar($char)
   } 
 
   $secureString
} 
 
foreach ($i in $data)
{ 
 
$ss = new-securestring $i.password
$upn = $i.lastname + "." + $i.firstname + "@" + "bch.local"
 
new-mailbox -Password $ss -Alias $i.alias -LastName $i.lastname -Firstname $i.firstname -Name $i.name -Database $i.database -UserPrincipalName $upn -OrganizationalUnit $i.ou
} 
 
and my sample .csv file
Alias,LastName,FirstName,Name,Password,OU,Database
mgoodlooking,Goodlooking,Mr,Goodlooking Mr,snoopy3@mybch.org,TEMP,Users,External-Medium-F-J

Open in new window

Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image


Hey,

Yep it's possible. Something like this would do it.

Chris
## Run BulkImport.ps1 c:\temp\importusers.csv
## Import data from csv and store it in variable 'data' 
 
$data = import-csv $args[0] 
 
## Function to convert password into a secure string 
 
function New-SecureString([string] $plainText) 
{
   $secureString = new-object Security.SecureString 
   foreach($char in $plainText.ToCharArray()) { $secureString.AppendChar($char) } 
   $secureString
} 
 
foreach ($i in $data)
{ 
   $ss = new-securestring $i.password
 
  # Build the UPN
  $UPN = $i.lastname
  # If the last name length is greater than 7 characters trim off the end
  If ($UPN.Length -gt 7) { $UPN = $UPN.SubString(0, 7) }
  # Concatenate with the first character of the first name and the domain name
  $UPN = "$UPN.$($i.firstname.SubString(0, 1))@bch.local"
 
  new-mailbox -Password $ss -Alias $i.alias -LastName $i.lastname -Firstname $i.firstname `
    -Name $i.name -Database $i.database -UserPrincipalName $upn -OrganizationalUnit $i.ou
} 

Open in new window

Avatar of The_Spaz
The_Spaz

ASKER

Man that was quick. If you can add one more thing than I will be very glad to award the points as I just checked that it does work.  Any idea at the new-mailbox piece on how to add that new user also to the active directory group citrix_staff as i notice when they get created they only belong to Domain Users.  P.s. I have no problem adding and extra column in my .csv script with the exact group(s) name for each user if that helps.  

Is "citrix_staff " a distribution group (mail enabled)?

If so it should work with this extra command.

If it's not mail enabled it's a tad more complex, so I haven't included it unless you need it :)

Chris
## Run BulkImport.ps1 c:\temp\importusers.csv
## Import data from csv and store it in variable 'data' 
 
$data = import-csv $args[0] 
 
## Function to convert password into a secure string 
 
function New-SecureString([string] $plainText) 
{
   $secureString = new-object Security.SecureString 
   foreach($char in $plainText.ToCharArray()) { $secureString.AppendChar($char) } 
   $secureString
} 
 
foreach ($i in $data)
{ 
   $ss = new-securestring $i.password
 
  # Build the UPN
  $UPN = $i.lastname
  # If the last name length is greater than 7 characters trim off the end
  If ($UPN.Length -gt 7) { $UPN = $UPN.SubString(0, 7) }
  # Concatenate with the first character of the first name and the domain name
  $UPN = "$UPN.$($i.firstname.SubString(0, 1))@bch.local"
 
  new-mailbox -Password $ss -Alias $i.alias -LastName $i.lastname -Firstname $i.firstname `
    -Name $i.name -Database $i.database -UserPrincipalName $upn -OrganizationalUnit $i.ou
 
  Add-DistributionGroupMember "citrix_staff" -member $UPN
} 

Open in new window

Citrix_staff is actually a global security group in AD group that we need to have them added to (like Domain Users).  So, it is not mail enabled.  Hope that thelps.
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great work.  This works well.