【发布时间】:2018-10-11 15:46:37
【问题描述】:
我创建了一个如下所示的 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.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/
整个过程在带有 PS 2.0.-1.-1 管理机器的 Windows 7 上运行,并使用所需的参数和参数创建了 4 个任务。订阅 - 事情有点棘手,因为我无法使用 GUI 操作事件触发器,只能编辑 XML。每个任务提供 2 个操作。
【问题讨论】:
-
请显示一些代码。我们可以帮助您,但前提是我们有事可做。
-
我现在无法访问我的代码,但这个问题一直在我脑海中旋转。我会尽快提供:)
-
请将编辑内容发布到您的帖子中,而不是在 cmets 中。这是为了便于阅读和其他帮助。也请以太回复我的评论或标题帖子而不是此评论,因为它属于 Ortolar
-
用你的工作代码更新了我的帖子
-
非常好,谢谢
标签: powershell