Windows Task Scheduler图表?

根循环

有谁知道是否有任何实用程序/工具可以根据时间和日期来绘制Windows Task Scheduler中所有任务的图表,所以我会知道在特定时间点是否存在任何重叠导致服务器过载?

我们有一个用于所有计划任务的服务器,由于前管理员的糟糕设计,它最近运行缓慢,现在我需要找出每个任务的运行窗口,我可以在excel中手动制作图表,但也可以一件一件一件地要经历的事情。

希望会有一些实用程序可以做到这一点。

敲打器

这PowerShell脚本将读取任务计划的事件日志,并出口到CSV Task NameStart DateFinish DateDuration用于分别推出的所有任务。然后,您可以将该数据提供给您选择的电子表格并构建GANTT图。

要求:

  • PowerShell 2.0
  • Windows Server 2008 \ Vista

脚本接受以下参数:

  • 计算机要查询的计算机名称数组。如果未指定,它将查询本地计算机。
  • MaxEvents从事件日志中读取的最大事件数。默认值为100。
  • 路径磁盘上的现有文件夹,将在其中保存CSV。如果未指定,将使用脚本文件夹。CSV的命名如下:COMPUTERNAME_TaskScheduler.csv
  • 用户用于远程认证的用户名。
  • 密码用户密码。如果未指定,它将由脚本请求。
  • 详细脚本将通过Write-Verbose消息告诉您发生了什么

示例(从PowerShell控制台运行):

从本地计算机获取数据,处理最近的100个事件,将CSV保存到脚本文件夹:

.\TS_Gantt.ps1

从远程计算机获取数据,处理最后200个事件,将CSV保存到以下c:\ts_gantt文件夹:

.\TS_Gantt.ps1 -Computers Sun, Earth, Moon -MaxEvents 200 -Path 'c:\ts_gantt'

脚本(TS_Gantt.ps1):

Param
(
    [Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
    [ValidateNotNullOrEmpty()]
    [string[]]$Computers = $env:COMPUTERNAME,

    [Parameter(ValueFromPipelineByPropertyName = $true)]
    [ValidateRange(1, 2147483647)]
    [int]$MaxEvents = 100,

    [Parameter(ValueFromPipelineByPropertyName = $true)]
    [ValidateScript({
        if(!(Test-Path -LiteralPath $_ -PathType Container))
        {
            throw "Folder doesn't exist: $_"
        }
        $true
    })]
    [ValidateNotNullOrEmpty()]
    [string]$Path,

    [Parameter(ValueFromPipelineByPropertyName = $true)]
    [ValidateNotNullOrEmpty()]
    [string]$User,

    [Parameter(ValueFromPipelineByPropertyName = $true)]
    [string]$Password

)

# Get script path, to save CSV's, if not specified
if(!$Path)
{
    if($psISE.CurrentFile.FullPath)
    {
        $Path = $psISE.CurrentFile.FullPath | Split-Path
    }
    elseif($script:MyInvocation.MyCommand.Path)
    {
        $Path = $script:MyInvocation.MyCommand.Path | Split-Path
    }
    else
    {
        $Path = $PWD.Path
    }

    Write-Verbose "No Path specified, defaulting to: $Path"
}

# Get user credentials, if needed
if($User)
{
    Write-Verbose "User specified: $User"
    if($Password)
    {
        Write-Verbose 'Password specified, converting to credentials object'
        $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
        $Credentials =  New-Object System.Management.Automation.PSCredential -ArgumentList $User, $SecurePassword
    }
    else
    {
        Write-Verbose 'Password not specified, requesting from user.'
        $Credentials = Get-Credential -UserName $User -Message "Enter password for user: $User" -ErrorAction Stop
        if(!$Credentials)
        {
            Write-Verbose 'User cancelled password request'
        }
    }
}

# https://mnaoumov.wordpress.com/2014/05/15/task-scheduler-event-ids/
$TaskStartId = 100
$TaskFinishId = 102
$FilterXml = @"
<QueryList>
  <Query Id="0" Path="Microsoft-Windows-TaskScheduler/Operational">
    <Select Path="Microsoft-Windows-TaskScheduler/Operational">*[System[(EventID=$TaskStartId or EventID=$TaskFinishId)]]</Select>
  </Query>
</QueryList>
"@

# Hashtable to hold results
$Result = @{}

# Loop through computers
foreach ($PC in $Computers){

    # Grab the events from a PC
    $Params = @{
        ComputerName = $PC
        FilterXml = $FilterXml
        MaxEvents = $MaxEvents
    }

    if($Credentials)
    {
        $Params += @{Credential = $Credentials}
    }

    Write-Verbose "Trying to get Task Scheduler's event log. Computer: $PC"
    try
    {
        $Events = Get-WinEvent @Params -ErrorAction Stop
        Write-Verbose "Success"
    }
    catch
    {
        Write-Error "Can't access Task Scheduler's event log. Computer: $PC"
        continue
    }

    if(!$Events)
    {
        Write-Error "Task Scheduler's event log is empty! Computer: $PC"
        continue
    }

    Write-Verbose 'Extracting additional data from events'
    $Events |
        ForEach-Object {
            # Hashtable for new properties
            $Properties = @{}

            # Convert the event to XML and iterate through each one       
            # of the XML message properties to extract additional data  
            ([xml]$_.ToXml()).Event.EventData.Data |
                ForEach-Object {
                    $Properties.Add($_.name, $_.'#text')
                }

            # Add extracted properties to the event object
            $_ | Add-Member -NotePropertyMembers $Properties
        }

    # Set default start\finish date for event in case
    # it's still running or was started before $MaxEvents
    $DefaultStartDate = $Events[-1].TimeCreated
    $DefaultFinishDate = Get-Date
    Write-Verbose "Default task start date: $DefaultStartDate"
    Write-Verbose "Default task finish date: $DefaultFinishDate"

    Write-Verbose 'Processing events...'
    # Group events by ID and process them
    $PcEvents = $Events |
        Group-Object -Property InstanceId |
            ForEach-Object {
                # Get Name and start\finish Date
                $TaskName = $_.Group[0].TaskName
                $StartDate = ($_.Group | Where-Object {$_.OpcodeDisplayName -eq 'Start'}).TimeCreated
                $FinishDate = ($_.Group | Where-Object {$_.OpcodeDisplayName -eq 'Stop'}).TimeCreated

                # If we can't get dates, set them to defaults
                if(!$StartDate)
                {
                    $StartDate = $DefaultStartDate
                }
                elseif(!$FinishDate)
                {
                    $FinishDate = $DefaultFinishDate
                }

                # Hashtable holding object's properties
                $ItemProp = @{
                    Name = $TaskName
                    StartDate = $StartDate
                    FinishDate = $FinishDate
                    Duration = $FinishDate - $StartDate
                }

                # Output new object to the pipeline
                New-Object psobject -Property $ItemProp |
                    Select-Object Name, StartDate, FinishDate, Duration
        }

    # Add data to results
    $Result += @{$PC = $PcEvents}
}


# Loop through results
$Result.GetEnumerator() |
    ForEach-Object {
        # Export results to CSV, one file per computer
        $CsvPath = Join-Path -Path $Path -ChildPath ($_.Key + '_TaskScheduler.csv')
        Write-Verbose "Saving data to CSV: $CsvPath"
        $_.Value | Export-Csv -LiteralPath $CsvPath -Force -NoTypeInformation
    }

更新(1):我已经添加了以不同用户身份进行身份验证的功能(Username \ Password参数),并切换为使用XML进行过滤,这更快,并且应允许在Vista \ Server 2008 PC上运行此脚本(避免了该bug)。此外,PowerShell 2.0现在兼容。

更新(2):我已经调整了脚本的路径检测,因此现在在Powershell ISE中应该不会损坏。另外,我发现在某些PC上,“任务计划程序”日志已禁用。这是验证已启用日志记录的方法:

  1. 检查是否已All Tasks History启用。它应该显示为Disable All Tasks History(ugh):

所有任务历史记录

  1. 检查是否Operational启用了任务计划程序的事件日志。打开:

    Event Viewer→交通Applications and Services Log→交通Microsoft→交通Windows→交通Task Scheduler→交通Operational→交通右键单击它,(或转到右侧窗格中)Properties

任务计划程序操作事件日志

任务计划程序操作事件日志属性

更新(3):修复了丢失或不可用的事件日志的处理,添加了一堆Verbose消息。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何运行预制的Windows Task Scheduler Task?

来自分类Dev

Windows Task Scheduler事件ID

来自分类Dev

Scrapyd vs Windows Task Scheduler

来自分类Dev

Windows Server上的Laravel Task Scheduler

来自分类Dev

Windows Task Scheduler无法运行VBScript

来自分类Dev

通过Windows Task Scheduler运行Powershell脚本

来自分类Dev

Windows Task Scheduler Winlogon事件-注销不足(?)

来自分类Dev

在Windows Task Scheduler上进行计划

来自分类Dev

Windows Task Scheduler上的文件更改

来自分类Dev

Windows Task Scheduler的“模拟”触发器

来自分类Dev

自定义Windows Task Scheduler

来自分类Dev

使Windows Task Scheduler在失败时提醒我

来自分类Dev

Windows .NET上的Task Scheduler的问题

来自分类Dev

使用Windows Task Scheduler解锁PC

来自分类Dev

Windows Server上的Laravel Task Scheduler

来自分类Dev

从Windows Task Scheduler自动运行curl

来自分类Dev

在Windows Task Scheduler(Windows 7)中运行Powershell脚本

来自分类Dev

Linux替代Windows Task Scheduler或其他方式

来自分类Dev

在哪里可以找到Windows Task Scheduler退出代码列表?

来自分类Dev

如何通过Windows Task Scheduler执行Postgresql过程

来自分类Dev

Windows Task Scheduler中带有$ _GET变量的PHP脚本

来自分类Dev

如何使用Windows Task Scheduler设置XLSM文件的定期计划

来自分类Dev

PowerShell脚本无法从Windows Task Scheduler中正常运行

来自分类Dev

使用Process.Start()和Windows Task Scheduler的异常

来自分类Dev

仅在Windows Task Scheduler打开文件时运行Excel VBA

来自分类Dev

用于数据迁移的Sitecore计划任务与Windows Task Scheduler

来自分类Dev

使用Windows Task Scheduler自动执行WAR文件

来自分类Dev

Windows Task Scheduler从睡眠中唤醒并播放视频

来自分类Dev

Linux替代Windows Task Scheduler或其他方式

Related 相关文章

  1. 1

    如何运行预制的Windows Task Scheduler Task?

  2. 2

    Windows Task Scheduler事件ID

  3. 3

    Scrapyd vs Windows Task Scheduler

  4. 4

    Windows Server上的Laravel Task Scheduler

  5. 5

    Windows Task Scheduler无法运行VBScript

  6. 6

    通过Windows Task Scheduler运行Powershell脚本

  7. 7

    Windows Task Scheduler Winlogon事件-注销不足(?)

  8. 8

    在Windows Task Scheduler上进行计划

  9. 9

    Windows Task Scheduler上的文件更改

  10. 10

    Windows Task Scheduler的“模拟”触发器

  11. 11

    自定义Windows Task Scheduler

  12. 12

    使Windows Task Scheduler在失败时提醒我

  13. 13

    Windows .NET上的Task Scheduler的问题

  14. 14

    使用Windows Task Scheduler解锁PC

  15. 15

    Windows Server上的Laravel Task Scheduler

  16. 16

    从Windows Task Scheduler自动运行curl

  17. 17

    在Windows Task Scheduler(Windows 7)中运行Powershell脚本

  18. 18

    Linux替代Windows Task Scheduler或其他方式

  19. 19

    在哪里可以找到Windows Task Scheduler退出代码列表?

  20. 20

    如何通过Windows Task Scheduler执行Postgresql过程

  21. 21

    Windows Task Scheduler中带有$ _GET变量的PHP脚本

  22. 22

    如何使用Windows Task Scheduler设置XLSM文件的定期计划

  23. 23

    PowerShell脚本无法从Windows Task Scheduler中正常运行

  24. 24

    使用Process.Start()和Windows Task Scheduler的异常

  25. 25

    仅在Windows Task Scheduler打开文件时运行Excel VBA

  26. 26

    用于数据迁移的Sitecore计划任务与Windows Task Scheduler

  27. 27

    使用Windows Task Scheduler自动执行WAR文件

  28. 28

    Windows Task Scheduler从睡眠中唤醒并播放视频

  29. 29

    Linux替代Windows Task Scheduler或其他方式

热门标签

归档