none
Powershell and Outlook removestore method

    Question

  • Hi scripting guys,
    I am new to powershell (this is my first script), although I'm familar with scripting\VBS.
     
    I'm having trouble with using the removestore method to remove a .PST from Outlook 2010 running on Windows 7.

    When I run the following test code:

    $Outlook = new-object -com outlook.application 
    
    $Namespace = $Outlook.getNamespace("MAPI") 
    
    $PSTPath = "C:\Temp\Test.Pst"
    
    $Namespace.addStore($pstpath)
    
    $PSTFolder = $Namespace.stores | Where {$_.FilePath -eq $PSTPath} | ?{$Namespace.GetFolderFromID($_.StoreID)}
    
    $Namespace.RemoveStore($PSTFolder)
    
    
    I get this error generated from the last line:

    Cannot convert argument "0", with value: "System.__ComObject", for "RemoveStore " to type "Microsoft.Office.Interop.Outlook.MAPIFolder": "Cannot convert the "S ystem.__ComObject" value of type "System.__ComObject#{000630c7-0000-0000-c000-0 00000000046}" to type "Microsoft.Office.Interop.Outlook.MAPIFolder"." At C:\Temp\test.ps1:6 char:16 + $NS.RemoveStore <<<< ($PSTFolder) + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

    Other sources seem to use the removestore without any problems.  I'm not sure if this is a powershell or Outlook problem.
    Any clues on how I can get this working please?

    Many thanks,

    Tim
    • Edited by goldendel Monday, March 7, 2011 1:23 AM formatting
    Monday, March 7, 2011 1:22 AM

Answers

  • Here is an InvokeMember example (based on the link Bill provided):

     

    
    $Outlook = new-object -com outlook.application 
    $Namespace = $Outlook.getNamespace("MAPI") 
    $PSTPath = "C:\scripts\Test1.Pst"
    $Namespace.addStore($pstpath)
    $PSTFolder = $Namespace.Folders.Item('Test1.pst')
    
    $Namespace.GetType().InvokeMember(
      'RemoveStore',
      [System.Reflection.BindingFlags]::InvokeMethod,
      $null,
      $Namespace,
      ($PSTFolder))
    
    

    Uros Calakovic
    • Marked as answer by goldendel Saturday, March 19, 2011 11:45 AM
    Thursday, March 17, 2011 5:39 PM
    Moderator

All replies

  • Hi,

    It looks like you are running into the problem described here: http://abhishek225.spaces.live.com/blog/cns!13469C7B7CE6E911!165.entry?sa=59540192

    Bill

    Monday, March 14, 2011 6:50 PM
    Moderator
  • Thanks so much for your reply Bill.  Trust me to find a powershell limitation in my first script! (the 2nd one worked like a dream).

    I will try what you have suggested and respond once I have it working.

    Tim

    Monday, March 14, 2011 9:39 PM
  • Hi Bill,

    I'm not having much luck.  I've tried:

    <strong>Add-Type -AssemblyName microsoft.office.interop.outlook</strong>
    <strong>$Outlook = new-object -comobject outlook.application 
    $Namespace = $Outlook.getNamespace('MAPI') 
    $PSTPath = "C:\Temp\Test.Pst"
    $Namespace.addStore($pstpath)
    $PSTFolder = $Namespace.Stores | Where {$_.FilePath -eq $PSTPath} | ?{$Namespace.GetFolderFromID($_.StoreID)}
    $Namespace.RemoveStore($PSTFolder)</strong>
    

     

     

     

    and

    [Reflection.Assembly]::LoadWithPartialname("Microsoft.Office.Interop.Outlook") | out-null
    $Outlook = new-object -comobject outlook.application 
    $Namespace = $Outlook.getNamespace('MAPI') 
    $PSTPath = "C:\Temp\Test.Pst"
    $Namespace.addStore($pstpath)
    $PSTFolder = $Namespace.Stores | Where {$_.FilePath -eq $PSTPath} | ?{$Namespace.GetFolderFromID($_.StoreID)}
    $Namespace.RemoveStore($PSTFolder)
    

    Both fail with the same error:

    Cannot convert argument "0", with value: "System.__ComObject", for "RemoveStore" to type "Microsoft.Office.Interop.Outlook.MAPIFolder": "Cannot convert the "System.__ComObject" value of type "System.__ComObject#{000630c7-0000-0000-c000-000000000046}" to type "Microsoft.Office.Interop.Outlook.MAPIFolder"."
    At C:\Temp\test1.ps1:15 char:23
    + $Namespace.RemoveStore <<<< ($PSTFolder)
        + CategoryInfo          : NotSpecified: (:) [], MethodException
        + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

    Are you able to offer any further guidance?

    Many thanks,

    Tim

     

     

    Thursday, March 17, 2011 4:33 AM
  • Hi,

    Sorry, I don't use Outlook any more, so I can't offer any further guidance.

    Bill

    Thursday, March 17, 2011 2:52 PM
    Moderator
  • Here is an InvokeMember example (based on the link Bill provided):

     

    
    $Outlook = new-object -com outlook.application 
    $Namespace = $Outlook.getNamespace("MAPI") 
    $PSTPath = "C:\scripts\Test1.Pst"
    $Namespace.addStore($pstpath)
    $PSTFolder = $Namespace.Folders.Item('Test1.pst')
    
    $Namespace.GetType().InvokeMember(
      'RemoveStore',
      [System.Reflection.BindingFlags]::InvokeMethod,
      $null,
      $Namespace,
      ($PSTFolder))
    
    

    Uros Calakovic
    • Marked as answer by goldendel Saturday, March 19, 2011 11:45 AM
    Thursday, March 17, 2011 5:39 PM
    Moderator
  • Thanks Uros.  Your example gave me enough to get it working.  I found that the foldername defined needs to be the displayname.  Now it works perfectly :)  Thank you so much!

    Here is the sample code to create a PST, backup selected folders and then disconnect the PST.  The most difficult bit by far was the disconnect.  It is working with Outlook 2010:

    $outlook = New-Object -ComObject Outlook.Application
    $namespace = $outlook.getnamespace("MAPI")
    $Calendar = $namespace.GetDefaultFolder(9)
    $Contacts = $namespace.GetDefaultFolder(10)
    $Tasks = $namespace.GetDefaultFolder(13)
    $Notes = $namespace.GetDefaultFolder(12)
    $Drafts = $namespace.GetDefaultFolder(16)
    $PSTPath = "C:\Temp\Example.Pst"
    $PSTDisplayName = "Backup" #Define variable for DisplayName
    
    $namespace.AddStore($PSTPath) #Attach .PST
    
    $PST = $namespace.Stores | ? {$_.FilePath -eq $PSTPath} #Find Store just attached as .PST
    $PSTRoot = $PST.GetRootFolder() #Get Root Folder name of PST
    $PSTRoot.Name = $PSTDisplayName # Change Displayname for PST to Backup
    
    $Calendar.CopyTo($PST) | out-null #Copy Calendar items to PST
    $Contacts.CopyTo($PST) | out-null #Copy Contact items to PST
    $Tasks.CopyTo($PST) | out-null #Copy Task items to PST
    $Notes.CopyTo($PST) | out-null #Copy Note items to PST
    $Drafts.CopyTo($PST) | out-null #Copy Draft items to PST
    
    #Cleanup Variables before PST disconnect
    
    rm variable:Calendar
    rm variable:Contacts
    rm variable:Tasks
    rm variable:Notes
    rm variable:Drafts
    
    $PSTFolder = $Namespace.Folders.Item($PSTDisplayName) #Bind to PST for disconnection
    $Namespace.GetType().InvokeMember('RemoveStore',[System.Reflection.BindingFlags]::InvokeMethod,$null,$Namespace,($PSTFolder)) #Disconnect .PST
    
    #Cleanup reamining variables before closure
    
    rm variable:PSTFolder
    rm variable:PSTDisplayName
    rm variable:PSTRoot
    rm variable:PST
    rm variable:PSTPath
    rm variable:namespace
    rm variable:outlook
    
    Saturday, March 19, 2011 11:50 AM