szczeppan
szczeppan

Reputation: 34

Powershell error during making copy of outlook folder

I try to make copy of all folders from user account into pst file. Script works fine for all folders except Inbox. When I try to copy data from inbox at end of copying I have error

Exception calling "CopyTo" with "1" argument(s): "Cannot move or copy folders. Cannot copy folder. A top-level folder cannot be copied to one of its subfolders. Or, you may not have appropriate permissions for the folder. To check your permissions for the folder, right-click the folder, and then click Properties on the shortcut menu."

Script below

$start = Get-Date

#checking if outlook is running
$isRunning = (@(Get-Process -ea silentlycontinue OUTLOOK).count -gt 0)

#showing dialog to choose direcotry where pst should be saved
$app = new-object -com Shell.Application
$folder = $app.BrowseForFolder(0, "Select Folder", 0, "C:\")
if ($folder.Self.Path -ne "") {
    $path = $folder.Self.Path+"\"+$env:USERNAME+".pst"
} else {
    Write-Host "directory not selected. Exiting.."
    [Environment]::Exit(1)
}

#reference to outlook
Add-Type -Assembly "Microsoft.Office.Interop.Outlook" | out-null
$outlook = New-Object -ComObject outlook.application
$ns = $outlook.GetNamespace("MAPI")

#adding personal archive file
$ns.AddStoreEx($path, 1)
Write-Host created $path
$dst = $ns.Folders.GetLast()
Write-Host folder name: $dst.Name

#list of directory types to copy. more details https://msdn.microsoft.com/en-us/library/office/ff861868.aspx
$folderTypes = 9,10,3,16,6,12,4,5,30,13,28

#iterating trough list of directories and for each dir make copy in pst file. next counting of objects in archive and pst
foreach ($id in $folderTypes) { 
    $src = $ns.GetDefaultFolder($id)
    $tmp = $src.copyTo($dst)
    Write-Host copied to $tmp.name 
}

#deattaching personal store from outlook
$ns.GetType().InvokeMember('RemoveStore',[System.Reflection.BindingFlags]::InvokeMethod, $null, $ns, ($dst))
Write-Host $dst.name closed

#if outlook wasn't running on start we must close it
if (-not $isRunning) {
    Write-Host closing outlook
    $outlook.Quit()
}

$end = Get-Date
$diff = New-TimeSpan -start $start -end $end
Write-Host done
"time elapsed {0:g}" -f $diff

Any ideas why this error occurs? Thanks in advace

Upvotes: 0

Views: 782

Answers (1)

Eugene Astafiev
Eugene Astafiev

Reputation: 49395

Make sure that you don't have the DisableCrossAccountCopy key in the windows registry set. Here is what the CopyTo method description states:

Setting the REG_MULTI_SZ value, DisableCrossAccountCopy, in HKCU\Software\Microsoft\Office\14.0\Outlook in the Windows registry has the side effect of disabling this method.

Also I'd suggest releasing underlying COM objects in the code instantly. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. This is particularly important if your add-in attempts to enumerate more than 256 Outlook items in a collection that is stored on a Microsoft Exchange Server. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. Read more about that in the Systematically Releasing Objects article.

Upvotes: 0

Related Questions