2014-02-12 40 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用戶部署需要本地管理員。