Repository navigation
Expand file tree
/
Copy pathProcessSuspendResume.ps1
More file actions
116 lines (98 loc) · 2.48 KB
/
Copy pathProcessSuspendResume.ps1
File metadata and controls
116 lines (98 loc) · 2.48 KB
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
Add-Type -TypeDefinition @"
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
public static class ntdll
{
[DllImport("ntdll.dll", PreserveSig = false)]
public static extern void NtSuspendProcess(IntPtr processHandle);
[DllImport("ntdll.dll", PreserveSig = false, SetLastError = true)]
public static extern void NtResumeProcess(IntPtr processHandle);
}
public static class kernel32
{
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr OpenProcess(ProcessAccessFlags processAccess,bool bInheritHandle,int processId);
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool CloseHandle(IntPtr hObject);
}
[Flags]
public enum ProcessAccessFlags : uint
{
All = 0x001F0FFF,
Terminate = 0x00000001,
CreateThread = 0x00000002,
VirtualMemoryOperation = 0x00000008,
VirtualMemoryRead = 0x00000010,
VirtualMemoryWrite = 0x00000020,
DuplicateHandle = 0x00000040,
CreateProcess = 0x000000080,
SetQuota = 0x00000100,
SetInformation = 0x00000200,
QueryInformation = 0x00000400,
QueryLimitedInformation = 0x00001000,
Synchronize = 0x00100000
}
"@
function Suspend-Process {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true,ValueFromPipeline = $True)]
[ValidateNotNullorEmpty()]
[Alias('ID')]
[string]
$Name
)
$isPID = $true
$ProcessID = $null
try {
$ProcessID = [int]$Name
}
catch {
$isPID = $false
}
if($isPID)
{
$Process = get-process -Id $ProcessID
}
else {
$Process = get-process -Name $Name
}
if($Process)
{
$handle = [kernel32]::OpenProcess("ALL",$false,($Process.Id))
[ntdll]::NtSuspendProcess($handle)
[kernel32]::CloseHandle($handle)
}
}
function Resume-Process {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true,ValueFromPipeline = $True)]
[ValidateNotNullorEmpty()]
[Alias('ID')]
[string]
$Name
)
$isPID = $true
$ProcessID = $null
try {
$ProcessID = [int]$Name
}
catch {
$isPID = $false
}
if($isPID)
{
$Process = get-process -Id $ProcessID
}
else {
$Process = get-process -Name $Name
}
if($Process)
{
$handle = [kernel32]::OpenProcess("ALL",$false,($Process.Id))
[ntdll]::NtResumeProcess($handle)
[kernel32]::CloseHandle($handle)
}
}