This particular naming provision requires Firstname M. Lastname. However, some accounts have no middle name meaning that there would be an uncessary whitespace in the Display Name.
To solve this I used an IF statement to vary my code during the conidtion of a CSV record having no middle name.
CSV File:
firstname,middle,lastname
Jonathan,M,Test
Jonathan2,,Test2
The key to this is using an IF statement on the CSV
if($_.csvfield){perform action with data in field} else {perform all actions without including any data from empty field}
The program using myorg.com domain FQDN and default "Users" OU:
$password = Read-Host "Enter password" -AsSecureString;
Import-Csv -Path C:\CSV\newtestaccounts.csv | ForEach-Object{
$_.firstname;
$firstNameTemp = $_.firstName;
$lastNameTemp = $_.lastname;
$middle = $_.middle;
if($_.middle)
{
$userPrin = $firstNameTemp.substring(0,1) + $middle + $lastNameTemp + "@myorg.com";
$displayName = $firstnameTemp + " " + $middle + "." + " " + $lastNameTemp;
$alias = $firstNameTemp.substring(0,1) + $middle + $lastNameTemp + "qa";
$userPrin = $userPrin.tolower()
$alias = $alias.tolower()
New-Mailbox -UserPrincipalName $userPrin -Alias $alias -Database "TempDB" -Name $displayName -OrganizationalUnit "myorg.com/Users" -Password $password -FirstName $firstName -LastName $lastName -DisplayName $displayName -ResetPasswordOnNextLogon $false
$alias
}
$userPrin = $firstNameTemp.substring(0,1)+ $lastNameTemp + "@myorg.com";
$displayName = $firstnameTemp + " " + $lastNameTemp;
$alias = $firstNameTemp.substring(0,1) + $lastNameTemp + "qa";
$userPrin = $userPrin.tolower()
$alias = $alias.tolower()
New-Mailbox -UserPrincipalName $userPrin -Alias $alias -Database "TempDB" -Name $displayName -OrganizationalUnit "myorg.com/Users" -Password $password -FirstName $firstName -LastName $lastName -DisplayName $displayName -ResetPasswordOnNextLogon $false
$alias
}
No comments:
Post a Comment