Windows Server TechCenter > Windows Server Forums > Windows PowerShell > Working with Move-Mailbox XML file
Ask a questionAsk a question
 

AnswerWorking with Move-Mailbox XML file

  • Thursday, November 05, 2009 4:27 AMVAs Hachi Roku Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hello,

    I'm still very new to PowerShell, and I'm working on a move mailbox script. I know after each user move it create an XML file. Inside that XML file there are two lines I'm interested in, the main one is the error code and the status of the move.

    <Result IsWarning="False" ErrorCode="0">This mailbox has been moved to the target database.</Result>

    Basically what I need to do is after each user move check the XML file for that user, get the status and write that to an SQL database. Now I've never worked with XML files in PowerShell and to be honest never really dealt with XML files in general.

    I searched around and saw that Import-CliXML gives me a schema error when trying to import, so I found this way

    $usermoved = [xml] (Get-Content mailboxmove.xml)

    At this point well there isnt much i can do with it, when I type $usermoved it just returns two properties and a bunch of methods. The two properties are
    xml which is blank
    move-mailbox which is move-mailbox

    Can anyone give me a quick run down on how to work with the XML file and just get that error status line set it to a variable, once that line is in a variable the easy part is writing that variable to the SQL table.

    Thanks

Answers

  • Thursday, November 05, 2009 6:45 AMMike Pfeiffer Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    If you just want the error code:

    $usermoved = [xml](Get-Content "C:\mailboxmove.xml")
    $errorcode = $usermoved."move-mailbox".taskdetails.item.result.errorcode

    Another thing you may want to consider is using a single xml report file for every mailbox move. Then you could get the error code and warning status for each mailbox using this code:

    $usermoved = [xml](Get-Content "C:\mailboxmove.xml")
    $usermoved."move-mailbox".taskdetails.item | select mailboxname,@{n="ErrorCode";e={$_.result.errorcode}},@{n="Warning";e={$_.result.IsWarning}}

    The output would look like this:

    MailboxName   ErrorCode  Warning
    -----------        ---------      -------
    Administrator   0               False
    TestUser0001   0               False
  • Thursday, November 05, 2009 2:55 PMMike Pfeiffer Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Just could use an if statement;

    $result = if ($errorcode -eq 0) {
        write "mailbox was moved successful!"
        }
        else {
        write "mailbox was NOT moved successful!"
    }

All Replies

  • Thursday, November 05, 2009 6:45 AMMike Pfeiffer Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    If you just want the error code:

    $usermoved = [xml](Get-Content "C:\mailboxmove.xml")
    $errorcode = $usermoved."move-mailbox".taskdetails.item.result.errorcode

    Another thing you may want to consider is using a single xml report file for every mailbox move. Then you could get the error code and warning status for each mailbox using this code:

    $usermoved = [xml](Get-Content "C:\mailboxmove.xml")
    $usermoved."move-mailbox".taskdetails.item | select mailboxname,@{n="ErrorCode";e={$_.result.errorcode}},@{n="Warning";e={$_.result.IsWarning}}

    The output would look like this:

    MailboxName   ErrorCode  Warning
    -----------        ---------      -------
    Administrator   0               False
    TestUser0001   0               False
  • Thursday, November 05, 2009 9:50 AMVAs Hachi Roku Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks for your reply ill play around with it on the plane. How would i get the "mailbox was moved successful!" text into a variable rather than the error code? Thanks again
  • Thursday, November 05, 2009 2:55 PMMike Pfeiffer Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Just could use an if statement;

    $result = if ($errorcode -eq 0) {
        write "mailbox was moved successful!"
        }
        else {
        write "mailbox was NOT moved successful!"
    }