WiX CustomAction with elevated privileges SCHTASKS "Access Denied"

时间:2016-08-31 18:01:22

标签: windows wix schtasks.exe

My installer currently installs a few files to a directory and then executes some CustomAction scripts to create 2 scheduled tasks named Script1 and Script2.

I have another 2 CustomAction scripts called RemoveScript1 and RemoveScript2.

They are all the same type 34 CA's with the following attributes:

<CustomAction Id="XXXXXX"
              Directory="AFolder"
              ExeCommand="XXXXXX"
              Execute="deferred"
              Impersonate="no"
              Return="asyncWait" />

And the following install sequence.

<InstallExecuteSequence>
  <Custom Action="RemoveScript1" Before="InstallFinalize" >REMOVE="ALL"</Custom>
  <Custom Action="RemoveScript2" Before="InstallFinalize" >REMOVE="ALL"</Custom>
  <Custom Action="Script1" Before="InstallFinalize" >(NOT Installed) OR MaintenanceMode="Modify"</Custom>
  <Custom Action="Script2" Before="InstallFinalize" >(NOT Installed) OR MaintenanceMode="Modify"</Custom>
</InstallExecuteSequence>

Example Create script:

SCHTASKS.exe /Create /TN NAME /TR "pathtosomeexe" /SC ONEVENT /EC System /MO *[System/EventID=XXXX] /F

Example Delete script:

SCHTASKS /Delete /TN NAME /F

The create scripts execute fine, however, the delete scripts do not seem to work. When checking the logs I see the following lines related to RemoveScriptX.

  • MSI (s) (60:B0) [17:20:49:555]: Doing action: RemoveScript1
  • Action start 17:20:49: RemoveScript1.
  • Action ended 17:20:49: RemoveScript1. Return value 1.
  • MSI (s) (60:B0) [17:20:49:649]: Executing op: ActionStart(Name=RemoveScript1,,)
  • MSI (s) (60:B0) [17:20:49:649]: Executing op: CustomActionSchedule(Action=RemoveScript1,ActionType=3234,Source=AFolder,Target=C:\Windows\SysWOW64\SCHTASKS.exe /Delete /TN NAME /F,)
  • 1: RemoveScript1 2: 1631

When I try to delete these scripts created by the installer, using the normal command line, I get "ERROR: Access is denied." . Which is great! It means the scheduled tasks were created by admin. I can delete the task using the admin command line as expected.

If I create a task using the normal command line, I can delete it normally as expected.

I should note I have tried both InstallScope = perMachine, and perUser. The only difference it seems is that I have to run the actual installer as Admin for perUser to access ProgramFiles directory for the installation of the other files.

Why are my delete scripts not using admin/elevated privileges? Have I missed something stupid? I had assumed "1631" was the error code returned from SCHTASKS.exe, please correct me if I am wrong.

OS: Windows 10

WixVersion: 3.10

1 个答案:

答案 0 :(得分:0)

这可能不是理想的答案,但这是我解决问题的方法。

而不是像这样的参数调用SCHTASKS:

[SystemFolder]SCHTASKS.exe /Create /TN TASKNAME

我改为给POWERSHELL打电话给SCHTASKS及其论据如下:

[POWERSHELLEXE] &amp; SCHTASKS /Create /TN TASKNAME

当我转向使用WiX QtExec http://wixtoolset.org/documentation/manual/v3/customactions/qtexec.html时,我无法发布我用于powershell版本的确切代码。

它需要将脚本放入属性“Value”而不是原始问题中使用的“ExeCommand”,并且每个格式的格式都有所不同。