[英]Importing Scheduled Tasks with Powershell in Windows 2012 R2
I am very new to Powershell scripting but have attempted to modify a script that I found here to import some XML Scheduled Tasks using Powershell in Windows 2012 R2. 我对Powershell脚本非常陌生,但是尝试修改我在这里找到的脚本,以便在Windows 2012 R2中使用Powershell导入一些XML计划任务。
I have been successful in importing the Scheduled Tasks into the root [Task Scheduler Library] using this script. 我已成功使用此脚本将“计划任务”导入到根[Task Scheduler Library]中。
The problem seems to be that the Schedule Tasks need to be imported into a subfolder under Task Scheduler Library, let's say "SubTasks" 问题似乎是计划任务需要导入到任务计划程序库下的子文件夹中,比如说“ SubTasks”
$task_path = "C:\Users\me\Desktop\ST Testing\exported ST's\scheduledTask.xml"
$task_user = "usr"
$task_pass = "pwd"
$schedule = new-object -com("Schedule.Service")
$schedule.Connect("server") # servername
#$folder = $schedule.GetFolder("\") <===This works fine
$folder = $schedule.GetFolder("\SubTasks") #<===This does not work
Write-Host $folder
Get-Item $task_path | % {
$task_name = $_.Name.Replace('.xml', '')
$task_xml = Get-Content $_.FullName
$task = $schedule.NewTask($null)
$task.XmlText = $task_xml
$folder.RegisterTaskDefinition($task_name, $task, 6, $task_user, $task_pass, 1, $null)
} }
When I run the above Powershell Script I receive this error: 当我运行上述Powershell脚本时,出现此错误:
Exception calling "RegisterTaskDefinition" with "7" argument(s): "The specified path is invalid. (Exception from HRESULT: 0x800700A1)" At C:\\Users\\me\\Desktop\\ST Testing\\ImportSTs.ps1:22 char:5 + $folder.RegisterTaskDefinition($task_name, $task, 6, $task_user, $task_pass, ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ComMethodTargetInvocation
带有“ 7”参数的异常调用“ RegisterTaskDefinition”:“指定的路径无效。(来自HRESULT的异常:0x800700A1)”在C:\\ Users \\ me \\ Desktop \\ ST Testing \\ ImportSTs.ps1:22 char:5 + $ folder.RegisterTaskDefinition($ task_name,$ task,6,$ task_user,$ task_pass,... + ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 〜~~~ + CategoryInfo:未指定:(:) [],MethodInvocationException + FullyQualifiedErrorId:ComMethodTargetInvocation
Thanks in advance. 提前致谢。
if you're on powershell 3 (win2k12 - so you are) and above, there is a whole module for task scheduling. 如果您使用的是Powershell 3(win2k12-如此)及以上版本,则有一个用于任务调度的完整模块。
see: gcm -Module PSScheduledJob
参见:
gcm -Module PSScheduledJob
however, it does not seem to come with an easy way to import tasks from xml. 但是,它似乎没有一种从xml导入任务的简便方法。
there are modules for that, this particular module deserializes the xml file and tests for all settings, seems more cumbersome than schtasks solution listed below. 有一些模块可以使该文件反序列化xml文件并测试所有设置,这似乎比下面列出的schtasks解决方案更麻烦。
for powershell 2 (win2k8) I found it easier to use schtasks
- full ref here: 对于powershell 2(win2k8),我发现使用
schtasks
更容易-完整的参考在这里:
https://msdn.microsoft.com/en-us/library/windows/desktop/bb736357(v=vs.85).aspx https://msdn.microsoft.com/en-us/library/windows/desktop/bb736357(v=vs.85).aspx
for example (scheduling a task from an xmlfile, the task will run under a particular user creds: 例如(从xmlfile安排任务,该任务将在特定的用户信誉下运行:
schtasks /Create /XML $xmlfile /RU $creds.UserName /RP $creds.GetNetworkCredential().Password /TN "My Task Name"
I got my xml file by manually creating the task and exporting to xml (I removed credential and info nodes from xml) 我通过手动创建任务并导出到xml获得了xml文件(我从xml中删除了凭证和信息节点)
Register-ScheduledTask
supports using XML to define the task: Register-ScheduledTask
支持使用XML定义任务:
Register-ScheduledTask `
-User $task_user `
-Password $task_pass `
-TaskName ([IO.Path]::GetFileNameWithoutExtension($task_path)) `
-TaskPath '\SubTasks' `
-Xml (Get-Content $task_path -Raw)
Note that you need to fetch the actual XML content . 请注意,您需要获取实际的XML 内容 。 The
-Raw
flag prevents Get-Content
from returning a String[]
, which also makes Register-ScheduledTask
unhappy. -Raw
标志可防止Get-Content
返回String[]
,这也会使Register-ScheduledTask
不满意。
Also note that Register-ScheduledTask
has a -TaskPath
argument, allowing you specify a subfolder to put the task in if desired. 还要注意,
Register-ScheduledTask
具有-TaskPath
参数,如果需要,您可以指定一个子文件夹将任务放入其中。
I'm not fond of using the grave mark for line continuation, so I prefer splatting to spread things out over multiple lines: 我不喜欢使用坟墓标记来继续行,因此我更喜欢使用散布法将内容分布在多行上:
$taskArgs = @{
User='usr';
Password='pwd';
TaskName=([IO.Path]::GetFileNameWithoutExtension($task_path));
TaskPath='\SubTasks';
Xml=(Get-Content $task_path -Raw)
}
Register-ScheduledTask @taskArgs
Was able to get this to work for our needs. 能够使它满足我们的需求。
$task_user="username"
$task_pass="password"
$schedule=new-object -ComObject ("Schedule.Service")
$schedule.Connect("server")
$folder=$schedule.GetFolder("\tasks")
$path="\\server\c$\temp\Tasks\"
Get-ChildItem $path -Filter "*.xml"| foreach {
$task_conf=Get-Item -Path $_.FullName
$taskname=$task_conf.Name
$task_xml=$task_conf.FullName
$task=$schedule.NewTask(0)
$task.XmlText=(Get-Content $task_xml).Replace('Task version="1.1" xmlns','Task version="1.2" xmlns')
$folder.RegisterTaskDefinition($taskname,$task,6,$task_user,$task_pass,1,$null)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.