Netsh 命令语法、上下文和格式设置Netsh Command Syntax, Contexts, and Formatting

适用于:Windows 服务器 (半年频道),Windows Server 2016Applies to: Windows Server (Semi-Annual Channel), Windows Server 2016

可以使用本主题以了解如何输入 netsh 上下文和子上下文,了解 netsh 语法和命令格式设置,以及如何在本地和远程计算机上运行的 netsh 命令。You can use this topic to learn how to enter netsh contexts and subcontexts, understand netsh syntax and command formatting, and how to run netsh commands on local and remote computers.

Netsh 是计算机的命令行脚本实用工具,可用于显示或修改当前正在运行的网络配置。Netsh is a command-line scripting utility that allows you to display or modify the network configuration of a computer that is currently running. 可以通过在 netsh 提示符下键入命令运行 Netsh 命令和他们可以在批处理文件或脚本中使用。Netsh commands can be run by typing commands at the netsh prompt and they can be used in batch files or scripts. 可以使用 netsh 命令配置远程计算机和本地计算机。Remote computers and the local computer can be configured by using netsh commands.

Netsh 还提供脚本功能,可让你在批处理模式下对指定的计算机运行一组命令。Netsh also provides a scripting feature that allows you to run a group of commands in batch mode against a specified computer. 你可以使用 Netsh 将配置脚本保存在文本文件中,以便存档或者帮助你配置其他计算机。With netsh, you can save a configuration script in a text file for archival purposes or to help you configure other computers.

Netsh 上下文Netsh contexts

Netsh 与其他操作系统组件交互使用动态-链接库(DLL)文件。Netsh interacts with other operating system components by using dynamic-link library (DLL) files.

每个 netsh 帮助程序 DLL 提供了一组广泛的功能称为上下文,这是一组特定于网络的服务器角色或功能的命令。Each netsh helper DLL provides an extensive set of features called a context, which is a group of commands specific to a networking server role or feature. 这些上下文提供的配置和监视对一个或多个服务、 实用工具或协议的支持扩展 netsh 的功能。These contexts extend the functionality of netsh by providing configuration and monitoring support for one or more services, utilities, or protocols. 例如,Dhcpmon.dll 提供 netsh 使用上下文和组的配置和管理 DHCP 服务器所需的命令。For example, Dhcpmon.dll provides netsh with the context and set of commands necessary to configure and manage DHCP servers.

获取上下文的列表Obtain a list of contexts

可以通过运行 Windows Server 2016 或 Windows 10 的计算机上打开命令提示符或 Windows PowerShell 获取 netsh 上下文的列表。You can obtain a list of netsh contexts by opening either command prompt or Windows PowerShell on a computer running Windows Server 2016 or Windows 10. 键入命令netsh然后按 ENTER。Type the command netsh and press ENTER. 类型 /? ,然后按 ENTER。Type /?, and then press ENTER.

下面是运行 Windows Server 2016 Datacenter 的计算机上的这些命令的示例输出。Following is example output for these commands on a computer running Windows Server 2016 Datacenter.

PS C:\Windows\system32> netsh
netsh>/?

The following commands are available:

Commands in this context:
..            - Goes up one context level.
?             - Displays a list of commands.
abort         - Discards changes made while in offline mode.
add           - Adds a configuration entry to a list of entries.
advfirewall   - Changes to the `netsh advfirewall' context.
alias         - Adds an alias.
branchcache   - Changes to the `netsh branchcache' context.
bridge        - Changes to the `netsh bridge' context.
bye           - Exits the program.
commit        - Commits changes made while in offline mode.
delete        - Deletes a configuration entry from a list of entries.
dhcpclient    - Changes to the `netsh dhcpclient' context.
dnsclient     - Changes to the `netsh dnsclient' context.
dump          - Displays a configuration script.
exec          - Runs a script file.
exit          - Exits the program.
firewall      - Changes to the `netsh firewall' context.
help          - Displays a list of commands.
http          - Changes to the `netsh http' context.
interface     - Changes to the `netsh interface' context.
ipsec         - Changes to the `netsh ipsec' context.
ipsecdosprotection - Changes to the `netsh ipsecdosprotection' context.
lan           - Changes to the `netsh lan' context.
namespace     - Changes to the `netsh namespace' context.
netio         - Changes to the `netsh netio' context.
offline       - Sets the current mode to offline.
online        - Sets the current mode to online.
popd          - Pops a context from the stack.
pushd         - Pushes current context on stack.
quit          - Exits the program.
ras           - Changes to the `netsh ras' context.
rpc           - Changes to the `netsh rpc' context.
set           - Updates configuration settings.
show          - Displays information.
trace         - Changes to the `netsh trace' context.
unalias       - Deletes an alias.
wfp           - Changes to the `netsh wfp' context.
winhttp       - Changes to the `netsh winhttp' context.
winsock       - Changes to the `netsh winsock' context.

The following sub-contexts are available:
 advfirewall branchcache bridge dhcpclient dnsclient firewall http interface ipsec ipsecdosprotection lan namespace netio ras rpc trace wfp winhttp winsock

To view help for a command, type the command, followed by a space, and then type ?.

子上下文Subcontexts

Netsh 上下文可以包含命令和其他上下文中,调用子上下文的局部Netsh contexts can contain both commands and additional contexts, called subcontexts. 例如,在路由上下文中,您可以更改到 IP 和 IPv6 子上下文。For example, within the Routing context, you can change to the IP and IPv6 subcontexts.

若要显示的命令和在上下文中,在 netsh 提示符下,可以使用的子上下文列表键入上下文名称,然后键入任一 /?To display a list of commands and subcontexts that you can use within a context, at the netsh prompt, type the context name, and then type either /? 帮助or help. 例如,若要在路由上下文中,在 netsh 提示符下显示的子上下文和可以使用的命令列表(,即netsh> ),键入以下值之一:For example, to display a list of subcontexts and commands that you can use in the Routing context, at the netsh prompt (that is, netsh>), type one of the following:

路由 /?routing /?

路由的帮助routing help

若要另一个上下文中执行任务,而无需更改当前上下文中,键入想要在 netsh 提示符下使用的命令的上下文路径。To perform tasks in another context without changing from your current context, type the context path of the command you want to use at the netsh prompt. 例如,若要将名为 IGMP 上下文中而无需为 IGMP 上下文,在 netsh 提示符下,第一个更改"本地区域连接"接口添加键入:For example, to add an interface named "Local Area Connection" in the IGMP context without first changing to the IGMP context, at the netsh prompt, type:

路由 ip igmp 添加接口"本地连接"startupqueryinterval = 21routing ip igmp add interface "Local Area Connection" startupqueryinterval=21

正在运行的 netsh 命令Running netsh commands

若要运行 netsh 命令,必须首先 netsh 命令提示符下键入netsh然后按 ENTER。To run a netsh command, you must start netsh from the command prompt by typing netsh and then pressing ENTER. 接下来,您可以更改为包含你想要使用的命令的上下文。Next, you can change to the context that contains the command you want to use. 可用的上下文取决于已安装的网络组件。The contexts that are available depend on the networking components that you have installed. 例如,如果键入dhcp在 netsh 提示符下按 ENTER 键,netsh 更改为 DHCP 服务器上下文。For example, if you type dhcp at the netsh prompt and press ENTER, netsh changes to the DHCP server context. 如果您不具备 DHCP 安装,但是,出现以下消息:If you do not have DHCP installed, however, the following message appears:

找不到以下命令: dhcp。The following command was not found: dhcp.

格式化图例Formatting Legend

可以使用以下格式设置图例理解和在 netsh 提示符下或批处理文件或脚本中运行命令时使用正确的 netsh 命令语法。You can use the following formatting legend to interpret and use correct netsh command syntax when you run the command at the netsh prompt or in a batch file or script.

  • 中的文本斜体是键入命令时必须提供的信息。Text in Italic is information that you must supply while you type the command. 例如,如果一条命令有一个名为的参数用户名,必须键入的实际用户名。For example, if a command has a parameter named -UserName, you must type the actual user name.
  • 中的文本加粗是必须严格按照所示键入该命令时键入的信息。Text in Bold is information that you must type exactly as shown while you type the command.
  • 文本跟省略号(...)是可以在命令行中多次重复的参数。Text followed by an ellipsis (...) is a parameter that can be repeated several times in a command line.
  • 方括号之间的文本 [ ] 是可选的一项。Text that is between brackets [ ] is an optional item.
  • 大括号之间的文本 { } 用竖线分隔的选项提供了一组选择从中必须仅选择了一个,如{enable|disable}Text that is between braces { } with choices separated by a pipe provides a set of choices from which you must select only one, such as {enable|disable}.
  • 用 Courier 字体格式的文本是代码或程序输出。Text that is formatted with the Courier font is code or program output.

从命令提示符或 Windows PowerShell 中运行的 Netsh 命令Running Netsh commands from the command prompt or Windows PowerShell

若要启动网络 Shell 并输入 netsh 命令提示符或 Windows PowerShell 中,可以使用以下命令。To start Network Shell and enter netsh at the command prompt or in Windows PowerShell, you can use the following command.

netshnetsh

Netsh 是计算机的命令行脚本实用工具,可用于,本地或远程,显示或修改当前正在运行的网络配置。Netsh is a command-line scripting utility that allows you to, either locally or remotely, display or modify the network configuration of a currently running computer. 使用不带参数, netsh将打开 Netsh.exe 命令提示符(即netsh> )。Used without parameters, netsh opens the Netsh.exe command prompt (that is, netsh>).

语法Syntax

netsh [ -a 别名文件] [ -c   上下文 ] [ -r 远程计算机] [ -u [ DomainName\ ] 用户名 ] [ -p  密码 | * ] [{NetshCommand | -f  ScriptFile}]netsh[ -a AliasFile] [ -c Context ] [-r RemoteComputer] [ -u [ DomainName\ ] UserName ] [ -p Password | *] [{NetshCommand | -f ScriptFile}]

ParametersParameters

-a

可选。Optional. 指定将返回到netsh提示运行之后别名文件Specifies that you are returned to the netsh prompt after running AliasFile.

AliasFile

可选。Optional. 指定包含一个或多个文本文件的名称netsh命令。Specifies the name of the text file that contains one or more netsh commands.

-c

可选。Optional. 指定该 netsh 进入指定netsh上下文。Specifies that netsh enters the specified netsh context.

Context

可选。Optional. 指定netsh你想要输入的上下文。Specifies the netsh context that you want to enter.

-r

可选。Optional. 指定你想要在远程计算机上运行的命令。Specifies that you want the command to run on a remote computer.

重要

当您使用一些 netsh 命令远程在另一台计算机上netsh – r参数,必须在远程计算机上运行远程注册表服务。When you use some netsh commands remotely on another computer with the netsh –r parameter, the Remote Registry service must be running on the remote computer. 如果未运行,Windows 将显示"未找到网络路径"错误消息。If it is not running, Windows displays a “Network Path Not Found” error message.

RemoteComputer

可选。Optional. 指定你想要配置的远程计算机。Specifies the remote computer that you want to configure.

-u

可选。Optional. 指定你想要运行的用户帐户下的 netsh 命令。Specifies that you want to run the netsh command under a user account.

DomainName\\

可选。Optional. 指定的用户帐户所在的域。Specifies the domain where the user account is located. 如果,默认值为本地域DomainName\ 未指定。The default is the local domain if DomainName\ is not specified.

UserName

可选。Optional. 指定的用户帐户名称。Specifies the user account name.

-p

可选。Optional. 指定你想要为用户帐户提供密码。Specifies that you want to provide a password for the user account.

Password

可选。Optional. 指定使用指定的用户帐户的密码 -u 用户名Specifies the password for the user account that you specified with -u UserName.

NetshCommand

可选。Optional. 指定netsh你想要运行的命令。Specifies the netsh command that you want to run.

-f

可选。Optional. 退出netsh运行与指定的脚本后ScriptFileExits netsh after running the script that you designate with ScriptFile.

ScriptFile

可选。Optional. 指定你想要运行的脚本。Specifies the script that you want to run.

/?

可选。Optional. 在 netsh 提示符下显示帮助。Displays help at the netsh prompt.

备注

如果指定 -r 另一个命令后, 跟netsh远程计算机上运行该命令,然后返回到 Cmd.exe 命令提示符下。If you specify -r followed by another command, netsh runs the command on the remote computer and then returns to the Cmd.exe command prompt. 如果指定 -r 而无需另一个命令netsh在远程模式下打开。If you specify -r without another command, netsh opens in remote mode. 该过程是类似于使用集机的 Netsh 命令提示符处。The process is similar to using set machine at the Netsh command prompt. 当你使用 -r ,设置的当前实例的目标计算机netsh仅。When you use -r, you set the target computer for the current instance of netsh only. 退出并重新输入后netsh,目标计算机重置为本地计算机。After you exit and reenter netsh, the target computer is reset as the local computer. 你可以运行netsh通过指定计算机的远程计算机上的命令名称在 WINS 中存储、 UNC 名称、 Internet 名称解析由 DNS 服务器或 IP 地址。You can run netsh commands on a remote computer by specifying a computer name stored in WINS, a UNC name, an Internet name to be resolved by the DNS server, or an IP address.

键入 netsh 命令的参数字符串值Typing parameter string values for netsh commands

在 Netsh 命令参考整个有包含一个字符串值是必需的参数的命令。Throughout the Netsh command reference there are commands that contain parameters for which a string value is required.

在其中一个字符串值之间包含空格字符,如包含多个单词的字符串值的情况下也需要将字符串值括在引号中。In the case where a string value contains spaces between characters, such as string values that consist of more than one word, it is required that you enclose the string value in quotation marks. 例如,对于名为的参数接口字符串值为无线网络连接,字符串值两边使用引号:For example, for a parameter named interface with a string value of Wireless Network Connection, use quotation marks around the string value:

interface="Wireless Network Connection"