Я работаю над некоторыми сценариями автоматизации выпуска, которые используют Powershell для обновления существующих запланированных задач, которые выполняют различные приложения. В моем сценарии я могу установить Путь и Рабочий Каталог приложения, но это, кажется, не сохраняет изменения назад в задаче.
function CreateOrUpdateTaskRunner {
param (
[Parameter(Mandatory = $TRUE, Position = 1)][string]$PackageName,
[Parameter(Mandatory = $TRUE, Position = 2)][Version]$Version,
[Parameter(Mandatory = $TRUE, Position = 3)][string]$ReleaseDirectory
)
$taskScheduler = New-Object -ComObject Schedule.Service
$taskScheduler.Connect("localhost")
$taskFolder = $taskScheduler.GetFolder('\')
foreach ($task in $taskFolder.GetTasks(0)) {
# Check each action to see if it references the current package
foreach ($action in $task.Definition.Actions) {
# Ignore actions that do not execute code (e.g. send email, show message)
if ($action.Type -ne 0) {
continue
}
# Ignore actions that do not execute the specified task runner
if ($action.WorkingDirectory -NotMatch $application) {
continue
}
# Find the executable
$path = Join-Path $ReleaseDirectory -ChildPath $application | Join-Path -ChildPath $Version
$exe = Get-ChildItem $path -Filter "*.exe" | Select -First 1
# Update the action with the new working directory and executable
$action.WorkingDirectory = $exe.DirectoryName
$action.Path = $exe.FullName
}
}
}
Я до сих пор не мог найти очевидную функцию Сохранения в документации (https://msdn.microsoft.com/en-us/library/windows/desktop/aa383607 (v=vs.85) .aspx). Я проявляю неправильный подход здесь и потребность бездельничать с задачей XML?
Метод RegisterTask имеет флаг обновления, который вы могли бы использовать. Примерно так:
# Update the action with the new working directory and executable
$action.WorkingDirectory = $exe.DirectoryName
$action.Path = $exe.FullName
#Update Task
$taskFolder.RegisterTask($task.Name, $task.Definition, 4, "<username>", "<password>", 1, $null)
Подробную информацию по каждому параметру см. В статье msdn.