我將代碼減少到最低限度,以突出 Powershell 的一個非常奇怪的問題
pwsh 5.1.22000.282 和 7.2.2 相同。
代碼:
$outlook = new-object -com Outlook.Application;
$namespace = $outlook.GetNameSpace("MAPI");
$inputFolderObj=$namespace.Folders.Item('[email protected]').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:\Udummyfolder\test.ps1:7 ?
Au caractère C:\dummyfolder\test.ps1:7 : 1
$search.Results.Count
~~~~~~~~~~~~~~~~~~~~~
[DBG]: PS C:\dummyfolder>> c
1
它是一個錯誤嗎?我在這里想念什么嗎?謝謝,
編輯 17.03.2022 - 做了一些測驗。似乎在第 7 行有一個斷點并且只獲取結果不會每次都產生“1”。重新啟動$search,然后$search.Results在斷點處多次最終產生“1”。因此,這可能是 Outlook 的 Marshall 互操作的問題,但如果有人知道為什么......
uj5u.com熱心網友回復:
回答:
處理 Outlook 的 Marshall 物件正在異步加載。
我們可以在結果計數之前添加一定的暫停來獲取物件。除錯斷點默默地引入了這個額外的步驟。
最終代碼:
$outlook = new-object -com Outlook.Application;
$namespace = $outlook.GetNameSpace("MAPI");
$inputFolderObj=$namespace.Folders.Item('[email protected]').Folders.Item('temp')
$scope = $inputFolderObj.FolderPath
$filter = ""
$search = $outlook.AdvancedSearch("'$scope'", $filter, $True)
Start-Sleep 10
$search.Results.Count
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/453631.html
