none
Need Powershell assistance to loop and goto RRS feed

  • Question

  • We are in need of a script that will require PowerShell for Exchange 2013 mailboxes. Back in the DOS days this was simple with IF, ELSE, GOTO commands, but I'm having trouble grasping onto how this should work in PowerShell. I have no doubts this is simple for you PowerShell scripting guys, but that is not the case for me.

    Once a mailbox starts exporting the script will ask if you want to continue based on the status of the export. If you want to check the progress and the status shows Completed, I would then need to jump to the remaining part of the script. If the status shows not completed then I want to loop back so it will show the status again.

    Thanks in advance for any efforts you put into this.

    ~Rick

    Thursday, June 5, 2014 3:50 PM

Answers

  • Hi,

    one thing, you choose the wrong forum, but i have a solution for you.

    had this Script for the sam task, you need to change a little bit, but should work for you.

    #Import Module
    #Import-Module ActiveDirectory
    Add-PSSnapin  Microsoft.Exchange.Management.PowerShell.E2010
    
    #Variables
    $sAMAccountName=“UserName”
    $HomeDirectoryOrig=“c:/”
    $Destination=“d”:/
    
    
    
    #Settings
    $SysInfo="Info"
    $Info=$null
    $HomeDirectory=$HomeDirectoryOrig+"\"+$sAMAccountName+".pst"
    $HomeDirectoryArchiv=$HomeDirectoryOrig+"\"+$sAMAccountName+"_archive.pst"
    
    
    #Magic
    New-MailboxExportRequest -Mailbox $sAMAccountName -FilePath "$HomeDirectory" -ExcludeDumpster
    $Request=Get-MailboxExportRequest -Mailbox $sAMAccountName
    
    while ($Request.Status -eq "Queued") {
    $Request=Get-MailboxExportRequest -Mailbox $sAMAccountName
    sleep -Seconds 5
    #Write-Host "Wait”
    }
    
    while ($Request.Status -eq "InProgress") {
    $Request=Get-MailboxExportRequest -Mailbox $sAMAccountName
    sleep -Seconds 5
    #Write-Host “running”
    }
    
    if ($Request.Status -eq "Completed") {
        Move-Item -Path $HomeDirectory -Destination $Destination -Force
        Get-MailboxExportRequest -Mailbox $sAMAccountName | Remove-MailboxExportRequest -Confirm:$false
        } else {
        $SysInfo="Error"
        $Info="Error on PST"
        }
    
    
    #Export Archive
    if ((Get-Mailbox -Identity $sAMAccountName).ArchiveDatabase -ne $null ) {
        New-MailboxExportRequest -Mailbox $sAMAccountName -FilePath "$HomeDirectoryArchiv" -IsArchive
        $Request=Get-MailboxExportRequest -Mailbox $sAMAccountName
    
        while ($Request.Status -eq "Queued") {
        $Request=Get-MailboxExportRequest -Mailbox $sAMAccountName
        sleep -Seconds 5
        #Write-Host "Warten"
        }
    
        while ($Request.Status -eq "InProgress") {
        $Request=Get-MailboxExportRequest -Mailbox $sAMAccountName
        sleep -Seconds 5
        #Write-Host "Läuft"
        }
    
        if ($Request.Status -eq "Completed") {
            Move-Item -Path $HomeDirectory -Destination $Destination -Force
            Get-MailboxExportRequest -Mailbox $sAMAccountName | Remove-MailboxExportRequest -Confirm:$false
            } else {
            $SysInfo="Error"
            $Info="Error on PST"
            }
    }
    


    Seidl Michael | http://www.techguy.at | twitter.com/techguyat | facebook.com/techguyat

    Thursday, June 5, 2014 4:08 PM

All replies

  • Hi,

    one thing, you choose the wrong forum, but i have a solution for you.

    had this Script for the sam task, you need to change a little bit, but should work for you.

    #Import Module
    #Import-Module ActiveDirectory
    Add-PSSnapin  Microsoft.Exchange.Management.PowerShell.E2010
    
    #Variables
    $sAMAccountName=“UserName”
    $HomeDirectoryOrig=“c:/”
    $Destination=“d”:/
    
    
    
    #Settings
    $SysInfo="Info"
    $Info=$null
    $HomeDirectory=$HomeDirectoryOrig+"\"+$sAMAccountName+".pst"
    $HomeDirectoryArchiv=$HomeDirectoryOrig+"\"+$sAMAccountName+"_archive.pst"
    
    
    #Magic
    New-MailboxExportRequest -Mailbox $sAMAccountName -FilePath "$HomeDirectory" -ExcludeDumpster
    $Request=Get-MailboxExportRequest -Mailbox $sAMAccountName
    
    while ($Request.Status -eq "Queued") {
    $Request=Get-MailboxExportRequest -Mailbox $sAMAccountName
    sleep -Seconds 5
    #Write-Host "Wait”
    }
    
    while ($Request.Status -eq "InProgress") {
    $Request=Get-MailboxExportRequest -Mailbox $sAMAccountName
    sleep -Seconds 5
    #Write-Host “running”
    }
    
    if ($Request.Status -eq "Completed") {
        Move-Item -Path $HomeDirectory -Destination $Destination -Force
        Get-MailboxExportRequest -Mailbox $sAMAccountName | Remove-MailboxExportRequest -Confirm:$false
        } else {
        $SysInfo="Error"
        $Info="Error on PST"
        }
    
    
    #Export Archive
    if ((Get-Mailbox -Identity $sAMAccountName).ArchiveDatabase -ne $null ) {
        New-MailboxExportRequest -Mailbox $sAMAccountName -FilePath "$HomeDirectoryArchiv" -IsArchive
        $Request=Get-MailboxExportRequest -Mailbox $sAMAccountName
    
        while ($Request.Status -eq "Queued") {
        $Request=Get-MailboxExportRequest -Mailbox $sAMAccountName
        sleep -Seconds 5
        #Write-Host "Warten"
        }
    
        while ($Request.Status -eq "InProgress") {
        $Request=Get-MailboxExportRequest -Mailbox $sAMAccountName
        sleep -Seconds 5
        #Write-Host "Läuft"
        }
    
        if ($Request.Status -eq "Completed") {
            Move-Item -Path $HomeDirectory -Destination $Destination -Force
            Get-MailboxExportRequest -Mailbox $sAMAccountName | Remove-MailboxExportRequest -Confirm:$false
            } else {
            $SysInfo="Error"
            $Info="Error on PST"
            }
    }
    


    Seidl Michael | http://www.techguy.at | twitter.com/techguyat | facebook.com/techguyat

    Thursday, June 5, 2014 4:08 PM
  • Thanks Michael. I will put this to test and get back to you.

    ~Rick

    Thursday, June 5, 2014 4:51 PM
  • Michael

    Other than some simple re-wording, this does exactly what I need it to do. Many thanks.

    ~Rick

    Thursday, June 5, 2014 6:44 PM