none
PowerShell: suppressing hashtable error messages RRS feed

  • Question

  • Hi, 

    I am trying to get a list of all the sub folders and sub-sub-folders. I am using Powershell, and I am adding key-value pairs to a hashtable. Some keys are expected to be duplicates, in which case I dont need to add the pair. How can I suppress the error message? -errorAction does not seem to work here. 

    $Outlook = New-Object -ComObject 'Outlook.Application' -ErrorAction 'Stop'
    $pstRootFolder =$outlook.GetNameSpace('MAPI').Stores|?{("C:\Data\Outlook\Archive.pst" -eq [string]$_.FilePath)}|%{$_.GetRootFolder()}

    $AllFolders = @{}
    ForEach ($Folder in $pstRootFolder.Folders) {$AllFolders.Add($Folder.EntryID, $Folder)}

    ForEach ($Folder in $AllFolders.Values.Folders) {$AllFolders.Add($Folder.EntryID, $Folder)} 

    ForEach ($Folder in $pstRootFolder.Folders) {$AllFolders.Add($Folder.EntryID, $Folder)} -errorAction SilentContinue

    Exception calling "Add" with "2" argument(s): "Item has already been added. Key in dictionary: '000000005A41BF34832FAC4CA08F5F45358E6A2F62800000'  Key being added:
    '000000005A41BF34832FAC4CA08F5F45358E6A2F62800000'"
    At line:1 char:46
    + ... ootFolder.Folders) {$AllFolders.Add($Folder.EntryID, $Folder)} -error ...
    +                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : ArgumentException 

    Friday, June 9, 2017 6:35 PM

Answers

  • if( -not $allFolders.ContainsKey('xxx')){$allFolders.Add($Folder.EntryID, $Folder)}

    \_(ツ)_/

    Friday, June 9, 2017 6:51 PM