Laeeq Qazi's Exchange Server Blog

Exchange Server blog

How to recreate Primary Hierarchy Public Folder Mailbox in Exchange 2013

Hi,

Sometimes, in our lab, developers delete Primary Public Folder Hierarchy Mailbox by mistake, and after it, we cannot perform any public folder operation via PF cmdlets.

E.g. a get-publicFolder cmd thows this error:

[PS] C:\Windows\system32>Get-PublicFolder \ -GetChildren
Couldn't find the public folder mailbox.
+ CategoryInfo : NotSpecified: (:) [Get-PublicFolder], ObjectNotFoundException 

Confirmation:

This is how I first confirm that ‘Primary Hierarchy Public Folder Mailbox’ has been deleted:

First I get the Primary Hierarchy PF mailbox using this command

Get-OrganizationConfig | FL RootPublicFolderMailbox

RootPublicFolderMailbox represents GUID of PF mailbox.

When I retrieved all PF Mailboxes, Guid of RootPublicFolderMailbox was not there:

Get-Mailbox –PublicFolder | FL Name,ExchangeGuid

which confirms that ‘Primary Hierarchy Public Folder Mailbox’ is no more in Exchange Server 2013, and someone has deleted it.

Recreation of Public Folder Hierarchy Mailbox:

The only way to recreate ‘Primary Hierarchy PF Mailbox’ I discovered via this helpful blog http://careexchange.in/how-to-recreate-public-folder-master-hierarchy-in-exchange-2013.
which is to remove all Public folders, all Secondary Public Folder Mailboxes, but I was not able to remove Public Folders and getting this error.

[PS] C:\Windows\system32> Get-PublicFolder -Recurse | Remove-PublicFolder
Couldn't find the public folder mailbox.
+ CategoryInfo : NotSpecified: (:) [Get-PublicFolder], ObjectNotFoundException

So in the end, what I did was ‘Remove AD users associated with all Secondary PF Mailboxes using ‘Active Directory Users and Computers’ console.

For this, first I got the location of all secondary PF Mailboxes like this:

Get-Mailbox –PublicFolder | FL DistinguishedName

And then Removed the associated AD user using ADUC console, which subsequently disconnected all PF mailboxes.

Then I opened ADSIEdit.msc , and went to “Cn=Configuration” container, and browsed ‘Exchange Organization’ object’s properties, whose AD Path looks like this:

CN=OrganizationName,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=AdDomain,DC=lab

From properties of this object, I just cleared the value from the attribute ‘msExchDefaultPublicFolderMailbox’, which you can see is just a garbage.

Then I just re-created the Primary Hierarchy PF mailbox using this command:

New-Mailbox -PublicFolder -Name 'Primary Hierarchy PF Mailbox'

Then I recreated my secondary PF Mailboxes, and then created Public Folders.

Note: Please test this in a lab, before applying to production scenarios. Also, This doesnt recover existing Public folders.

December 26, 2013 Posted by | Exchange 2013 | , | 8 Comments

How to log ‘Exchange Management Shell’ session in Exchange 2010\2013

Hi,

Exchange admins often want to log each and every command and their result to a text file, to be used\reviewed in future.

This is very easy in Exchange 2010\2013.

Locate ‘RemoteExchange.ps1″ file in Exchange Server bin folder, which has this path with default C: drive installation:

C:\Program Files\Microsoft\Exchange Server\V15\Bin

First take a backup of this file and then Open ‘RemoteExchange.ps1″ and put this commands:

$status = start-transcript;
Write-Host -ForegroundColor Green "`n$status;"
Write-Host -ForegroundColor Green "All of your commands would be logged to above mentioned text file`n";

as the last lines in the script, after “get-tip ” command.

Save this file, and restart “Exchange Management Shell”.

Hope this helps you.

Kind Regards,

December 9, 2013 Posted by | Exchange 2010, Exchange 2013 | | Leave a comment

Cannot invoke pipeline because runspace is not in the Opened state

Hi

I developed a web app to manage exchange server 2010 objects (domains, mailboxes etc) using Remote PoewerShell API. When deployed this web app on an other fresh server with Exchange Server 2010 installed, I got this error:

Cannot invoke pipeline because runspace is not in the Opened state. Current state of runspace is ‘Broken’.

The user, which I was using for Remote PowerShell credentials, was a domain admin, with all  sorts of rights, so I looked into basic RemotePowershell configuration, and found the solution.

I followed these 2 steps to resove this error:

1. Enabled Powershell remoting on server. Open Windows PowerShell console and run

    Enable-PSRemoting

It would prompt you for input, and then just press ‘Enter’, 2 times.

2. Go to IIS manager, and under default website, locate Exchange ‘PowerShell’ virtual directory. On right side pane,  double click “Authentication” and enable “Basic” authentication.

Dont forget to reset IIS by Start->Run-> IisReset.

These 2 steps resolved my error. I would also suggest to look for 3rd step, which is to check whether the user, which you are using for Remote PowerShell credentials, has the “RemotePowerShellEnabled” flag enabled or not, which you can check via Exchange Management Shell on Exchange server by running:

get-user domain\User|fl RemotePowerShellEnabled

If it returns false, then enable it by using

set-user domain\User-RemotePowerShellEnabled:$True

Hope this help someone else too.

Kind Regards,

Laeeq Qazi

January 3, 2013 Posted by | Exchange 2010, Exchange 2013 | Leave a comment

Create Mailbox on Exchange Server 2013 using C#

Hi,

Creating a mailbox on Exchange 2013 takes same source code as I used on Exchange 2010.


RunspaceConfiguration runspaceConf = RunspaceConfiguration.Create();

PSSnapInException PSException = null;

PSSnapInInfo info = runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out PSException);

Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConf);

runspace.Open();

Pipeline pipeline = runspace.CreatePipeline();

//StringToSecureString is a function defined at the end

SecureString securePwd = StringToSecureString("Passw0rd1");
Command newMailBox = new Command("New-Mailbox");

newMailBox.Parameters.Add("Name", "TestName1");
newMailBox.Parameters.Add("Alias", "TestName1");
newMailBox.Parameters.Add("database", "Mailbox Database 1406738839");
newMailBox.Parameters.Add("Password", securePwd);
newMailBox.Parameters.Add("DisplayName", "TestName1");
newMailBox.Parameters.Add("UserPrincipalName", "TestName1@ad.lab");
newMailBox.Parameters.Add("OrganizationalUnit", "ou=myorg,dc=ad,dc=lab");
newMailBox.Parameters.Add("FirstName", "TestName1");

pipeline.Commands.Add(newMailBox);

Collection<PSObject> result = pipeline.Invoke();

Here is the helper function

public static SecureString StringToSecureString(string str)
{
   SecureString ss = new SecureString();
   char[] passwordChars = str.ToCharArray();

   foreach (char c in passwordChars)
   {
      ss.AppendChar(c);
   }
   return ss;
}

Note: I used VS 2010 for it. If you use any of the Exchange DLLs, like Microsoft.Exchange.Management.dll etc, then you would have to use VS 2010, as these Exchange DLLs have been developed using .Net 4.0, and you cannot add a reference of .net 4 DLL in VS 2008 or lower version of VS.

Very Important: After a healthy discussion on Exchange 2013 Forum on support of Local PowerShell sessions on Exchange 2010\2013, I don’t recommend to use above mentioned approach to create mailboxes on Exchange Server 2013, instead use Remote PowerShell sessions. I would put a post on it in coming days, where I would show how to create a mailbox on Exchange 2010\2013 using Remote PowerShell. Please visit the link mentioned in the first comment below for more details. I would also mention some problems while adopting Remote PowerShell approach like Object Deserialization issue.

Regards,

Laeeq Qazi

October 2, 2012 Posted by | Exchange 2013 | , , , | 9 Comments

No snap-ins have been registered for Windows PowerShell version 3

Hi,

I was developing an app to manage exchange mailboxes and having the error when running the app in VS 2010.

No snap-ins have been registered for Windows PowerShell version 3

It turned out that I was targeting x86 platform in project properties, and I think due to which app was trying to pick 32 bit powershell. I changed the target cpu to “Any CPU” and then error disappeared.

Also please look at http://blogs.msdn.com/b/pareshj/archive/2010/07/30/error-msg-no-snap-ins-have-been-registered-for-windows-powershell-version-2.aspx

Hope it helps someone else too.

Note: To develop apps for Exchange 2013 preview using Exchange PowerShell sdk, you would have to use VS 2010, becuase Exchange server 2013 have been developed using .net 4.0, and you cannot add .net 4.0 DLL references in VS 2008 or lower versions.

Regards,

Laeeq Qazi

October 2, 2012 Posted by | Exchange 2010, Exchange 2013 | , | 4 Comments

Send Email via PowerShell cmdlet Send-MailMessage

## Start of Scirpt ##

##This script reads Recipinets and Email Contents from 2 different text files
##And then sends email to all of the recipients

$cred =Get-Credential;
$smtpServer = “smtpServer.domain.com”

$Mailboxes = Get-Content c:\mailboxes.txt
$Email = Get-Content c:\Email.txt

$Contents = “”;
$Sender = “Admin@domain.com”;

foreach($row in $Email){$Contents+= $row};

foreach($mailbox in $Mailboxes)
{
$mailbox = $mailbox.Trim();
$mailbox;
$sub = “Hi $mailbox “;
$From = $Sender;

$To= $mailbox;
$body= $Contents;

if($To -ne $null -and $To -ne ”)
{
send-mailmessage -from $From -to $To -subject $sub -credential $cred -body $body -smtpServer $smtpServer
}
}

## End of Scirpt ##

This script uses powerShell 2.0 command send-mailmessage to send the mail message to various recipients, who are placed in a text file , each recipient on a single line like this:

user@domain1.com

user@domain2.com

user@domain3.com etc.

Script reads the Email contents from text file too, which makes this script dynamic. I used this script while working a email extracting application for load testing.  I had to work on very heavy mailboxes, with 10,000+ emails, and I used this script with one other loop, to send 2000 emails via single command.

I hope it help you.

Regards,

Laeeq Qazi

December 7, 2011 Posted by | Uncategorized | 2 Comments

2010 in review

The stats helper monkeys at WordPress.com mulled over how this blog did in 2010, and here’s a high level summary of its overall blog health:

Healthy blog!

The Blog-Health-o-Meter™ reads This blog is doing awesome!.

Crunchy numbers

Featured image

A helper monkey made this abstract painting, inspired by your stats.

A Boeing 747-400 passenger jet can hold 416 passengers. This blog was viewed about 3,600 times in 2010. That’s about 9 full 747s.

 

In 2010, there were 6 new posts, growing the total archive of this blog to 7 posts.

The busiest day of the year was November 4th with 41 views. The most popular post that day was Manually Generating Offline Address Book on Exchange 2007/2010.

 

Where did they come from?

The top referring sites in 2010 were social.technet.microsoft.com, google.com, networksteve.com, google.co.in, and edugeek.net.

Some visitors came searching, mostly for msexchquerybasedn exchange 2010, exchange 2010 change gal for users, laeeq qazi, powershell display msexchquerybasedn, and moveallreplicas.ps1 exchange 2010.

Attractions in 2010

These are the posts and pages that got the most views in 2010.

1

Manually Generating Offline Address Book on Exchange 2007/2010 February 2010
2 comments

2

How to set GAL for mailboxes in OWA for Exchange 2007/2010 in bulk April 2010
4 comments and 1 Like on WordPress.com,

3

How to uninstall Exchange Server 2007 May 2010

4

About Laeeq Qazi November 2008
2 comments

5

How to remove client permissions from a public folder and all its children? June 2010

January 2, 2011 Posted by | Uncategorized | Leave a comment

How to remove client permissions from a public folder and all its children?

Hi,

In Exchange 2007/2010 you can remove client permissions from a public folder and all its child public folders for all users in bulk using following script.

Suppose you have a root level public folder ‘\Sales’ on public folder server “Mailbox01″, which in turn has 2 child public folders ‘\Sales\Uk’ and ”\Sales\US’, then this script will remove all client permissions from these 3 public folders for all users.

########### Script Start #####################
$AllPublicFolders = Get-publicFolder ‘\Sales’ -recurse -server Mailbox01

foreach($Pf in $AllPublicFolders ){

Get-PublicFolderClientPermission $Pf | Foreach{ Remove-PublicFolderClientPermission $_.Identity -User $_.User -AccessRights $_.AccessRights }

}
########### Script End #####################

Note: Put above lines of code in a text file and save it as .ps1 (powershell) file and then execute it in Exchange Management Shell (EMS) or Windows Powershell console.

If you save this file in Exchange Scripts folder
“C:\Program Files\Microsoft\Exchange Server\Scripts” [Exchange 2007]
OR
“C:\Program Files\Microsoft\Exchange Server\V14\Scripts” [Exchange 2010]

then, in EMS, you will just need to enter the .ps1 file name and then press enter.

You might also need to set the powershell execution policy as RemoteSigned in EMS:

Set-ExecutionPolicy RemoteSigned

Also, if you decide to run this script in Windows Powershell Console then don’t forget to add powershell exchange snap-in using one of the following commands:

#For Exchange 2007:
Add-PSSnapIn “Microsoft.Exchange.Management.PowerShell.Admin”
#For Exchange 2010:
Add-PSSnapIn “Microsoft.Exchange.Management.PowerShell.E2010”

Note: For exchange 2013\Office 365, first line of script should be:

$AllPublicFolders = Get-publicFolder ‘\Sales’ -recurse

 

Regards,
Laeeq Qazi

June 27, 2010 Posted by | Exchange 2007, Exchange 2010, Exchange 2013, Office 365 | 6 Comments

How to remove all group memberships for one exchange mailbox user

There is often a need to remove a mailbox user membership from all exchange distribution groups.

Here is a powershell script for this purpose.

e.g your user’s primary smtp address is user@domain.com, then this script will remove the membership of this user from each mail enabled exchange distribution groups:

########### Script Start #####################

$DGs= Get-DistributionGroup | where { (Get-DistributionGroupMember $_ | foreach {$_.PrimarySmtpAddress}) -contains “user@domain.com” }

foreach( $dg in $DGs){

Remove-DistributionGroupMember $dg -Member user@domain.com
}

########### Script End #####################

Note: Put above lines of code in a text file and save it as .ps1 file and then execute it in EMS or powershell console.

If you save this file in Exchange Scripts folder “C:\Program Files\Microsoft\Exchange Server\Scripts” OR “C:\Program Files\Microsoft\Exchange Server\V14\Scripts” then, in EMS, you will just need to enter the .ps1 file name and then press enter.
You might also need to set the powershell execution policy as RemoteSigned in EMS:

set-executionPolicy RemoteSigned

Regards,
Laeeq Qazi

June 18, 2010 Posted by | Exchange 2007, Exchange 2010 | | Leave a comment

How to uninstall Exchange Server 2007

I have often seen people searching for the uninstall steps for Exchange 2007, specially for Mailbox Server. Uninstall of Mailbox Server is not difficult at all if steps are performed properly.

Suppose i have following 2 Servers

Exchange01 (HUB+CAS+Mailbox Roles)
Exchange02 (HUB+CAS+Mailbox Roles)

and i want to uninstall any one of them, e.g Exchange02.

So here are the steps to uninstall Exchange02 server (all 3 roles). I run Powershell Commands in EMS for most of the tasks:

First of all uninstall Mailbox Role from Exchange02 Server.

Login to Exchange02 Server using Administrator Account:

Open Exchange Management Shell

1. Move/remove all Offline Address Books for which Exchange02 is OAB Generation Server.

Move of OABs would be good choice and this can easily be done by opening EMC and then browsing to “Organization cofiguration->Offline Address Book” and then selecting any OAB and then clicking Move button on right side in the Action Pane. Perform this step for all OABs on Exchange02 server and move to Exchange01.

You can also remove the OABs on this server using the ps command:

1.1get-OfflineAddressBook -Server Exchange02 | remove-OfflineAddressBook

2. Go to Mailbox server and Remove all Public Folders+System Public Folders

2.1. Get-PublicFolder “\” -Recurse -ResultSize:Unlimited | Remove-PublicFolder -Recurse -ErrorAction:SilentlyContinue
2.2 Get-PublicFolder “\Non_Ipm_Subtree” -Recurse -ResultSize:Unlimited | Remove-PublicFolder -Recurse -ErrorAction:SilentlyContinue

3. Remove PublicFolder Database

3.1 Get-PublicFolderDatabase -server Exchange02| Remove-PublicFolderDatabase

Sometimes if we have more than one exchange Mailbox Servers we get public folder replica error while trying to remove a PublicFolder Database and can not remove the PF DB.

“The public folder database “PublicFolder Database” contains folder replicas. Before deleting the public folder database, remove the folders or move the replicas to another public folder database..”

Solution: Move all replicas to some other server.

There is nice powershell script “MoveAllReplicas.ps1” for this purpose, provided by Microsoft, and placed at
drive:\\Program Files\Microsoft\Exchange Server\Scripts.

Its usage is:
MoveAllReplicas.ps1 –Server currentServer –NewServer NewServerToHoldReplica

So I used:
MoveAllReplicas.ps1 –Server Exchangge02 –NewServer Exchangge01

After running this script I waited for 15 mins and then removed the Public Folders again using both above 2.1 and 2.2 commands for “\” and “\Non_Ipm_Subtree” and then again executed 3.1 command to remove the PF database.

4. Remove all mailboxes (This step will not delete default mailbox for Administrator)

4.1 Get-mailbox -server Exchange02 | remove-mailbox -confirm:$false

5. Now Disable Default Administrator mailbox using the command

5.1 Disable-mailbox Administrator if it exists on Exchange02 server.

6. Now delete all disabled mailboxes on this MBX server

6.1 get-MailboxStatistics -server Exchange02 | Where{$_.DisconnectDate -ne $null}| foreach {remove-mailbox -database $_.database -storemailboxidentity $_.mailboxguid }

7. Now open ‘Add/Remove Programs’ and select Exchange 2007 and then press ‘Remove’ button on the right side.
Exchange 2007 Uninstall window will appear and will ask what server roles you want to uninstall.

Now deselect Mailbox Serve role only.
Uninstall will start now and hopefully without any error.

8. Now uninstall HUB, CAS roles and Exchange management Tools using same method as u unistalled MBX server from ‘Add/Remove Programs’.

I hope that this post help you in uninstalling Exchange Server 2007 and specially Exchange 2007 Mailbox Role.
Please feel free to add comments to this post if you find any thing wrong, or want to convey improvement.

Regards,
Laeeq Qazi

May 28, 2010 Posted by | Exchange 2007 | | 3 Comments