Powershell을 사용하여 작업 스케줄러에서 작업을 만들려고합니다.PowerShell을 사용하여 작업 스케줄러에서 작업을 만들 수 없습니다.
$PSVersionTable.PSVersion Major Minor Build Revision ----- ----- ----- -------- 3 0 -1 -1
OS :
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
의 : 윈도우 서버 다음은 2012
는 참조 스크립트의
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 스크립트를 실행하려고, I 다음 오류가 발생했습니다 :
,210Register-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
참고 : Get-Content
를 사용하여 파일을 읽지 않고 (아래 그림 참조) 내가 직접 변수 (선택된 IP를 제거했다) sch_task_action_final.conf
의 내용을 전달하는 경우 스크립트에서, 다음 스크립트는 벌금과 작업 작동 스케줄러에서 생성됩니다.
$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에서 효과가있었습니다. 설명에 감사드립니다. :) 솔루션을 찾고 있었을 때 BTW, 나는 또한이 명령을 가로 질러왔다 :'$ sch_task_action = [IO.File] :: ReadAllText ("C : \ automation \ sch_task_action_final.conf")'이 역시 실패했다. 오류. 왜 그런가? – Technext
그것은'Get-Content -Raw'와 같은 일을하기 때문에. –