scriptproperty的动态计算值



下面的脚本是将msgs转换为pdfs的更大项目的一部分。我在实现attachments自定义属性时遇到的问题。我希望它可以根据邮件附件的计算值进行自定义。MailItem.Attachments.Count不会。它包括嵌入在消息正文中的图像。这可以通过第二个片段来规避。然而,我无法将两者结合起来。

主脚本:

$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)
}

第二部分:

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

显然,这会奏效。

$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

解释我的所作所为。我把"第二部分"和COM Object一起放在Add-Member之前,以便进行计算。我已经测试过了。唯一的问题是我每次都必须重新启动ISE。否则,$results变量将保持填充状态,脚本将无法按预期工作。

相关内容

  • 没有找到相关文章

最新更新