Powershell调试器如何修改运行时行为



我将代码减少到最低限度,以突出Powershell 的一个非常奇怪的问题

与pwsh 5.1.22000.282和7.2.2相同。

代码:

$outlook = new-object -com Outlook.Application;
$namespace = $outlook.GetNameSpace("MAPI");
$inputFolderObj=$namespace.Folders.Item('user@domain.com').Folders.Item('temp')
$scope = $inputFolderObj.FolderPath
$filter = ""
$search = $outlook.AdvancedSearch("'$scope'", $filter, $True)
$search.Results.Count

在CLI:上

PS C:dummyfolder> .test.ps1
0
PS C:dummyfolder> .test.ps1 # reproductible
0
PS C:dummyfolder> Set-PSBreakpoint -Line 7 -Script .test.ps1 | out-null
PS C:dummyfolder> .test.ps1
Passage en mode débogage. Utilisez h ou ? pour obtenir de l'aide.
Appuyez sur Point d'arrêt de ligne sur « C:Udummyfoldertest.ps1:7 »
Au caractère C:dummyfoldertest.ps1:7 : 1
+ $search.Results.Count
+ ~~~~~~~~~~~~~~~~~~~~~
[DBG]: PS C:dummyfolder>> c
1

是虫子吗?我错过什么了吗?谢谢,


编辑2022年3月17日-做了一些测试。似乎在第7行有一个断点并且只获取结果并不会每次都产生"1"。重新启动$search,然后在断点处多次启动$search.Results,最终产生"1"。因此,这可能是Outlook的马歇尔互操作的一个问题,但如果有人知道为什么。。。

答案:

处理Outlook的Marshall对象正在异步加载。

我们可以在结果计数之前添加一定的停顿来获取对象。调试断点无声地引入了这个额外的步骤。

最终代码:

$outlook = new-object -com Outlook.Application;
$namespace = $outlook.GetNameSpace("MAPI");
$inputFolderObj=$namespace.Folders.Item('user@domain.com').Folders.Item('temp')
$scope = $inputFolderObj.FolderPath
$filter = ""
$search = $outlook.AdvancedSearch("'$scope'", $filter, $True)
Start-Sleep 10
$search.Results.Count

最新更新