无法使用PowerShell在任务计划程序中创建任务
我正在尝试使用Powershell在任务计划程序中创建任务 $PSVersionTable.PSVersion Major Minor Build Revision ----- ----- ----- -------- 3 0 -1 -1无法使用PowerShell在任务计划程序中创建任务,powershell,scheduled-tasks,powershell-3.0,windows-server-2012,Powershell,Scheduled Tasks,Powershell 3.0,Windows Server 2012,我正在尝试使用Powershell在任务计划程序中创建任务 $PSVersionTable.PSVersion Major Minor Build Revision ----- ----- ----- -------- 3 0 -1 -1 schu task\u action.conf的内容: New-ScheduledTaskAction -Execute c:\windump\windump.exe –Argument "-i 1 -q -w e:\
schu task\u action.conf的内容
:
New-ScheduledTaskAction -Execute c:\windump\windump.exe –Argument "-i 1 -q -w e:\network-dump\Network-Traces.txt -n -C 100 -W 100 -U -n ip and not net 125.14.93.7 and not net 10.0.2.4"
尝试运行PowerShell脚本时,出现以下错误:
Register-ScheduledTask : Cannot process argument transformation on parameter 'Action'.
Cannot convert value "New-ScheduledTaskAction -Execute C:\windump\windump.exe –Argument
"-i 1 -q -w E:\network-dump\Network-Traces.txt -n -C 100 -W 100 -U -n ip and not net
125.14.93.7 and not net 10.0.2.4"
" to type "Microsoft.Management.Infrastructure.CimInstance[]".
Error: "Cannot convert value "New-ScheduledTaskAction -Execute C:\windump\windump.exe
–Argument "-i 1 -q -w E:\network-dump\Network-Traces.txt -n -C 100 -W 100 -U -n ip and
not net 125.14.93.7 and not net 10.0.2.4"
" to type "Microsoft.Management.Infrastructure.CimInstance".
Error: "Specified argument was out of the range of valid values.
Parameter name: className""
At C:\Automation\windump\win_dump.ps1:26 char:32
+ Register-ScheduledTask -Action $sch_task_action -Trigger $trigger -Principal $sc ...
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Register-ScheduledTask], ParameterBindingArgumentTransformationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,Register-ScheduledTask
我花了几个小时试图通过在线搜索找出答案,但没有结果。
获取内容-Raw
返回一个字符串,PowerShell不会解释该字符串,并且无法按原样传递给参数-Action
。从schu task\u action\u final.conf
中删除New ScheduledTaskAction-Execute
和-Argument
,然后更改
$sch_task_action = Get-Content c:\automation\sch_task_action_final.conf -Raw
到
或者,您可以使用
调用表达式
:
$sch_task_action = Invoke-Expresssion (Get-Content c:\automation\sch_task_action_final.conf -Raw)
但我不建议这样做。您可以使用COM对象创建计划任务。使用Schedule.Service,您可以创建它:
$service = new-object -ComObject("Schedule.Service")
# connect to the local machine.
$service.Connect()
$rootFolder = $service.GetFolder("\")
$TaskDefinition = $service.NewTask(0)
$TaskDefinition.RegistrationInfo.Description = "$TaskDescr"
$TaskDefinition.Settings.Enabled = $true
$TaskDefinition.Settings.AllowDemandStart = $true
$TaskDefinition.Settings.StartWhenAvailable = $true
$TaskDefinition.Settings.StopIfGoingOnBatteries=$false
$TaskDefinition.Settings.DisallowStartIfOnBatteries=$false
$TaskDefinition.Settings.MultipleInstances=2
$taskdefinition.Settings.WakeToRun=$true
$triggers = $TaskDefinition.Triggers
$trigger = $triggers.Create(1) # Creates a "One time" trigger
$trigger.StartBoundary = $TaskStartTime.ToString("yyyy-MM-dd'T'HH:mm:ss")
$time_interval=New-TimeSpan -Minutes $interval
$time_interval=$time_interval.TotalSeconds
$trigger.Repetition.Interval= "PT"+"$time_interval"+"S"
$trigger.Enabled = $true
$TaskDefinition.Principal.RunLevel =1
$Action = $TaskDefinition.Actions.Create(0)
$action.Path = "$TaskCommand"
$action.Arguments = "$TaskArg"
# In Task Definition,
# 6 indicates "the task will not execute when it is registered unless a time-based trigger causes it to execute on registration."
# 5 indicates "Indicates that a Local System, Local Service, or Network Service account is being used as a security context to run the task.In this case, its the SYSTEM"
$rootFolder.RegisterTaskDefinition("$TaskName",$TaskDefinition,6,"System",$null,5) | Out-Null
"Scheduled Task has been created successfully"
Write-Log -Message "Scheduled Task has been created successfully with the name SHA_$schedule_name" -Level Info
以下是供您参考的链接:
希望能有所帮助。你的建议很有效。非常感谢您的解释。:)顺便说一句,当我在寻找解决方案时,我也遇到了这个命令:
$sch_task_action=[IO.File]::ReadAllText(“C:\automation\sch_task_action_final.conf”)
,但同样的错误也导致了失败。知道为什么吗?因为它与获取内容-Raw
的功能相同。谢谢,但实际上我一直在寻找脚本失败的原因。@Technext:我看到Ansgar已经给出了很好的解释……我的目的是给出另一条出路。:)
$action, $params = (Get-Content C:\automation\sch_task_action_final.conf -Raw) -split ' ', 2
$sch_task_action = New-ScheduledTaskAction -Execute $action -Arugment $params
$sch_task_action = Invoke-Expresssion (Get-Content c:\automation\sch_task_action_final.conf -Raw)
$service = new-object -ComObject("Schedule.Service")
# connect to the local machine.
$service.Connect()
$rootFolder = $service.GetFolder("\")
$TaskDefinition = $service.NewTask(0)
$TaskDefinition.RegistrationInfo.Description = "$TaskDescr"
$TaskDefinition.Settings.Enabled = $true
$TaskDefinition.Settings.AllowDemandStart = $true
$TaskDefinition.Settings.StartWhenAvailable = $true
$TaskDefinition.Settings.StopIfGoingOnBatteries=$false
$TaskDefinition.Settings.DisallowStartIfOnBatteries=$false
$TaskDefinition.Settings.MultipleInstances=2
$taskdefinition.Settings.WakeToRun=$true
$triggers = $TaskDefinition.Triggers
$trigger = $triggers.Create(1) # Creates a "One time" trigger
$trigger.StartBoundary = $TaskStartTime.ToString("yyyy-MM-dd'T'HH:mm:ss")
$time_interval=New-TimeSpan -Minutes $interval
$time_interval=$time_interval.TotalSeconds
$trigger.Repetition.Interval= "PT"+"$time_interval"+"S"
$trigger.Enabled = $true
$TaskDefinition.Principal.RunLevel =1
$Action = $TaskDefinition.Actions.Create(0)
$action.Path = "$TaskCommand"
$action.Arguments = "$TaskArg"
# In Task Definition,
# 6 indicates "the task will not execute when it is registered unless a time-based trigger causes it to execute on registration."
# 5 indicates "Indicates that a Local System, Local Service, or Network Service account is being used as a security context to run the task.In this case, its the SYSTEM"
$rootFolder.RegisterTaskDefinition("$TaskName",$TaskDefinition,6,"System",$null,5) | Out-Null
"Scheduled Task has been created successfully"
Write-Log -Message "Scheduled Task has been created successfully with the name SHA_$schedule_name" -Level Info