Published: 2013-02-25
Updated: –
Version: 1.0

This script was developed because there was a need to configure every Shared Mailbox with a office value. In Exchange the value is called “Office” while in Active Directory this value is called “physicalDeliveryOfficeName”.

The script is retrieving all shared mailboxes where the office value isn’t populated and creates an array of these mailboxes. It includes a couple of attributes.

It does give you the opportunity to verify the changes that will be done, before they are done.
The array is showed to the screen and also saved as a CSV file called “pre-report-$timestamp.csv”

From each of the shared mailboxes, the script will bring the office value from the corresponding manager and put that value into respective shared mailbox office attribute value.

If there are no manager configured for any of the shared mailboxes, a log file will be created called “no-manager-configured-report.txt” and if there are no office attribute value configured on the managers, a log file will be created called “no-office-configured-report.txt”

When the script is completed, a log file will be created called “completed-report-$timestamp.txt” with the changes that have been done.

####################################################################
# SetOffice.ps1
# Description:
# This PowerShell script will stamp the office field for every shared mailbox
# that doesn't have a value set. The value will be collected from each manager
# If no Manager is set, the script will not stamp the office value
#
# Jonas Andersson, MCC 2011 and 2012
# http://www.testlabs.se/blog
# Twitter @jonand82
####################################################################</pre>
Write-Host "Starting script.." -ForegroundColor White -BackgroundColor Black

$ou = "target.local/Resource Mailboxes"

Write-Host 'Retrieving all Shared Mailboxes without "Office" value..' -ForegroundColor White -BackgroundColor Black

$UserList = Get-User -ResultSize unlimited -OrganizationalUnit $ou -RecipientTypeDetails SharedMailbox -Filter {Office -eq $null}

$MasterList = @()
 foreach ($User in $UserList)
 {
 $MyObject = New-Object PSObject -Property @{

Mailbox = (Get-User -Identity $User).DistinguishedName
 Manager = ((Get-User -Identity $User).Manager).DistinguishedName
 Manage = (Get-User -Identity $User).Manager | ForEach-Object{
 $o = (Get-User $_).Office
 }

Office = $o
 }

$MasterList += $MyObject
 }

if ((Read-Host 'Show and save list, the listed shared mailboxes? (Y/N)') -eq 'Y' )
 {
 $MasterList | Select Mailbox,Manager,Office

$timestamp = Get-Date -UFormat %Y%m%d-%H%M
 $reportfile = ".\pre-report-$timestamp.csv"
 $MasterList | Select Mailbox,Manager,Office | Export-CSV $reportfile -Encoding Unicode -Notype

Write-Host 'The file is saved as '$reportfile''
 }

if ((Read-Host 'Start the configuration? (Y/N)') -eq 'Y')
 {
 Write-Host 'Starting the script..' -ForegroundColor White -BackgroundColor Black
 Foreach ($item in $MasterList)
 {

if (($item.Manager -eq $null) -or ($item.Manager -eq ""))
 {
 Write-Host 'Item '$item.Mailbox' doesnt have a Manager configured!' -ForegroundColor Red -BackgroundColor Black
 $reportfile = "no-manager-configured-report.txt"
 $empty = $item.Mailbox,$item.Manager,$item.Office
 $empty | Out-File -FilePath $reportfile -Append

}

if (($item.Office -eq $null) -or ($item.Office -eq ""))
 {
 Write-Host 'Item '$item.Manager' doesnt have a Office value configured!' -ForegroundColor Red -BackgroundColor Black
 $reportfile = "no-office-configured-report.txt"
 $empty = $item.Mailbox,$item.Manager,$item.Office
 $empty | Out-File -FilePath $reportfile -Append

}

else
 {
 Write-Host 'Configuring the value for item'$item.Mailbox'to'$item.Office'' -ForegroundColor White -BackgroundColor Black -
 Set-Mailbox -Identity $item.Mailbox -Office $item.Office

$timestamp = Get-Date -UFormat %Y%m%d-%H%M
 $reportfile = "completed-report-$timestamp.txt"
 $list = $item.Mailbox,$item.Manager,$item.Office
 $list | Out-File -FilePath $reportfile -Append

}
 }

}

else{
 exit
 }
 

You can use this script for free, just make sure to test it in a lab environment before, I take no responsibility for any actions and I can’t give any support for it.
Feel free to use the script, edit it, publish it. If you do, please make sure to link back to the blog post url.

Download script

Regards,
Jonas