使用PowerShell在Windows 2008中安排任务

时间:2022-12-20 02:17:31

i want to schedule task in windows 2008 r2 with powershell. i used

我想使用powershell在Windows 2008 r2中安排任务。我用了

   $username = "BUILTIN\Users"
   $TaskName = 'kakaka'
   $t=1
   $TaskRun = "$PSHome\powershell.exe  Start-Process -FilePath 'C:\v.exe' -ArgumentList '/a' -Verb runas -WindowStyle Normal"
   $start = (Get-Date).AddMinutes($t).ToString("HH:mm") 
   schtasks /create /ru $username /tn $Taskname /tr $TaskRun /sc once /st $start /f

on using above script i got an error "schtasks.exe : ERROR: The task XML contains a value which is incorrectly formatted or out of range." same script worked fine on windows 7.

使用上面的脚本我得到一个错误“schtasks.exe:错误:任务XML包含一个格式不正确或超出范围的值。”相同的脚本在Windows 7上运行良好。

1 个解决方案

#1


It seems there is issue of password which is resolved in below post :

好像在下面的帖子中解决了密码的问题:

https://social.technet.microsoft.com/forums/windowsserver/en-US/fe7345ea-b037-469b-b425-17d5fee28e91/windows-server-backup-wont-schedule

SYMPTOMS

When you try to change the run as user for a scheduled task in Server 2008, The task is currently configured to runas SYSTEM. Using the following command line he receives the error. 

c:\>SCHTASKS /CHANGE /TN TestTask /RU TestUser 

SCHTASKS prompts the user for a password for the runas user account. After entering the password it displays the following error. 


"ERROR: The task XML contains a value which is incorrectly formatted or out of range. (52,4):Task:" 


The task's runas credentials have not changed. 


RESOLUTION

To avoid this error you must add the /RP option with an "*". The new command line will look like this: 

c:\>SCHTASKS /CHANGE /TN TestTask /RU TestUser /RP * 

SCHTASKS prompts the user for a password for the runas user account. After entering the password the task is updated without error.

#1


It seems there is issue of password which is resolved in below post :

好像在下面的帖子中解决了密码的问题:

https://social.technet.microsoft.com/forums/windowsserver/en-US/fe7345ea-b037-469b-b425-17d5fee28e91/windows-server-backup-wont-schedule

SYMPTOMS

When you try to change the run as user for a scheduled task in Server 2008, The task is currently configured to runas SYSTEM. Using the following command line he receives the error. 

c:\>SCHTASKS /CHANGE /TN TestTask /RU TestUser 

SCHTASKS prompts the user for a password for the runas user account. After entering the password it displays the following error. 


"ERROR: The task XML contains a value which is incorrectly formatted or out of range. (52,4):Task:" 


The task's runas credentials have not changed. 


RESOLUTION

To avoid this error you must add the /RP option with an "*". The new command line will look like this: 

c:\>SCHTASKS /CHANGE /TN TestTask /RU TestUser /RP * 

SCHTASKS prompts the user for a password for the runas user account. After entering the password the task is updated without error.