Powershell session issue?

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
rtiel1
Posts: 15
Joined: Thu Jul 26, 2012 11:34 pm

Powershell session issue?

Post by rtiel1 » Fri Jul 27, 2012 5:58 am

I'm running the below script in Powershell Studio 2012 which is not running properly. I want to export a mailbox to a pst file which works great when I run the script directly in the Exchange Management Shell. But when executing the script from Powershell Studio 2012 the export request is generated but the status stays on "queued". No pst file is created till I execute a command to remove the export request, then the pst file is created???Can anyone help me?


# Exchange 2010 SP1 Mailbox Export Script
# Steve Goodman. Use at your own risk!
#http://www.stevieg.org/tag/new-mailboxexportrequest/

###############
# Settings #
###############

# Pick ONE of the two below. If you choose both, it will use $Server.
$Server = "Exchange"
$Database = ""

# Share to export mailboxes to. Needs R/W by Exchange Trusted Subsystem
# Must be a UNC path as this is run by the CAS MRS service.
$ExportShare = "[LOCATION]"

# After each run a report of the exports can be dropped into the directory specified below. (The user that runs this script needs access to this share)
# Must be a UNC path or the full path of a local directory.
$ReportShare = "[LOCATION]"

# Shall we remove the PST file, if it exists beforehand? (The user that runs this script needs access to the $ExportShare share)
# Valid values: $true or $false
$RemovePSTBeforeExport = $false

###############
# Code #
###############

if ($Server)
{
if (!(Get-ExchangeServer $Server -ErrorAction SilentlyContinue))
{
throw "Exchange Server $Server not found";
}
if (!(Get-MailboxDatabase -Server $Server -ErrorAction SilentlyContinue))
{
throw "Exchange Server $Server does not have mailbox databases";
}

$Mailbox = get-mailbox "FTest"

# Pre-checks done

# Queue all mailbox export requests
if ($RemovePSTBeforeExport -eq $true -and (Get-Item "$ExportShare$($Mailbox.Alias).pst" -ErrorAction SilentlyContinue))
{
Remove-Item "$ExportShare$($Mailbox.Alias).pst" -Confirm:$false
}
New-MailboxExportRequest -Mailbox $Mailbox.Alias -FilePath "$ExportShare$($Mailbox.Alias).pst"


Write-Output "Waiting for export to complete"

# Wait for mailbox export requests to complete
while ((Get-MailboxExportRequest | Where {$_.Status -eq "Queued" -or $_.Status -eq "InProgress"}))
{

sleep 10
}

# Write reports if required
if ($ReportShare)
{
Write-Output "Writing reports to $ReportShare"
$Completed = Get-MailboxExportRequest | Where {$_.Status -eq "Completed"} | Get-MailboxExportRequestStatistics | Format-List
if ($Completed)
{
$Completed | Out-File -FilePath "$ReportShare$($Mailbox.Alias)_Completed.txt"
}
$Incomplete = Get-MailboxExportRequest | Where {$_.Status -ne "Completed"} | Get-MailboxExportRequestStatistics | Format-List
if ($Incomplete)
{
$Incomplete | Out-File -FilePath "$ReportShare$($Mailbox.Alias)_Incomplete.txt"
}
}

# Remove Requests
Get-MailboxExportRequest | Remove-MailboxExportRequest -Confirm:$false
}

User avatar
rtiel1
Posts: 15
Joined: Thu Jul 26, 2012 11:34 pm

Powershell session issue?

Post by rtiel1 » Fri Jul 27, 2012 5:58 am

I'm running the below script in Powershell Studio 2012 which is not running properly. I want to export a mailbox to a pst file which works great when I run the script directly in the Exchange Management Shell. But when executing the script from Powershell Studio 2012 the export request is generated but the status stays on "queued". No pst file is created till I execute a command to remove the export request, then the pst file is created???Can anyone help me?


# Exchange 2010 SP1 Mailbox Export Script
# Steve Goodman. Use at your own risk!
#http://www.stevieg.org/tag/new-mailboxexportrequest/

###############
# Settings #
###############

# Pick ONE of the two below. If you choose both, it will use $Server.
$Server = "Exchange"
$Database = ""

# Share to export mailboxes to. Needs R/W by Exchange Trusted Subsystem
# Must be a UNC path as this is run by the CAS MRS service.
$ExportShare = "[LOCATION]"

# After each run a report of the exports can be dropped into the directory specified below. (The user that runs this script needs access to this share)
# Must be a UNC path or the full path of a local directory.
$ReportShare = "[LOCATION]"

# Shall we remove the PST file, if it exists beforehand? (The user that runs this script needs access to the $ExportShare share)
# Valid values: $true or $false
$RemovePSTBeforeExport = $false

###############
# Code #
###############

if ($Server)
{
if (!(Get-ExchangeServer $Server -ErrorAction SilentlyContinue))
{
throw "Exchange Server $Server not found";
}
if (!(Get-MailboxDatabase -Server $Server -ErrorAction SilentlyContinue))
{
throw "Exchange Server $Server does not have mailbox databases";
}

$Mailbox = get-mailbox "FTest"

# Pre-checks done

# Queue all mailbox export requests
if ($RemovePSTBeforeExport -eq $true -and (Get-Item "$ExportShare$($Mailbox.Alias).pst" -ErrorAction SilentlyContinue))
{
Remove-Item "$ExportShare$($Mailbox.Alias).pst" -Confirm:$false
}
New-MailboxExportRequest -Mailbox $Mailbox.Alias -FilePath "$ExportShare$($Mailbox.Alias).pst"


Write-Output "Waiting for export to complete"

# Wait for mailbox export requests to complete
while ((Get-MailboxExportRequest | Where {$_.Status -eq "Queued" -or $_.Status -eq "InProgress"}))
{

sleep 10
}

# Write reports if required
if ($ReportShare)
{
Write-Output "Writing reports to $ReportShare"
$Completed = Get-MailboxExportRequest | Where {$_.Status -eq "Completed"} | Get-MailboxExportRequestStatistics | Format-List
if ($Completed)
{
$Completed | Out-File -FilePath "$ReportShare$($Mailbox.Alias)_Completed.txt"
}
$Incomplete = Get-MailboxExportRequest | Where {$_.Status -ne "Completed"} | Get-MailboxExportRequestStatistics | Format-List
if ($Incomplete)
{
$Incomplete | Out-File -FilePath "$ReportShare$($Mailbox.Alias)_Incomplete.txt"
}
}

# Remove Requests
Get-MailboxExportRequest | Remove-MailboxExportRequest -Confirm:$false
}

User avatar
rtiel1
Posts: 15
Joined: Thu Jul 26, 2012 11:34 pm

Powershell session issue?

Post by rtiel1 » Fri Jul 27, 2012 5:59 am

After more testing I found out that the script does work if I open Powershell studio 2012 for the first time and run the script for the first time. After the succesfull run and trying to execute the same script but for a different user the problem occurs. Does this mean it has to do something with the powershell session?

Locked