[英]Unable to create task in task scheduler using PowerShell
I am trying to create a task in task scheduler using Powershell. 我正在尝试使用Powershell在任务计划程序中创建任务。
$PSVersionTable.PSVersion Major Minor Build Revision ----- ----- ----- -------- 3 0 -1 -1
OS: Windows Server 2012 操作系统:Windows Server 2012
Here's the script for reference: 这是供参考的脚本:
Set-Location c:\automation
# Remove IP of host on which this script is going to run
$hostname = hostname
$private_ip = (Test-Connection $hostname -count 1).IPv4Address.IPAddressToString
(get-content .\sch_task_action.conf) | foreach-object {$_ -replace "and not net $private_ip", ""} | set-content .\sch_task_action_final.conf
# Create task schedular
$sch_task_action = Get-Content c:\automation\sch_task_action_final.conf -Raw
$sch_task_principal = New-ScheduledTaskPrincipal -UserId "LOCALSERVICE" -LogonType ServiceAccount
$sch_task_settings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit (New-TimeSpan -Days 7)
$trigger = New-ScheduledTaskTrigger -AtStartup
Register-ScheduledTask -Action $sch_task_action -Trigger $trigger -Principal $sch_task_principal -Settings $sch_task_settings -TaskName "Monitoring"
Content of sch_task_action.conf
: sch_task_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"
When I try to run the PowerShell script, I'm getting the following error: 当我尝试运行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
Note: In the script, if I pass the content of sch_task_action_final.conf
(which has the selected IP removed) directly to the variable (as shown below) without reading the file using Get-Content
, then the script works fine and task gets created in the scheduler. 注意:在脚本中,如果我不使用
Get-Content
读取文件而将sch_task_action_final.conf
(已删除选定IP)的内容直接传递到变量(如下所示),则脚本可以正常工作并创建任务在调度程序中。
$sch_task_action = 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"
I've spent few hours trying to figure out by searching online but to no avail. 我花了几个小时试图通过在线搜索找出答案,但无济于事。
Get-Content -Raw
returns a string, which is not interpreted by PowerShell and cannot be passed as-is to the parameter -Action
. Get-Content -Raw
返回一个字符串,PowerShell不会解释该字符串,并且不能按Get-Content -Raw
将其传递给参数-Action
。 Remove New-ScheduledTaskAction -Execute
and -Argument
from sch_task_action_final.conf
and change 删除
New-ScheduledTaskAction -Execute
和-Argument
从sch_task_action_final.conf
和变化
$sch_task_action = Get-Content c:\automation\sch_task_action_final.conf -Raw
to 至
$action, $params = (Get-Content C:\automation\sch_task_action_final.conf -Raw) -split ' ', 2
$sch_task_action = New-ScheduledTaskAction -Execute $action -Arugment $params
Alternatively you could use Invoke-Expression
: 或者,您可以使用
Invoke-Expression
:
$sch_task_action = Invoke-Expresssion (Get-Content c:\automation\sch_task_action_final.conf -Raw)
but I wouldn't recommend that. 但我不建议这样做。
You can create a scheduled Task using COM-Object . 您可以使用COM-Object创建计划任务。 Using Schedule.Service , you can create it:
使用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
Here is the link for your reference: Scheduled Task in PS 这是供您参考的链接: PS中的计划任务
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.