Perfmon/Public/New-PerformanceDataCollectorSet.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
function New-PerformanceDataCollectorSet
{
    [CmdletBinding(DefaultParameterSetName = 'Counter')]
    Param(
        [Parameter(Mandatory)]
        [string]$CollectorSetName,

        [datetime]$StartDate,

        [Parameter(ParameterSetName = 'Counters')]
        [string[]]$Counters,

        [Parameter(ParameterSetName = 'Xml')]
        [string[]]$XmlTemplatePath,
        
        [string]$ComputerName = 'localhost'
    )
    
    if ((Get-PerformanceDataCollectorSet -CollectorSetName $CollectorSetName -ComputerName $ComputerName -ErrorAction SilentlyContinue))
    {
        Write-Error "There is already a data collector set named '$CollectorSetName' on machine '$ComputerName'"
        return
    }

    $collectorSet = New-Object -COM Pla.DataCollectorSet
    if ($XmlTemplatePath)
    {
        if (-not (Test-Path -Path $XmlTemplatePath -PathType Leaf))
        {
            Write-Error "The file '$XmlTemplatePath' could not be found."
            return
        }
        $xml = Get-Content -Path $XmlTemplatePath
        $collectorSet.SetXml($xml)
    }
    else
    {
        $collectorSet.DisplayName = $CollectorSetName
        $collectorSet.Duration = 50400 
        $collectorSet.SubdirectoryFormat = 1 
        $collectorSet.SubdirectoryFormatPattern = 'yyyy\-MM'
        $collectorSet.RootPath = "%systemdrive%\PerfLogs\Admin\$CollectorSetName"

        $collector = $collectorSet.DataCollectors.CreateDataCollector(0) 
        $collector.FileName = $CollectorSetName + '_'
        $collector.FileNameFormat = 0x1
        $collector.FileNameFormatPattern = 'yyyy\-MM\-dd'
        $collector.SampleInterval = 15
        $collector.LogAppend = $true

        if (-not $Counters)
        {
            $Counters = @(
                '\PhysicalDisk\Avg. Disk Sec/Read',
                '\PhysicalDisk\Avg. Disk Sec/Write',
                '\PhysicalDisk\Avg. Disk Queue Length',
                '\Memory\Available MBytes', 
                '\Processor(_Total)\% Processor Time', 
                '\System\Processor Queue Length'
            )
        }

        $collector.PerformanceCounters = $Counters
        $collectorSet.DataCollectors.Add($collector)

        if ($StartDate)
        {
            $newSchedule = $collectorSet.Schedules.CreateSchedule()
            $newSchedule.Days = 127
            $newSchedule.StartDate = $StartDate
            $newSchedule.StartTime = $StartDate
    
            $collectorSet.Schedules.Add($newSchedule)
        }        
    }

    try
    {
        $collectorSet.Commit($CollectorSetName, $ComputerName, 3) | Out-Null #3 = CreateOrModify
    }
    catch
    { 
        Write-Host 'Exception Caught: ' $_.Exception -ForegroundColor Red 
        return 
    }
}