2014-02-12 35 views
0

我想在c#中以编程方式为任务管理器创建一个新的任务。 但我需要为任务添加参数。说“myProg.exe -doSomething”。 到目前为止,我确实找到了使用正常schtasks命令行添加参数的任何解决方案(http://technet.microsoft.com/en-us/library/cc772785%28v=ws.10%29.aspx) 因此,我想我可以创建一个XML并导入它。将任务添加到任务管理器与C#中的xml和schtasks#

在xml:使用上http://technet.microsoft.com/en-us/library/cc722156.aspx 的例子

<?xml version="1.0" encoding="UTF-16"?> 
<Task version="1.1" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"> 
    <RegistrationInfo> 
    <Author>mldz</Author> 
    </RegistrationInfo> 
    <Triggers> 
    <TimeTrigger> 
     <StartBoundary>2014-02-12T20:01:00</StartBoundary> 
     <EndBoundary>2015-02-13T16:44:04.324784</EndBoundary> 
     <Enabled>true</Enabled> 
    </TimeTrigger> 
    </Triggers> 
    <Principals> 
    <Principal id="Author"> 
     <RunLevel>LeastPrivilege</RunLevel> 
     <UserId>User-PC\myusername</UserId> 
     <LogonType>InteractiveTokenOrPassword</LogonType> 
    </Principal> 
    </Principals> 
    <Settings> 
    <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries> 
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries> 
    <IdleSettings> 
     <StopOnIdleEnd>false</StopOnIdleEnd> 
     <RestartOnIdle>false</RestartOnIdle> 
    </IdleSettings> 
    <Enabled>true</Enabled> 
    <Hidden>false</Hidden> 
    <RunOnlyIfIdle>false</RunOnlyIfIdle> 
    <WakeToRun>false</WakeToRun> 
    <ExecutionTimeLimit>P3D</ExecutionTimeLimit> 
    <DeleteExpiredTaskAfter>PT0S</DeleteExpiredTaskAfter> 
    <Priority>5</Priority> 
    </Settings> 
    <Actions Context="Author"> 
    <Exec> 
     <Command>myapp.exe</Command> 
     <Arguments>-doSomething</Arguments> 
     <WorkingDirectory></WorkingDirectory> 
    </Exec> 
    </Actions> 
</Task> 

我试图导入XML文件(保存在 “C:\测试\的test.xml”)

的schtasks /创建/ s的“USER-PC”/ u“muusername”/ p“mypassword”/ xml“c:\ test \ task.xml”/ tn“from xml”

但是我得到这个错误: 错误:用户凭据不是在本地机器上允许。

我试着这样说: 的schtasks /创建/ s的 “USER-PC”/ XML: “从XML”

“C:\测试\ task.xml”/ TN但我得到这个错误 ERROR :访问被拒绝。

所以我试图

C:\用户\ mldz>的schtasks /创建/ s的 “USER-PC”/る “名为myUsername”/ RP “输入mypassword”/ XML “C:\测试\ task.xml”/tn“uit xml”

但又是: 错误:访问被拒绝。

如何导入xml文件到任务管理器?或者在这样的命令添加参数:

schtasks /create /sc once /tn myTask /tr "MyApp.exe -backup" /st 20:01 /F /Z /V1 /rp "mypassword" /ru "mydomain\myusername"

回答

0

schtasks /create /tn startMyApp /tr "myapp.exe -dowork" /sc once /st 16:50 /f

SUCCESS: The scheduled task "startMyApp" has successfully been created.

创建;-)似乎留出的V1/Z被招添加的参数。

但是,如果有人能够帮助我解决XML问题,请告诉我。

1

/U/P参数具体是什么会给你访问到服务器,这样就可以CREATE任务。不是你需要RUN的任务。如果您在示例中给出的凭据特别是您想要运行该任务的帐户,那么您需要改用/RU/RP参数。

此答案的评价是thebackroomtech.com。我正在做同样的事情

0

这适用于PowerShell 2.0或更高版本。不是C#,但很容易适应。

$taskname = 'mytask' 
$taskpath = 'mypath' 
$schtasks = 'c:\windows\system32\schtasks.exe' 
$compname = ($env:COMPUTERNAME) 
$xmlfile = 'c:\mydirectory\tasks\mytask.xml' 
$principal = "mydomain\myuser" 
$password = 'mypassword' 
$tn = "$taskpath\$taskname" 
Invoke-Expression "& $schtasks /create /s $compname /ru $principal /rp $password /xml $xmlfile /tn $tn /f" 

schtasks.exe是有限的。除非您使用xml,否则有些参数无法配置。我在GUI中创建了任务,然后将其导出到xml。(注意:与GPO用户部署需要本地管理员。