Update to the Create-LabUsers tool - now, with bigger foo

Update: I've added a shortlink for this tool: https://aka.ms/createlabusers.

In my quest to make the perfect tool for creating a realistic Lab Environment, I have added yet another feature, based on requests that I imagine people are making but have not yet told me about.  I'm going to go ahead and add 'psychic' to my resume.

For this feature, I'm adding a random binary file attachment.  While my previous update necessitated creating a LoremIpsum function for generating random message body content, it has come to my attention that nothing fills up a mailbox like random binary blobs.

So, I give you ... THE RANDOM BINARY BLOB GENERATOR.

I suck at naming things.

At any rate, here's the nifty function that I stuffed inside the Create-LabUsers tool:

 function CreateFileAttachments
{
    Param (
        [String]$TargetPath = $PWD.Path.ToString(),
        [int64]$MinFileSize = 1MB,
        [int64]$MaxFileSize = 10MB
    )
    
    $FileSize = Get-Random -Minimum $MinFileSize -Maximum $MaxFileSize
    $FileData = New-Object -TypeName byte[] $FileSize
    (New-Object Random).NextBytes($FileData)
    
    # Generate a file name using characters a-z and appending ".bin"
    $FileName = ((([char[]]([char]97 .. [char]122)) | sort { Get-Random })[0 .. 8] -join '') + ".bin"
    $global:FilePath = Join-Path $TargetPath "$($Filename)"
    
    # Write the file to disk  
    try
    {
        [IO.File]::WriteAllBytes($FilePath, $FileData)
    }
    catch
    {
        $ExceptionMessage = "Unable to write attachment file to $($FilePath), error $($_.Exception.Message)"
        Write-Log -Message $($ExceptionMessage) -ConsoleOutput -LogFile $Logfile -LogLevel ERROR
    }
}

Then, because realistic lab users demand additional randomness, I wrapped this in an If statement that checks for the result of a Get-Random with boolean input.  After each message has been sent, the temp file is cleaned up, so you don't end up with a lot of useless data.  At least, not any more useless than user accounts with made-up data.

             $HasAttachment = (Get-Random -InputObject @([bool]$true, [bool]$false))
            If ($HasAttachment -eq $True)
            {
                CreateFileAttachments
                Write-Log -LogFile $Logfile -LogLevel INFO -ConsoleOutput -Message "Sending message [$($UserCounter) / $($TotalMessagesToSend)] with subject $($Subject) and attachment $($FilePath) to $($Recipients.Count) recipients"
                Send-MailMessage -To $Recipients -From $($User.PrimarySmtpAddress) -Body $Body -SmtpServer $SmtpServer -Subject $Subject -ea silentlycontinue -wa silentlycontinue -Attachments $FilePath
                Remove-Item -Path $FilePath -Force -ea SilentlyContinue -WA SilentlyContinue
            }
            Else
            {
                Write-Log -LogFile $Logfile -LogLevel INFO -ConsoleOutput -Message "Sending message [$($UserCounter) / $($TotalMessagesToSend)] with subject $($Subject) to $($Recipients.Count) recipients"
                Send-MailMessage -To $Recipients -From $($User.PrimarySmtpAddress) -Body $Body -SmtpServer $SmtpServer -Subject $Subject -ea silentlycontinue -wa silentlycontinue
            }

The result?  Random email subjects and bodies, and random file attachments.  Just one more bit of awesomeness to add to your lab arsenal.

Once it has run, you can check the output in Get-MailboxStatistics:

I also removed Count as a mandatory parameter, and updated the rest of the pieces so that other functions can act independently of creating users.  This enables you to use the tool iteratively to perform separate functions (create users, create mailboxes, create and assign group memberships, assign mailbox permissions, inflate mailboxes).  I added a -DomainControllers parameter so you can also specify the domain controller you want to perform operations against--this should help alleviate issues confirming/locating objects when you're working in an environment with multiple domain controllers.

Also, a shout out to Jonathan Christie for feedback and some ideas around dealing with some of the issues that were discovered when running the tool in multi-domain environments.

You can update your version (or download a brand-spanking new one) at https://gallery.technet.microsoft.com/Create-Realistic-Lab-Users-b756fedf.