从 XML 使用 Powershell v2 创建新的计划任务
Creating a new scheduled task with Powershell v2 from XML
我需要使用 Powershell v2 创建一个新的计划任务。到目前为止我尝试的是任务调度程序 com 对象,如下所示:
$task_path = "c:\Temp\tasks\*.xml"
$task_user = "Administrator"
$task_pass = "mypass"
$sch = New-Object -ComObject("Schedule.Service")
$sch.connect("localhost")
$folder = $sch.GetFolder("\")
Get-Item $task_path | %{
$task_name = $_.Name.Replace('.xml', '')
$task_xml = Get-Content $_.FullName
$task = $sch.NewTask($null)
$task.XmlText = $task_xml
$folder.RegisterTaskDefinition($task_name, $task, 6, $task_user, $task_pass, 1, $null)
}
现在我面临的问题是我不想在方法 RegisterTaskDefinition
中指定除任务名称和任务对象之外的任何其他信息,因为一切都在 XML 文件,甚至是用户名和密码。
如何操作?
试试这个:
schtasks.exe /Create /XML C:\task.xml /tn taskname
希望对您有所帮助!
我需要使用 Powershell v2 创建一个新的计划任务。到目前为止我尝试的是任务调度程序 com 对象,如下所示:
$task_path = "c:\Temp\tasks\*.xml"
$task_user = "Administrator"
$task_pass = "mypass"
$sch = New-Object -ComObject("Schedule.Service")
$sch.connect("localhost")
$folder = $sch.GetFolder("\")
Get-Item $task_path | %{
$task_name = $_.Name.Replace('.xml', '')
$task_xml = Get-Content $_.FullName
$task = $sch.NewTask($null)
$task.XmlText = $task_xml
$folder.RegisterTaskDefinition($task_name, $task, 6, $task_user, $task_pass, 1, $null)
}
现在我面临的问题是我不想在方法 RegisterTaskDefinition
中指定除任务名称和任务对象之外的任何其他信息,因为一切都在 XML 文件,甚至是用户名和密码。
如何操作?
试试这个:
schtasks.exe /Create /XML C:\task.xml /tn taskname
希望对您有所帮助!