I created a CSV File that looks like this
sequence,reportsource,reportname,reportid,fullpath
It contains several lines of configuration data for the scheduled tasks. I already figured out to use import-csv to get the content of the file. Now I want to loop through every line of the file and store all the values of the line to variables - collection ($sequence,$reportsource,$reportname,$reportid,$fullpath), so I can create scheduled tasks with this values, but I don't know how to access the object, that import-csv provides. I need those values from the csv to hand over arguments to schtasks.exe to create my tasks.
$path = "C:\Workspace\macros\FullReportPathMR.csv"
$PSVersionTable.PSVersion
$csv = Import-CSV -Path $path -Delimiter ";" | %{
##SCHTASKS /Create /TN $($_.reportname) /sc monthly /tr "C:\Program Files (x86)\Microsoft Office###\Office14\EXCEL.EXE"
#New-ScheduledTaskAction -Execute "C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.EXE" -Argument #"$($_.fullpath)"
#}
# The name of the scheduled task
[string]$TaskName = "$($_.reportsource) - $($_.reportname) - $($_.reportid)"
# The description of the task
[string]$TaskDescr = "Hello, it's you again! I am $($_.reportname) and I will get started, when Report ID $($_.reportid) is fired. Have a nice day!"
# The Task Action command
$TaskCommand0 = "kill.cmd"
$TaskCommand1 = "`"C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.EXE`""
# The Task Action command argument
$TaskArg = "hallelujah"
$TaskArg1 = "$($_.fullpath)"
# attach the Task Scheduler com object
$service = new-object -ComObject("Schedule.Service")
# connect to the local machine.
# http://msdn.microsoft.com/en-us/library/windows/desktop/aa381833(v=vs.85).aspx
$service.Connect()
$rootFolder = $service.GetFolder("\")
$TaskDefinition = $service.NewTask(0)
$TaskDefinition.RegistrationInfo.Description = "$TaskDescr"
$TaskDefinition.Settings.Enabled = $true
$TaskDefinition.Settings.AllowDemandStart = $true
$triggers = $TaskDefinition.Triggers
#http://msdn.microsoft.com/en-us/library/windows/desktop/aa383915(v=vs.85).aspx
$trigger = $triggers.Create(0) # Creates an "On an event" trigger
$trigger.Subscription = "<QueryList><Query Id='0'><Select Path='Application'>*[System[Provider[@Name='$($_.reportsource)'] and EventID='$($_.reportid)']]</Select></Query></QueryList>"
# http://msdn.microsoft.com/en-us/library/windows/desktop/aa381841(v=vs.85).aspx
$Action = $TaskDefinition.Actions.Create(0)
$action.Path = "$TaskCommand0"
$action.Arguments = "$TaskArg"
$Action = $TaskDefinition.Actions.Create(0)
$action.Path = "$TaskCommand1"
$action.Arguments = "$TaskArg1"
#http://msdn.microsoft.com/en-us/library/windows/desktop/aa381365(v=vs.85).aspx
$rootFolder.RegisterTaskDefinition("$TaskName",$TaskDefinition,6,"System",$null,5)
Write-Host $($_.sequence) $($_.reportsource) $($_.reportname) $($_.reportid) $($_.fullpath) "task created successfully."
}
CSV File Contains
sequence;reportsource;reportname;reportid;fullpath
1;Monthly Report;MR1;3;C:\Workspace\macros\1.txt
2;Monthly Report;MR2;6;C:\Workspace\macros\2.txt
3;Monthly Report;MR3;9;C:\Workspace\macros\3.txt
4;Monthly Report;MR4;12;C:\Workspace\macros\4.txt
https://www.verboon.info/2013/12/powershell-creating-scheduled-tasks-with-powershell-version-3/
https://community.spiceworks.com/topic/1028906-trigger-schedule-task-on-an-eventid
https://powershell.org/forums/topic/scheduled-task-with-multiple-actions/
The whole thing ran through on a Windows 7 with PS 2.0.-1.-1 administration machine and created 4 tasks with the desired parameters and arguments. The subscription - thing is a bit tricky, since I can't manipulate the event trigger using the GUI, just editing the XML. Every task provides 2 actions.