我创建了一个CSV文件,如下所示
sequence、reportsource、reportname、reportid _、fullpath
它包含多行计划任务的配置数据。我已经想好了使用import-csv来获取文件的内容。现在,我希望遍历文件的每一行,并将该行的所有值存储到变量集合($sequence、$reportsource、$reportname、$reportid、$fullpath),这样我就可以使用这些值创建计划任务,但是我不知道如何访问import-csv提供的对象。我需要csv中的那些值来将参数传递给schtasks.exe来创建我的任务。
$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文件包含
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.txthttps://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/
整个过程在装有PS 2.0的Windows 7管理机器上运行,并使用所需的参数和参数创建了4个任务。订阅的事情有点棘手,因为我不能使用GUI操作事件触发器,只能编辑XML。每个任务提供2个操作。
发布于 2018-10-12 00:21:55
下面的代码将加载代码所在的CSV。
然后它将在文件内循环并创建一个调度任务。
此脚本需要管理员权限才能创建新的计划任务,并且它将以当前用户的身份创建任务。
# 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
}https://stackoverflow.com/questions/52764152
复制相似问题