【发布时间】:2017-07-02 17:51:35
【问题描述】:
我正在尝试使用 Powershell 在任务计划程序中创建一个任务。
$PSVersionTable.PSVersion 主要次要版本修订 ----- ----- ----- -------- 3 0 -1 -1
操作系统:Windows Server 2012
这是供参考的脚本:
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"
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"
当我尝试运行 PowerShell 脚本时,我收到以下错误:
Register-ScheduledTask:无法处理参数“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 而不是 net
125.14.93.7 而不是净 10.0.2.4"
” 键入“Microsoft.Management.Infrastructure.CimInstance[]”。
错误:“无法转换值”New-ScheduledTaskAction -Execute C:\windump\windump.exe
--参数 "-i 1 -q -w E:\network-dump\Network-Traces.txt -n -C 100 -W 100 -U -n ip 和
不是净 125.14.93.7 也不是净 10.0.2.4"
” 键入“Microsoft.Management.Infrastructure.CimInstance”。
错误:“指定的参数超出了有效值的范围。
参数名称:className""
在 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"
我花了几个小时试图通过在线搜索来弄清楚,但无济于事。
【问题讨论】:
标签: powershell scheduled-tasks powershell-3.0 windows-server-2012