12/21/11

Skip empty fields in CSV Import-CSV provision mailboxes Exchange 2010

I was working on a small program to provision mailboxes in a lab Enviroment using the import-csv cmd-let and a CSV file that was generated to meet a customers naming provision.

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

}

12/19/11

Creating some test mailboxes in Exchange 2010 SP1

[PS] C:\Windows\system32>$password = Read-Host -AsSecureString
********

[PS] C:\Windows\system32>for ($i = 1; $i -lt 11; $i++){$name = "MyOrg Test" +
$i; $lastname = "Test " + $i; $userp = $name + "@mydomain.com"; New-Mailbox -Nam
e $name -database "TempDB" -password $password -firstname "MyOrg" -lastname $
lastname -UserPrincipalName $userp}


A simple for loop used to create unquie names for test accounts to use during pre-production testing of the system.

I noticed that this creates the SAM name with spaces so using this .PS1 you can remove spaces from the SAM account name of a mailbox:


Function Remove-Spaces {
param($target)

begin {
filter Do-RemoveSpaces { $_ -replace " ", "" }
}

process { if($_) { $_ Do-RemoveSpaces } }

end { if($target) {$target Do-RemoveSpaces} }
}

for ($i = 1; $i -lt 11; $i++){

$mbx = "MyOrg*" + $i

$samtemp = Get-Mailbox where {$_.name -like $mbx}
$samtempname = $samtemp.SamAccountName

$samtempname = remove-spaces $samtempname

set-mailbox -identity $samtemp -samaccountname $samtempname


That is all for now...