1

我正尝试使用Powershell在任务计划程序中创建任务。无法使用PowerShell在任务计划程序中创建任务

$PSVersionTable.PSVersion 

Major Minor Build Revision 
----- ----- ----- -------- 
3  0  -1  -1

操作系统:Windows Server 2012

下面是参考脚本:

内容 sch_task_action.conf
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" 

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

注:在脚本中,如果我通过的sch_task_action_final.conf(已经选择的IP移除)的内容直接向可变(如下所示),而不读取使用Get-Content该文件,那么脚本工作正常和任务在调度器中创建。

$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" 

我花了几个小时试图找出网上搜索,但无济于事。

回答

2

Get-Content -Raw返回一个字符串,该字符串不由PowerShell解释并且不能按原样传递给参数-Action。从sch_task_action_final.conf删除New-ScheduledTaskAction -Execute-Argument和改变

$sch_task_action = Get-Content c:\automation\sch_task_action_final.conf -Raw 

$action, $params = (Get-Content C:\automation\sch_task_action_final.conf -Raw) -split ' ', 2 
$sch_task_action = New-ScheduledTaskAction -Execute $action -Arugment $params 

另外,您可以使用Invoke-Expression

$sch_task_action = Invoke-Expresssion (Get-Content c:\automation\sch_task_action_final.conf -Raw) 

,但我不会建议。

+0

你的建议工作正常Ansgar。非常感谢解释。 :)顺便说一句,当我正在寻找解决方案时,我也碰到过这个命令:'$ sch_task_action = [IO.File] :: ReadAllText(“C:\ automation \ sch_task_action_final.conf”)'但是这也失败了错误。任何想法为什么? – Technext

+0

因为它和'Get-ContentRaw'的功能相同。 –

0

您可以使用COM-Object创建计划任务。使用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 

这里是链接,供大家参考:Scheduled Task in PS

希望它能帮助。

+0

谢谢,但我实际上正在寻找_my_脚本失败的原因。 – Technext

+0

@Technext:我看到Ansgar已经给出了一个很好的解释......我的观点是给出另一个出路。 :) –