-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMyProcesses.ps1
67 lines (57 loc) · 1.95 KB
/
MyProcesses.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
# # # # # # # # # # # # # # # # # # # # #
# Credit: http://powershell.com/cs/blogs/tips/archive/2009/12/17/get-process-owners.aspx
function Get-MyProcess {
[cmdletbinding()]
param (
[string]
$ProcessName = '',
[string]
$UserName = $env:USERNAME,
[switch]
$Get_Process
)
if ($null -eq $ProcessName) {
$MyProcess = Get-WmiObject Win32_Process
} else {
$MyProcess = Get-WmiObject Win32_Process -Filter "name='$ProcessName'"
}
Write-Debug -Message "MyProcess Length: $($MyProcess.Length)"
$MyProcess | foreach {
Write-Debug -Message "Add-Member Length: $($PSItem.GetOwner().User)"
Add-Member -MemberType NoteProperty -Name Owner -Value ($PSItem.GetOwner().User) -InputObject $PSItem -PassThru
} |
Where-Object -FilterScript {$PSItem.Owner -eq "$UserName"} |
Select-Object -Property Name, Owner, ProcessId, PSComputerName, SessionId
# Format-Table -Property Name, Owner, ProcessId, PSComputerName, SessionId -AutoSize
if ($Get_Process) {
# pipe WMI object back into Get-Process cmdlet, for standard output
return (Get-Process -Id ($MyProcess.ProcessId))
} else {
return $MyProcess
}
}
function Stop-MyProcess {
[cmdletbinding()]
param
(
[string]
$ProcessName = 'iexplore.exe',
[string]
$UserName = $env:USERNAME,
[switch]
$Confirm
)
$p = Get-MyProcess
<# Get-WmiObject Win32_Process -Filter "name='$ProcessName'" |
foreach {
Add-Member -MemberType NoteProperty -Name Owner -Value ($PSItem.GetOwner().User) -InputObject $PSItem -PassThru
} |
Where-Object -FilterScript {$PSItem.Owner -eq "$UserName"}
#>
if ($Confirm) {
return Stop-Process -Id ($p.ProcessId) -Confirm -PassThru
} else {
return Stop-Process -Id ($p.ProcessId) -PassThru
}
}