简体   繁体   中英

Not able to spawn off email message from windows search in powershell

I have a powershell script that is searching the Windows Search index directly for emails and files. I have the following code:

$searchme="my thing to find"
$sql="SELECT System.FileName, System.ItemPathDisplay, System.DateCreated, System.DateModified, system.itemurl, system.itemtypetext FROM SYSTEMINDEX WHERE Contains(System.FileName, '"+$searchme+"') OR Contains('"+$searchme+"')"
$adapter = new-object system.data.oledb.oleDBDataadapter -argumentlist $sql, "Provider=Search.CollatorDSO;Extended Properties=’Application=Windows’;"
$ds      = new-object system.data.dataset
$adapter.Fill($ds)
foreach ($record in $ds.Tables[0].Rows)
{
    $exeparams = $record[4]
    write-host $exeparams
    write-host $exeparams.split(":")[0]
    if ($exeparams.split(":")[0] -eq "mapi15")
    {
        $exeparams2="mapi://" + $exeparams.substring(8)
    }
    write-host $exeparams
    write-host "start"
    $exe="start"
    $exe+" "+$exeparams | Out-File 'file.txt' -encoding Unicode
    write-host "start-process"
    Start-Process $exeparams
    Start-Process $exeparams2
    write-host "andpersand process"
    &$exe $exeparams
    &$exe $exeparams2
    write-host "dotnet"
    $proc = [Diagnostics.Process]::Start($exeparams)
    $proc.WaitForExit()
    $proc = [Diagnostics.Process]::Start($exeparams2)
    $proc.WaitForExit()
}

There are several "shell" calls because I was trying to figure out if it was a process spawning issue. Files work with no issue. Emails however fail with either No such interface if i leave mapi15, or Unspecified error if i change mapi15 to mapi. I believe that Open mails in outlook from java using the protocol "mapi://" may be the solution, but if it is I am not sure how to apply this in powershell. Thank you for your help.

Ok, that took more work than I expected, and I blame Office 2013 for it. Here's the short answer:

$exeparams2 = $exeparams -replace "^mapi15", "mapi"
& start $exeparams2

That is the code that now opens an email for me. That code did not do that yesterday, all it did is tell me:

Either there is no default mail client or the current mail client cannot fulfill the messaging request. Please run Microsoft Outlook and set it as the default mail client.

Infuriating is what this was, because I did have Outlook, in fact it was running, and was the default mail application for everything email related. This annoyed me, and sent me on a quest to figure out WTF was wrong, and if I could fix it. The answers to that are "I'm not real sure WTF was wrong, except maybe a naming change on MS's part", and "yes, yes I can fix it".

I finally found the fix that worked for me (and I believe that this is probably Office 2013/Office 365 specific ) was found at the bottom of this thread:

https://social.technet.microsoft.com/Forums/office/en-US/64c0bedf-2bcd-40aa-bb9c-ad5de20c84be/cannot-send-email-with-microsoft-outlook-2010-64-bit-from-adobe-outlook-not-recognised-as?forum=outlook

The process is simple. Change 2 Registry Entries, then re-set your default mail application. Registry entries:

HKLM:\\Software\\Clients\\Mail(default)
HKLM:\\Software\\Clients\\Mail\\Microsoft Outlook(default)

Change the value of (Default) from "Microsoft Outlook" to simply "Outlook".

After that I set Outlook to be the default for everything it could be ( in Win8 that's Control Panel > All Control Panel Items > Default Programs > Set Default Programs then select Outlook, and choose the first option to make it default for all extensions that it is registered for). Once that was done I was able to run the modified code above to launch an email that I had search the index for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM