Search code examples
powershellpropertiesscriptblockcustom-propertiesadd-member

Dynamically calculated value for scriptproperty


The script below is a part of a bigger project of converting msgs to pdfs. What I'm having problem with implementing is the attachments custom property. I'd like is for it to take custom value based on calculated value based on msg attachments. The MailItem.Attachments.Count will not do. It includes images embeden in the body of message. This can be circumvented with the second snippet. I however cannot combide the two.

Main script:

$o = new-object -comobject outlook.application
$path = "PATH"
cd $path
gc test.csv|Select -skip 2 -First 1|%{$_|Add-Member -MemberType NoteProperty -Name 'BaseName' -Value $_.substring($_.Lastindexof('\')+1).substring(0, $_.substring($_.Lastindexof('\')+1).Lastindexof('.'));
$_|Add-Member -MemberType NoteProperty -Name 'FileName' -Value $_.substring($_.Lastindexof('\')+1);
$_|Add-Member -MemberType NoteProperty -Name 'FullName' -Value $_;
$_|Add-Member -MemberType NoteProperty -Name 'Folder' -Value $_.substring(0, $_.Lastindexof('\'));
$_|Add-Member -MemberType ScriptProperty -Name 'New Loc' -Value {if($msg.Attachments.Count -eq 0){$msgDirectory + '\Converted\'}elseif($msg.Attachments.Count -ge 1){$msgDirectory + '\Converted\' + $msgBaseName + '\'}};





$_|Add-Member -MemberType ScriptProperty -Name 'Attachments' -Value {IF ((SECOND SNIPPET) -gt 0){"YES"}ELSE{"NO}};




$msgBaseName = $_.BaseName
$msgFullname = $_.FullName
$msgDirectory = $_.Folder
$msgName = $_.Filename

$msg = $o.CreateItemFromTemplate($msgFullname)
}

Second part:

$results = 0
$msg.Attachments|%{$att = $_
$attach = $att.FileName; 
$file = 'C:\Users\anowak\Downloads\Script_Test\' + $attach
$file
IF(($msg.HTMLBody) -like "*cid:$attach*"){}else{$results ++} #check if 'attachment' present in the body
$results

Solution

  • Apparently this will do the trick.

    $o = new-object -comobject outlook.application
    $path = "Path"
    cd $path
    
    $output = @()
    
    gc test.csv |
      Select -skip 3 -First 1 | 
      % { 
    
        $_ | Add-Member -MemberType NoteProperty -Name 'BaseName' -Value $_.substring($_.Lastindexof('\') + 1).substring(0, $_.substring($_.Lastindexof('\') + 1).Lastindexof('.'))
        $_ | Add-Member -MemberType NoteProperty -Name 'FileName' -Value $_.substring($_.Lastindexof('\') + 1)
        $_ | Add-Member -MemberType NoteProperty -Name 'FullName' -Value $_
        $_ | Add-Member -MemberType NoteProperty -Name 'Folder' -Value $_.substring(0, $_.Lastindexof('\'))
        $_ | Add-Member -MemberType ScriptProperty -Name 'New Loc' -Value { if ($msg.Attachments.Count -eq 0) { $msgDirectory + '\Converted\' }elseif ($msg.Attachments.Count -ge 1) { $msgDirectory + '\Converted\' + $msgBaseName + '\' } }
    
        $msgBaseName = $_.BaseName
        $msgFullname = $_.FullName
        $msgDirectory = $_.Folder
        $msgName = $_.Filename
    
        $msg = $o.CreateItemFromTemplate($msgFullname)
    
        $results = 0
        $msg.Attachments | 
          % { 
            $att = $_
            $attach = $att.FileName
            $file = 'Path' + $attach
            IF (($msg.HTMLBody) -like "*cid:$attach*") {} else { $results++ } #check if 'attachment' present in the body
            $results
          }
      
        $_ | Add-Member -MemberType ScriptProperty -Name 'Attachments' -Value { if ($results -eq 0) { 'NO' } else { 'Yes' } };
    
        $output += $_ | Select FullName, FileName, "New Loc", Attachments
    
      }
    
    $output | Out-GridView
    

    To explain what I've done. I placed 'second part' before Add-Member along with COM Object so that calculations can be made. I've tested it and works. The only issue is that I have to restart ISE everytime. Otherwise the $results variable will remain populated and the script will not work as intended.