📜  批处理脚本-处理

📅  最后修改于: 2020-11-22 17:51:57             🧑  作者: Mango


在本章中,我们将讨论批处理脚本中涉及的各种过程。

查看正在运行的进程列表

在批处理脚本中,TASKLIST命令可用于获取系统中当前正在运行的进程的列表。

句法

TASKLIST [/S system [/U username [/P [password]]]] [/M [module] | /SVC | /V] [/FI filter]
[/FO format] [/NH]
S.No. Options & Description
1.

/S system

Specifies the remote system to connect to

2.

/U

[domain\]user

Specifies the user context under which the command should execute.

3.

/P [password]

Specifies the password for the given user context. Prompts for input if omitted.

4.

/M [module]

Lists all tasks currently using the given exe/dll name. If the module name is not specified all loaded modules are displayed.

5.

/SVC

Displays services hosted in each process.

6.

/V

Displays verbose task information.

7.

/FI filter

Displays a set of tasks that match a given criteria specified by the filter.

8.

/FO format

Specifies the output format. Valid values: “TABLE”, “LIST”, “CSV”.

9.

/NH

Specifies that the “Column Header” should not show in the output. Valid only for “TABLE” and “CSV” formats.

例子

TASKLIST

上面的命令将获取本地系统上运行的所有进程的列表。以下是按原样运行上述命令时呈现的输出快照。从下面的输出中可以看到,不仅您获得了系统上运行的各种进程,还获得了每个进程的内存使用率。

Image Name                    PID       Session Name       Session#     Mem Usage
========================= ========    ================ =========== ============
System Idle Process             0        Services            0              4 K
System                          4        Services            0            272 K
smss.exe                      344        Services            0          1,040 K
csrss.exe                     528        Services            0          3,892 K
csrss.exe                     612        Console             1         41,788 K
wininit.exe                   620        Services            0          3,528 K
winlogon.exe                  648        Console             1          5,884 K
services.exe                  712        Services            0          6,224 K
lsass.exe                     720        Services            0          9,712 K
svchost.exe                   788        Services            0         10,048 K
svchost.exe                   832        Services            0          7,696 K
dwm.exe                       916        Console             1        117,440 K
nvvsvc.exe                    932        Services            0          6,692 K
nvxdsync.exe                  968        Console             1         16,328 K
nvvsvc.exe                    976        Console             1         12,756 K
svchost.exe                  1012        Services            0         21,648 K
svchost.exe                   236        Services            0         33,864 K
svchost.exe                   480        Services            0         11,152 K
svchost.exe                  1028        Services            0         11,104 K
svchost.exe                  1048        Services            0         16,108 K
wlanext.exe                  1220        Services            0         12,560 K
conhost.exe                  1228        Services            0          2,588 K
svchost.exe                  1276        Services            0         13,888 K
svchost.exe                  1420        Services            0         13,488 K
spoolsv.exe                  1556        Services            0          9,340 K
tasklist > process.txt

上面的命令获取任务列表显示的输出,并将其保存到process.txt文件中。

tasklist /fi "memusage gt 40000"

上面的命令只会获取内存大于40MB的进程。以下是可以呈现的示例输出。

Image Name                    PID      Session Name     Session#     Mem Usage
=========================   ======== ================ =========== ============
dwm.exe                        916     Console             1        127,912 K
explorer.exe                  2904     Console             1        125,868 K
ServerManager.exe             1836     Console             1         59,796 K
WINWORD.EXE                   2456     Console             1        144,504 K
chrome.exe                    4892     Console             1        123,232 K
chrome.exe                    4976     Console             1         69,412 K
chrome.exe                    1724     Console             1         76,416 K
chrome.exe                    3992     Console             1         56,156 K
chrome.exe                    1168     Console             1        233,628 K
chrome.exe                     816     Console             1         66,808 K

杀死一个特定的过程

允许运行Microsoft Windows XP Professional,Windows 2003或更高版本的用户通过进程ID(PID)或映像名称从Windows命令行中终止任务。用于此目的的命令是TASKILL命令。

句法

TASKKILL [/S system [/U username [/P [password]]]] { [/FI filter] 
[/PID processid | /IM imagename] } [/T] [/F]
S.No. Options & Description
1.

/S system

Specifies the remote system to connect to

2.

/U

[domain\]user

Specifies the user context under which the command should execute.

3.

/P [password]

Specifies the password for the given user context. Prompts for input if omitted.

4.

/FI

FilterName

Applies a filter to select a set of tasks. Allows “*” to be used. ex. imagename eq acme* See below filters for additional information and examples.

5.

/PID

processID

Specifies the PID of the process to be terminated. Use TaskList to get the PID.

6.

/IM

ImageName

Specifies the image name of the process to be terminated. Wildcard ‘*’ can be used to specify all tasks or image names.

7.

/T

Terminates the specified process and any child processes which were started by it.

8.

/F

Specifies to forcefully terminate the process(es).

例子

taskkill /f /im notepad.exe

上面的命令将杀死打开的记事本任务(如果已打开)。

taskill /pid 9214

上面的命令将杀死一个进程为9214的进程。

开始新流程

DOS脚本还可以完全启动一个新进程。这可以通过使用START命令来实现。

句法

START "title" [/D path] [options] "command" [parameters]

其中

  • title -CMD窗口标题栏的文本(必填。)

  • 路径-起始目录。

  • 命令-要运行的命令,批处理文件或可执行程序。

  • 参数-传递给命令的参数。

S.No. Options & Description
1.

/MIN

Start window Minimized

2.

/MAX

Start window maximized.

3.

/LOW

Use IDLE priority class.

4.

/NORMAL

Use NORMAL priority class.

5.

/ABOVENORMAL

Use ABOVENORMAL priority class.

6.

/BELOWNORMAL

Use BELOWNORMAL priority class.

7.

/HIGH

Use HIGH priority class.

8.

/REALTIME

Use REALTIME priority class.

例子

START "Test Batch Script" /Min test.bat

上面的命令将在新窗口中运行批处理脚本test.bat。窗口将以最小化模式启动,并且标题为“ Test Batch Script”。

START "" "C:\Program Files\Microsoft Office\Winword.exe" "D:\test\TESTA.txt"

上面的命令实际上将在另一个进程中运行Microsoft Word,然后在MS Word中打开文件TESTA.txt。