微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

windows-server-2008-r2 – 如何使用Powershell修改现有的计划任务?

我正在研究一些使用Power shell更新执行各种应用程序的现有计划任务的发布自动化脚本.在我的脚本中,我可以设置应用程序的路径和工作目录,但似乎不会将更改保存回任务.
function CreateOrUpdateTaskRunner {
    param (
        [Parameter(Mandatory = $TRUE,Position = 1)][string]$PackageName,[Parameter(Mandatory = $TRUE,Position = 2)][Version]$Version,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文章.

原文地址:https://www.jb51.cc/windows/369141.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐