Skip to main content

Microsoft

Using PowerShell in Windows Server 2012 to create a simple lab

I’ve been meaning to sit down and spend some time exploring the new Active Directory cmdlets that come with Windows Server 2012 so I decided to use my lab to create some test objects and populate the mailboxes with some messages.
My lab setup is very simple:

  • 1 – Windows Server 2012 domain controller
  • 1 – Exchange 2013 server (hosted on Windows 2012)
  • 1 – Windows 8 client with Office 2013

My goal was to be able to quickly create some test users and groups in a new OU structure, populate the groups with the accounts, and finally populate the mailboxes with some test messages. Here is the script I created to do that. It should be fairly straightforward to follow. There are obviously many other ways to do this. This is just one such way. I ran the script from the Exchange 2013 Management Shell after installing the Active Directory PowerShell module.

For example,

Install-WindowsFeature RSAT-AD-PowerShell

Import-Module ActiveDirectory

 
If you want to just create the AD users and groups you could comment out or remove the section that begins with “Mailbox and mail enable users and groups”. To control the number of users and groups created and the number of test messages to send to each mail object, edit the variables section at the top of the script.
 

# Using Windows Server 2012 Active Directory module to create test accounts

 

# Configure the PowerShell UI settings and clear the screen

$pshost = get-host

$pswindow = $pshost.ui.rawui

 

$newsize = $pswindow.buffersize

$newsize.height = 9999

$newsize.width = 150

$pswindow.buffersize = $newsize

 

$newsize = $pswindow.windowsize

$newsize.height = 50

$newsize.width = 150

$pswindow.windowsize = $newsize

 

$pswindow.windowtitle = "Create Test Environment"

$pswindow.foregroundcolor = "White"

$pswindow.backgroundcolor = "Black"

 

cls

 

# Define variables

 

# Top-level OU

$TestOURootName="Test Objects"

 

# Sub-level OUs

$TestUserOUName="Employees"

$TestGroupOUName="Groups"

$TestContactOUName="Contacts"

$TestMachineOUName="Machines"

 

# Define limits

$numMsgs=1

$numGroups=5

$numUsers=5

 

# Get the current domain root DN

$domainroot=(Get-ADDomain).distinguishedName

 

# Create OU structure

New-ADOrganizationalUnit -Name $TestOURootName -Path $domainroot -ProtectedFromAccidentalDeletion:$false

$TestOURoot=(Get-ADOrganizationalUnit -SearchBase $domainroot -Filter 'name -eq $TestOURootName').distinguishedName

# Create sub OU structure

New-ADOrganizationalUnit -Name $TestUserOUName -Path $TestOURoot -ProtectedFromAccidentalDeletion:$false

New-ADOrganizationalUnit -Name $TestGroupOUName -Path $TestOURoot -ProtectedFromAccidentalDeletion:$false

New-ADOrganizationalUnit -Name $TestContactOUName -Path $TestOURoot -ProtectedFromAccidentalDeletion:$false

New-ADOrganizationalUnit -Name $TestMachineOUName -Path $TestOURoot -ProtectedFromAccidentalDeletion:$false

$TestOUUserPath=(Get-ADOrganizationalUnit -SearchBase $TestOURoot -Filter 'name -eq $TestUserOUName').distinguishedName

$TestOUGroupsPath=(Get-ADOrganizationalUnit -SearchBase $TestOURoot -Filter 'name -eq $TestGroupOUName').distinguishedName

$TestOUContactsPath=(Get-ADOrganizationalUnit -SearchBase $TestOURoot -Filter 'name -eq $TestContactOUName').distinguishedName

$TestOUMachinesPath=(Get-ADOrganizationalUnit -SearchBase $TestOURoot -Filter 'name -eq $TestMachineOUName').distinguishedName

 

 

# Create test accounts

$passwd = ConvertTo-SecureString -String "P@ssw0rd" -AsPlainText -Force

1..$numUsers|% {$userID="{0:d2}" -f $_;Write-Host "Creating " -ForegroundColor Yellow -NoNewline;Write-Host "Test Account$userID" -ForegroundColor Green;New-ADUser -Name "Test Account$userID" -AccountPassword $passwd -Company Contoso -DisplayName "Test Account$userID" -Enabled $true -GivenName Test -SamAccountName taccount$userID -Surname Account$userID -UserPrincipalName taccount$userID@norskecentral.com -Path $TestOUUserPath}

$testusers=Get-ADUser -SearchBase $TestOUUserPath -Filter *

 

# Create test groups

1..$numGroups|% {$grpID="{0:d2}" -f $_;Write-Host "Creating " -ForegroundColor Yellow -NoNewline;Write-Host "Group$grpID" -ForegroundColor Green;New-ADGroup -DisplayName "Group$grpID" -GroupCategory Distribution -GroupScope Universal -Name "Group$grpID" -Path $TestOUGroupsPath}

 

 

# Populate groups with newly created test accounts

$testgroups=Get-ADGroup -SearchBase $TestOUGroupsPath -Filter *

 

$groupNum=0

$userNum=0

$upperbound=$testgroups.Count

do {

    if ($groupNum -eq $upperbound){$groupNum=0}

    Add-ADGroupMember -Identity $testgroups[$groupNum].DistinguishedName -Members $testusers[$userNum].sAMAccountName

    $userNum++

    $groupNum++

}

while ($userNum -le $testusers.count -1)

 

Write-Host `r`n

 

# Mailbox and mail enable users and groups

write-host "Creating mailboxes" -ForegroundColor Yellow `r`n

$testusers|%{enable-mailbox -identity $_.distinguishedName}

Write-Host `r`n

write-host "Creating distribution groups" -ForegroundColor Yellow `r`n

$testgroups|%{enable-distributiongroup -identity $_.distinguishedName}|Out-Null

Write-Host `r`n

write-host "Modifying distribution groups" -ForegroundColor Yellow `r`n

$testgroups|%{set-distributiongroup -identity $_.distinguishedName -RequireSenderAuthenticationEnabled:$false} # Disable require authentication so we can send test messages to group without authenticating first (For testing only)

Write-Host `r`n

 

# Populate mailboxes with test messages

# Read in users and groups to get newly stamped email addresses

$testusers=Get-ADUser -SearchBase $TestOUUserPath -Filter * -Properties emailaddress|sort surname

$testgroups=Get-ADGroup -SearchBase $TestOUGroupsPath -Filter * -Properties mail |sort name

 

$j=1

foreach ($mbx in $testusers){

    # Send test messages to each user and group

    Write-Host "Processing mailbox: " -ForegroundColor Yellow -NoNewline;Write-Host $mbx.Name -ForegroundColor Red `r`n

    Write-Progress -Activity "Populating mailboxes" -Status "Sending test messages from mailbox: $mbx" -PercentComplete (($j/$testusers.count)*100)

    1..$numMsgs|%{

        for ($i=0;$i -le $testusers.count-1;$i++){

            Write-Host "Sending batch #: " -NoNewline;Write-Host $_ -ForegroundColor Yellow -NoNewline;Write-Host " mail message to: " -NoNewline;Write-Host $testusers[$i].emailaddress -ForegroundColor Green

            Send-MailMessage -To $testusers[$i].emailaddress -From $mbx.emailaddress -Subject "Test message$_" -Body "This is a test message. No reply necessary." -BodyAsHtml -SmtpServer exch2k13-1.contoso.com

        }

        for ($i=0;$i -le $testgroups.count-1;$i++){

            Write-Host "Sending batch #: " -NoNewline;Write-Host $_ -ForegroundColor Yellow -NoNewline;Write-Host " mail message to: " -NoNewline;Write-Host $testgroups[$i].mail -ForegroundColor Green

            Send-MailMessage -To $testgroups[$i].mail -From $mbx.emailaddress -Subject "Test message$_" -Body "This is a test message. No reply necessary." -BodyAsHtml -SmtpServer exch2k13-1.contoso.com

        }

    }

    Write-Host `r`n

    $j++

}

 

Write-Host "Done!" -ForegroundColor Yellow

 
Enjoy!
Until next time.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Categories
Follow Us