看看使用预定任务是否有效。
我一直难以从 PHP 调用 powershell 脚本。PHP 一直超时(不是所有的 powershell 命令;只有一些命令。可能与安全设置和 WAMP 的运行方式有关)。以下是一种解决方法。不优雅,但它完成了工作。在您的情况下,您只想运行一个普通的非 powershell 命令行命令,这将不会更简单,并且相同的方法应该可以工作。
至于我,我将 powershell 脚本称为计划任务,如下所示(在 PHP 中):
$cmd = 'c:\wamp64\www\runme4.bat';
$cmd2 = 'SCHTASKS /F /Create /TN _notepad3 /TR "'.$cmd.'" /SC DAILY /RU INTERACTIVE';
shell_exec($cmd2);
shell_exec('SCHTASKS /RUN /TN "_notepad3"');
shell_exec('SCHTASKS /DELETE /TN "_notepad3" /F');
我必须将 powershell 命令放在 .bat 中的原因是因为双引号太多。单引号不起作用。这是我的runme4.bat的内容(注意两个点之间有一个空格。这个poweshell命令运行ps1脚本(脚本叫做AppointmentsToday.ps1)加上调用里面的函数。里面的函数叫做Get -今天的约会):
powershell -executionpolicy 绕过 -noexit "..\Get-AppointmentsToday.ps1;Get-AppointmentsToday"
更具体地说,在我的案例中, powershell 脚本是这样的(在本地版本的 Outlook 2019 的 detault 和用户定义的日历中查找今天的约会。仅获取包含主题标签或单词“会议”的主题。保存将结果输出到 .txt 文件中):
Set-ExecutionPolicy Bypass -Scope CurrentUser -Force
Function Get-AppointmentsToday
{
Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null
$olFolders = "Microsoft.Office.Interop.Outlook.OlDefaultFolders" -as [type]
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace("MAPI")
$folder = $namespace.getDefaultFolder($olFolders::olFolderCalendar)
$folder.items | Select-Object -Property Subject, Start
foreach($customfolder in $folder.Folders)
{
$customfolder.items | Select-Object -Property Subject, Start
}
} #end function Get-OutlookCalendar
$todayStart = Get-Date -Hour 0 -Minute 0 -Second 0
$todayEnd = Get-Date -Hour 23 -Minute 59 -Second 59
Get-AppointmentsToday | where-object { $_.start -gt $todayStart -AND $_.start -lt $todayEnd -AND ($_.subject -ilike "*#*" -OR $_.subject -ilike "*meeting*") } | Out-File -FilePath \wamp64\www\AppointmentsToday.txt
当我自己进入 Powershell 或者自己从命令行调用它时,代码运行顺利。但是,一旦我尝试从 PHP 运行它或将其打包到 .EXE 中,脚本就无法完成。PHP一直超时。我删除了所有行,然后逐行重新添加它们。一旦我取消注释这两行,执行就会从内存中停止: $folder = $namespace.getDefaultFolder($olFolders::olFolderCalendar) $folder.items | 选择对象 - 属性主题,开始
计划任务方法对我有用。希望我的方法可以帮助处于类似情况的人。如果其他 .EXE 应用程序无法使用正常方法加载,您也可以使用此 shedulestask 方法运行它们。我很高兴知道一种更好的做事方式。