我正嘗試使用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"
我花了幾個小時試圖找出網上搜索,但無濟於事。
你的建議工作正常Ansgar。非常感謝解釋。 :)順便說一句,當我正在尋找解決方案時,我也碰到過這個命令:'$ sch_task_action = [IO.File] :: ReadAllText(「C:\ automation \ sch_task_action_final.conf」)'但是這也失敗了錯誤。任何想法爲什麼? – Technext
因爲它和'Get-ContentRaw'的功能相同。 –