Link to home
Start Free TrialLog in
Avatar of martineit
martineitFlag for France

asked on

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

Avatar of Maen Abu-Tabanjeh
Maen Abu-Tabanjeh
Flag of Jordan image

sorry last one by mistake
Avatar of martineit

ASKER

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...
just to try figure it out , you want to export mailbox to pst file? is that you want ? or there is something i missed?
Yes, this is the command: export-mailbox -identity "Mister x" -baditemlimit 1147483648 -pstfolderpath "d:\path\MisterX.pst"
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
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]

ASKER CERTIFIED SOLUTION
Avatar of martineit
martineit
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
not a real solution but a workaround