Processes.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
Set-StrictMode -Version Latest

function Test-Process
{
    <#
    .SYNOPSIS
        This function is called after the execution of an external CMD process to log the status of how the process was exited.
    .PARAMETER Process
        A System.Diagnostics.Process object type that is output by using the -Passthru parameter on the Start-Process cmdlet
    #>

    [OutputType([bool])]
    [CmdletBinding()]
    param (
        [Parameter()]
        [System.Diagnostics.Process]$Process
    )
    process
    {
        try
        {
            
            if (@(0, 3010) -notcontains $Process.ExitCode)
            {
                Write-Log -Message "Process ID $($Process.Id) failed. Return value was $($Process.ExitCode)" -LogLevel '2'
                $false
            }
            else
            {
                Write-Log -Message "Process ID $($Process.Id) exited with successfull exit code '$($Process.ExitCode)'."
                $true
            }
            
        }
        catch
        {
            Write-Log -Message "Error: $($_.Exception.Message) - Line Number: $($_.InvocationInfo.ScriptLineNumber)" -LogLevel '3'
            $PSCmdlet.ThrowTerminatingError($_)
        }
    }
}

function Get-ChildProcess
{
    <#
    .SYNOPSIS
        This function childs all child processes a parent process has spawned
    .PARAMETER ProcessId
        The potential parent process ID
    #>

    [OutputType([System.Management.ManagementObject])]
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string]$ProcessId
    )
    process
    {
        try
        {
            Get-WmiObject -Class Win32_Process -Filter "ParentProcessId = '$ProcessId'"
        }
        catch
        {
            Write-Log -Message "Error: $($_.Exception.Message) - Line Number: $($_.InvocationInfo.ScriptLineNumber)" -LogLevel '3'
            $PSCmdlet.ThrowTerminatingError($_)
        }
    }
}

function Stop-MyProcess
{
    <#
    .SYNOPSIS
        This function stops a process while provided robust logging of the activity
    .PARAMETER ProcessName
        One more process names
    #>

    [OutputType()]
    [CmdletBinding(SupportsShouldProcess)]
    param (
        [Parameter(Mandatory = $true)]
        [string[]]$ProcessName
    )
    process
    {
        try
        {
            
            $ProcessesToStop = Get-Process -Name $ProcessName -ErrorAction 'SilentlyContinue'
            if (-not $ProcessesToStop)
            {
                Write-Log -Message "-No processes to be killed found..."
            }
            else
            {
                foreach ($process in $ProcessesToStop)
                {
                    Write-Log -Message "-Process $($process.Name) is running. Attempting to stop..."
                    $WmiProcess = Get-WmiObject -Class Win32_Process -Filter "name='$($process.Name).exe'" -ErrorAction 'SilentlyContinue' -ErrorVariable WMIError
                    if ($WmiError)
                    {
                        throw "process $($process.Name). WMI query errored with `"$($WmiError.Exception.Message)`""
                    }
                    elseif ($WmiProcess)
                    {
                        foreach ($p in $WmiProcess)
                        {
                            if ($PSCmdlet.ShouldProcess("Process ID: $($p.Id)", 'Stop'))
                            {
                                $WmiResult = $p.Terminate()
                                if ($WmiResult.ReturnValue -eq 1603)
                                {
                                    Write-Log -Message "Process $($p.name) exited successfully but needs a reboot."
                                }
                                elseif ($WmiResult.ReturnValue -ne 0)
                                {
                                    throw "-Unable to stop process $($p.name). Return value was $($WmiResult.ReturnValue)"
                                }
                                else
                                {
                                    Write-Log -Message "-Successfully stopped process $($p.Name)..."
                                }
                            }
                        }
                    }
                }
            }
            
        }
        catch
        {
            Write-Log -Message "Error: $($_.Exception.Message) - Line Number: $($_.InvocationInfo.ScriptLineNumber)" -LogLevel '3'
            $PSCmdlet.ThrowTerminatingError($_)
        }
    }
}

function Stop-SoftwareProcess
{
    [OutputType()]
    [CmdletBinding(SupportsShouldProcess)]
    param
    (
        [Parameter(Mandatory = $true,ValueFromPipeline = $true)]
        [ValidateNotNullOrEmpty()]
        [object]$Software
    )
    begin {
        $ErrorActionPreference = 'Stop'
    }
    process {
        try
        {
            $Processes = (Get-Process | Where-Object { $_.Path -like "$($Software.InstallLocation)*" } | Select-Object -ExpandProperty Name)
            if ($Processes)
            {
                Write-Log -Message "Sending processes: $Processes to Stop-MyProcess..."
                ## Check to see if the process is still running. It's possible the termination of other processes
                ## already killed this one.
                $Processes = $Processes | Where-Object { Get-Process -Name $_ -ErrorAction 'SilentlyContinue' }
                if ($PSCmdlet.ShouldProcess("Process ID $($Processes)", 'Stop'))
                {
                    Stop-MyProcess $Processes
                }
            }
            else
            {
                Write-Log -Message 'No processes running under the install folder path'
            }
        }
        catch
        {
            Write-Log -Message "Error: $($_.Exception.Message) - Line Number: $($_.InvocationInfo.ScriptLineNumber)" -LogLevel '3'
            $PSCmdlet.ThrowTerminatingError($_)
        }
    }
}

function Wait-MyProcess
{
    <#
    .SYNOPSIS
        This function waits for a process and waits for all that process's children before releasing control
    .PARAMETER ProcessId
        A process Id
    .PARAMETER ProcessTimeout
        An interval (in seconds) to wait for the process to finish. If the process hasn't exited within this timeout
        it will be terminated. The default is 600 seconds (5 minutes) so no process will run longer than that.
    .PARAMETER ReportInterval
        The number of seconds between when it is logged that the process is still pending
    .PARAMETER SleepInterval
        The number of seconds the process should be checked to ensure it's still running
 
    #>

    [OutputType()]
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [int]$ProcessId,
        
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [int]$ProcessTimeout = 600,
        
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [int]$ReportInterval = 15,
        
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [int]$SleepInterval = 1
        
    )
    process
    {
        try
        {
            Write-Log -Message "Finding the process ID '$ProcessId'..."
            $Process = Get-Process -Id $ProcessId -ErrorAction 'SilentlyContinue'
            if ($Process)
            {
                Write-Log -Message "Process '$($Process.Name)' ($($Process.Id)) found. Waiting to finish and capturing all child processes."

                ## While waiting for the initial process to stop, collect all child IDs it spawns
                $ChildProcessesToLive = @()
                
                ## Start the timer to ensure we have a point to get total time from
                $Timer = [Diagnostics.Stopwatch]::StartNew()
                $i = 0
                
                ## Do this while the parent process is still running
                while (-not $Process.HasExited)
                {
                    ## Find any and all child processes the parent process spawned
                    $ChildProcesses = Get-ChildProcess -ProcessId $ProcessId
                    if ($ChildProcesses)
                    {
                        ## If any child processes are found, collect them all
                        $ChildProcessesToLive += $ChildProcesses
                    }
                    if ($Timer.Elapsed.TotalSeconds -ge $ProcessTimeout)
                    {
                        Write-Log -Message "The process '$($Process.Name)' ($($Process.Id)) has exceeded the timeout of $ProcessTimeout seconds. Killing process."
                        $Timer.Stop()
                        Stop-MyProcess -ProcessName $Process.Name
                    }
                    elseif (($i % $ReportInterval) -eq 0) ## Use a modulus here to write to the log every X seconds
                    {
                        Write-Log "Still waiting for process '$($Process.Name)' ($($Process.Id)) after $([Math]::Round($Timer.Elapsed.TotalSeconds, 0)) seconds"
                    }
                    Start-Sleep -Seconds $SleepInterval
                    $i++
                }
                Write-Log "Process '$($Process.Name)' ($($Process.Id)) has finished after $([Math]::Round($Timer.Elapsed.TotalSeconds, 0)) seconds"
                if ($ChildProcessesToLive) ## If any child processes were spawned while the parent process was running
                {
                    $ChildProcessesToLive = $ChildProcessesToLive | Select-Object -Unique ## Ensure we didn't accidently capture duplicate PIDs
                    Write-Log -Message "Parent process '$($Process.Name)' ($($Process.Id)) has finished but still has $(@($ChildProcessesToLive).Count) child processes ($($ChildProcessesToLive.Name -join ',')) left. Waiting on these to finish."
                    foreach ($Process in $ChildProcessesToLive)
                    {
                        Wait-MyProcess -ProcessId $Process.ProcessId
                    }
                }
                else
                {
                    Write-Log -Message 'No child processes found spawned'
                }
                Write-Log -Message "Finished waiting for process '$($Process.Name)' ($($Process.Id)) and all child processes"
            }
            else
            {
                Write-Log -Message "Process ID '$ProcessId' not found. No need to wait on it."
            }
        }
        catch
        {
            Write-Log -Message "Error: $($_.Exception.Message) - Line Number: $($_.InvocationInfo.ScriptLineNumber)" -LogLevel '3'
            $PSCmdlet.ThrowTerminatingError($_)
        }
    }
}