Tuesday, January 31, 2017

Delete all Exchange mailbox content using PowerShell

This can be a useful script for anyone who has the need to delete the entire contents of an Exchange mailbox. We have some test accounts that can get overridden with emails if they're not cleaned out. This script prints out a summary of how many items are in the mailbox and then deletes them all. It was tested against Exchange server 2013.

$id = "user@acme.com"

#See how many items are in mailbox
Get-MailboxFolderStatistics $id | Select Identity, ItemsInFolder

#Delete items in mailbox
Search-Mailbox -Identity $id -DeleteContent -force

Solving Exchange server 2013 out of disk space

I ran into a problem a little while ago where our on-premise Exchange 2013 environment was acting weird. An EWS (Exchange Web Services) application we have was behaving oddly such as throwing ServiceResponseExceptions with messages like "the folder in which items were to be saved could not be found". After some digging, I discovered that there was virtually no available disk space on the Exchange server. This environment had been a vanilla set-up to perform some tests with EWS and there had been load going on and off for months.

After some digging the following discussion on SpiceWorks helped solve the problem (discussion here). The issue was that by default Exchange does not purge any logs, and since we did not have any routine back-up or clean-up occurring, they were growing un-checked. 
"Exchange Server database transaction logs record all changes to an Exchange Server database. Over time, these log files accumulate and use all the available disk space if they are not periodically removed from the hard disk." (ref)
By using Scanner, I confirmed that the fat cat on the server was the Exchange logging. For our environment, we didn't care about backing up changes to the database. For this case, enabling circular logging was the solution because it enabled the Exchange server to automatically get rid of unnecessary log files and by so freeing up disk space. I used the directions here to enable circular logging, which involved dismounting and remounting the database.

Once that was done, I used this suggestion:
"To speed up new log file creation and the automatic deletion process, you can send yourself an e-mail message with a 5-megabyte (MB) attachment" (ref)
Oddly enough, it worked and shortly thereafter the Exchange server's available disk space increased and the EWS application behaved normally again.

References

https://community.spiceworks.com/topic/576313-low-disk-space-for-exchange-database 

https://technet.microsoft.com/en-us/library/dn756374(v=exchg.150).aspx 

https://support.microsoft.com/en-us/help/240145/how-to-remove-exchange-server-transaction-log-files

Create Lync-enabled Active Directory users in bulk with PowerShell!

This blog post explores how to create Lync-enabled users in Active Directory in bulk using PowerShell. This is a general outline for creating a bunch of generic accounts in Active Directory and then configuring them to be able to sign on to Skype for Business. This is tested against Skype for Business server 2015. The use case here is how to create a bunch of accounts for load testing.

The accounts will look as follows:

Windows logon (username): TestUser01
First name: Test
Last name: User 01
Display name: Test User 01
Email: TestUser01@acme.com
Password: default_password01!

This pattern will be used to create 1000 test user accounts.

Step 1: Create users in Active Directory

This script uses the ActiveDirectory PowerShell module. The variables startIndex and endIndex can be modified to create different ranges of users, for example if you need 25 more users at some point, you could change the startIndex to 1001 and the endIndex to 1025.

Import-Module ActiveDirectory
$startIndex = 1;
$endIndex = 1000;
$totalusers = $endIndex - $startIndex + 1

Write-Host "Starting"

for ($i=$startIndex; $i -le $endIndex ; $i++) 
 { 
 $userID = "{0:00}" -f ($i)
 $userName = "TestUser$userID"
 $givenName = "Test" 
 $surname = "User $userID" 
 $displayName = "$givenName $surname"

Write-Host "Creating AD user" ($i) "of" $totalusers ":" $userName

New-ADUser `
 -Name $userName  `
 -Path  "OU=MyFooBar,DC=Acme,DC=com" `
 -EmailAddress ("$userName@acme.com")`
 -SamAccountName $userName `
  -GivenName $givenName `
  -Surname $surname `
  -DisplayName $displayName `
 -AccountPassword (ConvertTo-SecureString "default_password01!" -AsPlainText -Force) `
 -Enabled $true
}

Write-Host "Done"

Step 2: Enable users in Skype for Business 2015

This script uses the pattern we know that our test users are created from to identify users by their display name and enable them in Lync.

$lyncServer = "myLync1.acme.com"
$domain = "acme.com";
$startIndex = 1;
$endIndex = 1000;
$totalusers = $endIndex - $startIndex + 1

Write-Host "Starting"

for ($i=$startIndex; $i -le $endIndex ; $i++) 
 { 
 $userID = "{0:00}" -f ($i)
 $userName = "TestUser$userID"
 $givenName = "Test" 
 $surname = "User $userID" 
 $displayName = "$givenName $surname"

Write-Host "Enabling AD user for Lync " ($i) "of" $totalusers ":" $userName

Enable-CsUser -Identity $displayName -RegistrarPool $lyncServer -SipAddressType SamAccountName -SipDomain $domain
}

Write-Host "Done"