Programmatically export mailbox via Powershell - error -2147219963

Dear Experts,
we are trying to automate the user account management using Powershell to handle all interactions with Exchange 2007.
The powershell commands are launched from a webservice developed in ASP.NET (C#).

The project is practically over aside from the export of the mailbox.

When I try to execute the following command:
export-mailbox -identity "Mister x" -baditemlimit 1147483648 -pstfolderpath "d:\path\MisterX.pst"
We get an error:
Export-Mailbox error results: Error was found for MisterX (MisterX@MisteryComp.net) because: An unknown error has occurred., error code: -2147219963

The same command runs fine from the exchange powershell console (same user, same server)
We also noticed the command completes successfully if I first run the exact same command directly on powershell (from the same server) and then leave the console window open.
If I close the console, the application starts failing again with the same error.
If I run the application pool that owns the webservice with Domain Admin account, everything works fine but the user I would like to use has all the necessary rights.

I get the feeling Powershell in order to function properly needs a session open in a specific way that I do not understand.

Here following the relevant portion of code:
        RunspaceConfiguration runspaceConf = RunspaceConfiguration.Create();
        PSSnapInException PSException = null;
        PSSnapInInfo info = runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out PSException);
        Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConf);
        runspace.Open();

        System.Management.Automation.SwitchParameter spConfirmYes;
        int iNbmaxerreurs = 1147483648;
        spConfirmYes = false; //Always False

        Pipeline pipelineEM = runspace.CreatePipeline();

        pipelineEM.Commands.Add("Export-Mailbox");
        pipelineEM.Commands[0].Parameters.Add("Identity", MailID.ToString());
        pipelineEM.Commands[0].Parameters.Add("BadItemLimit", iNbmaxerreurs);
        pipelineEM.Commands[0].Parameters.Add("Confirm", spConfirmYes);
        pipelineEM.Commands[0].Parameters.Add("PSTFolderPath", sExportfilePath.ToString() + ExportfileName.ToString() + ".pst");

        // Result Recuperation:
        System.Collections.ObjectModel.Collection<PSObject> psobjResults;
        System.Collections.ObjectModel.Collection<Object> objResultserror;

        psobjResults = pipelineEM.Invoke();

        objResultserror = pipelineEM.Error.ReadToEnd();

Open in new window

LVL 1
martineitAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Maen Abu-TabanjehNetwork Administrator, Network ConsultantCommented:
0
Maen Abu-TabanjehNetwork Administrator, Network ConsultantCommented:
0
Maen Abu-TabanjehNetwork Administrator, Network ConsultantCommented:
sorry last one by mistake
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

martineitAuthor Commented:
yeah but even the first has nothing to do with my problem:
that guy has an Exchange 2003, he's using a builtin feature to check mailbox size, while I have to do an export which is slightly more complicated than that...
0
Maen Abu-TabanjehNetwork Administrator, Network ConsultantCommented:
just to try figure it out , you want to export mailbox to pst file? is that you want ? or there is something i missed?
0
martineitAuthor Commented:
Yes, this is the command: export-mailbox -identity "Mister x" -baditemlimit 1147483648 -pstfolderpath "d:\path\MisterX.pst"
0
Maen Abu-TabanjehNetwork Administrator, Network ConsultantCommented:
as i read this because of permissions of mailbox you need to set permissions to mailbox to allow it to export as pst :


    Add-MailboxPermission -Identity $strMailbox -User $ServiceAccount -AccessRights 'FullAccess' -Confirm:$True

Open in new window


regarding to above powershell code you need to modify your code by adding something like this :
note that this code need to be modified depending on C# grammar :
AccessRightType = "FullAccess"
ServiceAccount = "Administrator@ethan.local"
 pipelineEM.Commands.Add("Add-MailboxPermission");
        pipelineEM.Commands[0].Parameters.Add("Identity", MailID.ToString());
        pipelineEM.Commands[0].Parameters.Add("User", ServiceAccount);
pipelineEM.Commands[0].Parameters.Add("AccessRights ", AccessRightType);
        pipelineEM.Commands[0].Parameters.Add("Confirm", spConfirmYes);

Open in new window

 
the code should be add on line 12 .. after this will see that your code will work fine
0
Maen Abu-TabanjehNetwork Administrator, Network ConsultantCommented:
0
martineitAuthor Commented:
Hello,
So:
- The user running the application pool (and therefore the user launching powershell) already has direct full access on the mailbox.
- the link you sent me is to have an automatic powershell script that archives older than x mailboxes which is not at all what I need. In fact that script uses the exact same script I use but in that case the export-mailbox is only one of the many commands launched.

[b]
My problem comes (probably) from the interaction between powershell and a .NET developed webservice
[/b]

0
Maen Abu-TabanjehNetwork Administrator, Network ConsultantCommented:
0
martineitAuthor Commented:
Sorry,
none of the answers above was good enough.
I ended up using a domain admin to launch the powershell script (very unsafe, as soon as I get some time I will try to find a better solution).

Thanks anyway,
Roberto.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
martineitAuthor Commented:
not a real solution but a workaround
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.