简体   繁体   中英

Running Powershell With PHP On IIS

PHP CODE -:

<?php

    $user = "email@outlook.com";

    $script = "C:\\inetpub\\wwwroot\\shell_script\\sc.ps1";

    $query = shell_exec("powershell -command $script <nul");
     if($query)
{
   echo "Successful" ;
}
else
   echo "Failed";
}
?>

POWERSHELL CODE -:

$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)

$Mail.To = "email2@outlook.com"
$Mail.Subject = "New Leave Request"
$Mail.Body ="
Dear,

This email contains the body of Email"

$Mail.Send()

I have Powershell installed on my laptop with Windows Server 2012 on Windows 8.

I have a website hosted on Windows Server 2012 IIS

When i run the powershell script sc.ps1 on command line on server by command./sc.ps1 the mail is sent to Outlook.

But when i run the php page in which i have the shell_exec command the mail is not triggered to the outlook and returns page with message Successful as written in PHP Code. All the work is done on Windows Server 2012

Running powershell script on command line sends mail but when running script via powershell doesn't trigger the mail and returns successful only as the shell-exec query is running.

I have done set-ExecutionPolicy as unrestricted.

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article in MSDN.

As a workaround you may use a low-level API - Extended MAPI which supports such scenarious, or just any other third-party wrapper around that API (for example, Redemption).

You probably have to pass the execution policy within your shell_exec :

shell_exec("powershell -executionpolicy unrestricted -command $script <nul");

If this doesn't work, you should check the identity of your application pool.

No Office app (Outlook included) can be used in a service.(such as IIS).

Your options are

  1. In case of an Exchange Server, use EWS to access the mailbox.

  2. Extended MAPI (C++ or Delphi only)

  3. Redemption (any language) - it wraps Extended MAPI and its RDO family of objects can be used from a service.

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