简体   繁体   中英

Read csv file to create tasks that trigger from an event id using powershell and Task Scheduler COM-Object

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.

OK the question looks like it could just be simply

"How do I store the values of a CSV in a variable and loop through them?"

Lets go over some basic Powershell concepts to understand what you should be looking for.

The solution is.

Import-Csv -Path "C:\Test.csv" | %{
    Put Code Here
}

Ok lets explain this.

Lets image we have a csv called test.csv and the inside looks like

"FirstName","LastName","Age" "Bill","Boxer","52" "Steve","Nix","21"

When I call Import-CSV it creates a PSObject with properties that are equal to the header (First line of the CSV) :

(Import-csv "C:\Test.csv").FirstName

will return

Bill Steve

In powershell we love piping | What this does is takes the information for the previous command and moves it to the next command using $_ as the variable. We also love shorthand known as Alias. Like %{ Code Here } is short hand for foreach-object

so

Import-csv "C:\Test.csv" | %{
    "My Name Is $($_.FirstName) $($_.LastName) and I am $($_.Age) years old"
}

Will get the data from the CSV and convert it to a PSObject. It will then Pipe | to a Foreach-Object aka %{} and then display the string :

My Name Is Bill Boxer and I am 52 years old My Name Is Steve Nix and I am 21 years old

On a side note the $() inside the quotes is called a expression "$(Code here)" Allows you to output the code inside of a string

"Hello $_.FirstName"

Would show everything in the variable $_ then add a .FirstName to the end of it like :

@{FirstName=Bill; LastName=Boxer; Age=52}.FirstName

But when you add the $() expression you are saying run this part as code.

"Hello $($_.FirstName)"

Shows

Hello Bill

So lets take a look at the code you posted in the comments (Please keep in mind to edit your post and post it there for other people to read.

Based on what I have posted above the issue is that your CSV isnt a CSV. CSV stands for Comma Separated Value. You have your values separated by Semi Colons.

You will need to define that in the Param for import-Csv -Delimiter ";"

A working script would be this :

$path = "C:\Workspace\macros\FullReportPathMR.csv"
Import-CSV -Path $path -Delimiter ";" | %{
    "$($_.sequence) $($_.reportsource) $($_.reportname) $($_.reportid) $($_.fullpath)"
}

output

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

The code below will load a CSV where the code is located.
Than it will loop inside of the file and create a schedule task.
This script requires admin rights to create a new scheduled task and it will create a task as the current user.

# get scripth path location
$scriptpath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition

# import CSV
$csvdata = Import-Csv "$scriptpath\t.csv"

# this will clycle throught the data inside the object $csvdata
foreach ($result in $csvdata) {
    # to access the data just refer to object $result.[columns names inside CSV]

    # Build task info
    $action  = New-ScheduledTaskAction -Execute "$($result.fullpath)" -Argument 'case there is any argument'
    $trigger = New-ScheduledTaskTrigger -Daily -At 9am 
    Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $($result.reportname) -Description $($result.reportname)
    # create the task scheduler
    New-ScheduledTaskAction -Execute 
}

The task creation part was base on: https://blogs.technet.microsoft.com/heyscriptingguy/2015/01/13/use-powershell-to-create-scheduled-tasks/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM