ExPerfwiz.psm1

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#############################################################################################
# DISCLAIMER: #
# #
# THE SAMPLE SCRIPTS ARE NOT SUPPORTED UNDER ANY MICROSOFT STANDARD SUPPORT #
# PROGRAM OR SERVICE. THE SAMPLE SCRIPTS ARE PROVIDED AS IS WITHOUT WARRANTY #
# OF ANY KIND. MICROSOFT FURTHER DISCLAIMS ALL IMPLIED WARRANTIES INCLUDING, WITHOUT #
# LIMITATION, ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR OF FITNESS FOR A PARTICULAR #
# PURPOSE. THE ENTIRE RISK ARISING OUT OF THE USE OR PERFORMANCE OF THE SAMPLE SCRIPTS #
# AND DOCUMENTATION REMAINS WITH YOU. IN NO EVENT SHALL MICROSOFT, ITS AUTHORS, OR #
# ANYONE ELSE INVOLVED IN THE CREATION, PRODUCTION, OR DELIVERY OF THE SCRIPTS BE LIABLE #
# FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS #
# PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) #
# ARISING OUT OF THE USE OF OR INABILITY TO USE THE SAMPLE SCRIPTS OR DOCUMENTATION, #
# EVEN IF MICROSOFT HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES #
#############################################################################################


# ============== Utility Functions ==============

# Writes output to a log file with a time date stamp
Function Write-Logfile {
    [cmdletbinding()]
    Param
    (
        [Parameter(Mandatory = $true)]
        [string]$String
    )

    # Get our log file path
    $LogFile = Join-path $env:LOCALAPPDATA ExPefwiz.log

    # Get the current date
    [string]$date = Get-Date -Format G

    # Build output string
    [string]$logstring = ( "[" + $date + "] - " + $string)

    # Write everything to our log file and the screen
    $logstring | Out-File -FilePath $LogFile -Append
    Write-Verbose  $logstring
}

Function Convert-OnOffBool {
    [cmdletbinding()]
    [OutputType([bool])]
    Param(
        [Parameter(Mandatory = $true)]
        [string]$tocompare
    )

    switch ($tocompare) {
        On { return $true }
        Default { return $false }
    }
}

# Create a scheduled task to run experfwiz
Function New-PerfWizScheduledTask {
    [CmdletBinding()]
    param (

        [String]
        $Name,

        [string]
        $StartTime,

        [string]
        $Server
    )

    # Going to use schtasks.exe for this instead of New-ScheduledTask
    # New-ScheduledTask is not support on 2012R2 it appears; only shows in Win10 or 2016+
    Write-Logfile -String "Creating Scehduled Task $Name"
    # Create the task ... 2>&1 used to redirect schtasks stderr output to stdopt so it can be processed in PS
    $TaskResult = SCHTASKS /Create /S $Server /RU "SYSTEM" /SC DAILY /TN $Name /TR "logman.exe start $name" /ST $starttime /F 2>&1

    # Check if we have an error
    if (![string]::IsNullOrEmpty(($TaskResult | select-string "Error:"))) {
        Write-Logfile -string "[ERROR] - Creating Scheduled Task"
        Write-Logfile -string [string]$TaskResult
        Throw "UNABLE TO CREATE SCHEDULED TASK: $TaskResult"

    }

}

Function Remove-PerfWizScheduledTask {
    [CmdletBinding()]
    param (
        [String]
        $Name,

        [string]
        $Server
    )

    # Remove scheduled task using schtasks
    $taskRemove = SCHTASKS /DELETE /S $server /TN $name /F 2>&1

    # if success record that
    if (![string]::IsNullOrEmpty(($taskRemove | select-string "SUCCESS:"))) {
        Write-Logfile -string "Removed task $name from server $server"
    }
    # Check if we didn't find the task and record that
    elseif (![string]::IsNullOrEmpty(($taskRemove | select-string "The system cannot find the file specified"))) {
        Write-Logfile -string "Task $name not found on server $server"
    }
    # all other cases throw error
    else {
        Write-Logfile -string "[ERROR] - Removing Scheduled Task"
        Write-Logfile -string [string]$TaskResult
        Throw "UNABLE TO REMOVE SCHEDULED TASK: $TaskResult"

    }
}

Function Get-PerfWizScheduledTask {
    [CmdletBinding()]
    param (
        [string]
        $Name,

        [string]
        $Server
    )

    $taskFound = SCHTASKS /QUERY /TN $name /S $Server /FO csv 2>&1

    # If we found a task with that name return True
    if (![string]::IsNullOrEmpty(($taskFound | select-string "Taskname"))) {
        return true
    }
    # if we didn't find a task with that name return false
    elseif (![string]::IsNullOrEmpty(($taskFound | select-string "The system cannot find the file specified."))) {
        return false
    }
    # If we got an error throw
    else {
        Write-Logfile -string "[ERROR] - Getting Scheduled Task"
        Write-Logfile -string [string]$TaskResult
        Throw "UNABLE TO GET SCHEDULED TASK: $TaskResult"
    }
}