Condividi tramite


Sintassi, contesti e formattazione dei comandi Netsh

Leggere questo articolo per informazioni su come immettere contesti e sottocontesti Netsh, per acquisire familiarità con la formattazione dei comandi e la sintassi Netsh e per capire come eseguire comandi Netsh in computer locali e remoti.

Netsh è una utilità di scripting da riga di comando che consente di visualizzare o modificare la configurazione di rete di un computer attualmente in esecuzione. I comandi Netsh possono essere eseguiti mediante digitazione alla shell netsh e possono essere usati in script o file batch. I computer remoti e il computer locale possono essere configurati usando i comandi Netsh.

Netsh offre inoltre una funzionalità di scripting che consente di eseguire un gruppo di comandi in modalità batch su un computer specificato. Con Netsh è possibile salvare uno script di configurazione in un file di testo per scopi di archiviazione o per facilitare la configurazione di altri computer.

Syntax

netsh
netsh [-a AliasFile] [-c Context] [-r RemoteMachine] [-u [DomainName\]UserName] [-p Password | *] [Command | -f ScriptFile]

I seguenti parametri sono considerati facoltativi.

Parameter Description
-a Specifies that you're returned to the netsh shell after running AliasFile.
AliasFile Specifies the name of the text file that contains one or more netsh commands.
-c Specifies that netsh enters the specified netsh context.
Context Specifies the netsh context that you want to enter.
-r Specifica che il comando deve essere eseguito in un computer remoto. The Remote Registry service must be running on the remote computer. Se non è in esecuzione, Windows visualizza un messaggio di errore "Percorso di rete non trovato".
RemoteComputer Specifica il nome del computer che intendi configurare.
-u Specifica di eseguire il comando netsh con un account utente.
DomainName\ Specifica il dominio in cui si trova l'account utente. The default is the local domain if DomainName\ isn't specified.
UserName Specifica il nome dell'account utente.
-p Specifica che vuoi fornire una password per l'account utente.
Password Specifies the password for the user account that you specified with -uUserName.
Command Specifies the netsh command that you want to run.
-f Exits netsh after running the script that you designate with ScriptFile.
ScriptFile Specifica lo script da eseguire.

Note

If you specify -r followed by another command, netsh runs the command on the remote computer and then returns to the command prompt. If you specify -r without another command, netsh opens in remote mode. The process is similar to using set machine at the netsh command shell. When you use -r, you set the target computer for the current instance of netsh only. After you exit and reenter netsh, the target computer is reset as the local computer. 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.

Esecuzione dei comandi Netsh

To run a netsh command, you must start netsh from the command prompt or PowerShell by typing netsh and then pressing ENTER. Puoi quindi passare al contesto che contiene il comando che vuoi usare. I contesti disponibili dipendono dai componenti di rete installati.

For example, if you type dhcp in the netsh shell and press ENTER, netsh changes to the DHCP server context. Se non si ha un ruolo server DHCP installato, viene visualizzato il messaggio seguente:

The following command was not found: dhcp.

Netsh contexts

Netsh interagisce con altri componenti del sistema operativo tramite file DLL dynamic-link library (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. I contesti estendono le funzionalità di Netsh fornendo supporto per la configurazione e il monitoraggio per uno o più servizi, utilità o protocolli.

Ad esempio, Dhcpmon.dll fornisce a Netsh il contesto e il set di comandi necessari per configurare e gestire i server DHCP.

È possibile ottenere un elenco di contesti netsh aprendo il prompt dei comandi o Windows PowerShell, quindi digitare netsh /? o netsh help e premere INVIO.

Un output di esempio è il seguente:

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 ?.

Netsh subcontexts

Netsh contexts can contain both commands and additional contexts, called subcontexts. For example, within the interface context, you can change to the IPv4 and IPv6 subcontexts.

To display a list of commands and subcontexts that you can use within a context, at the netsh shell, type the subcontext name, and then type either ? or help. For example:

netsh> interface help

The following commands are available:

Commands in this context:
6to4           - Changes to the `netsh interface 6to4' context.
?              - Displays a list of commands.
dump           - Displays a configuration script.
help           - Displays a list of commands.
httpstunnel    - Changes to the `netsh interface httpstunnel' context.
ipv4           - Changes to the `netsh interface ipv4' context.
ipv6           - Changes to the `netsh interface ipv6' context.
isatap         - Changes to the `netsh interface isatap' context.
portproxy      - Changes to the `netsh interface portproxy' context.
set            - Sets configuration information.
show           - Displays information.
tcp            - Changes to the `netsh interface tcp' context.
teredo         - Changes to the `netsh interface teredo' context.
udp            - Changes to the `netsh interface udp' context.

The following sub-contexts are available:
6to4 httpstunnel ipv4 ipv6 isatap portproxy tcp teredo udp

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

Per eseguire attività in un altro contesto senza cambiare il contesto corrente, digitare il percorso del contesto del comando che si desidera usare nella shell netsh. Ad esempio, per aggiungere un indirizzo IPv4 statico per "NetLAN1" e specificare sia la subnet mask che il gateway, digitare:

netsh interface ipv4 set address "NetLAN1" static <IP_Address> <Subnet_Mask> <Gateway>

Formatting legend

È possibile usare le convenzioni di formattazione seguenti per interpretare e usare la sintassi dei comandi Netsh corretta quando si esegue il comando alla shell netsh oppure in uno script o file batch.

  • 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.
  • Il testo racchiuso tra parentesi quadre [ ] è un elemento facoltativo.
  • Il testo tra parentesi graffe { } con opzioni separate da una barra verticale fornisce un set di opzioni da cui devi selezionarne solo una, ad esempio {enable|disable}.
  • Il testo formattato con il tipo di carattere Courier indica codice oppure output del programma.

Valori stringa per i comandi netsh

Esistono comandi che contengono parametri per i quali è richiesto un valore stringa. Nel caso in cui un valore stringa contenga spazi tra caratteri, è necessario racchiudere il valore stringa tra virgolette doppie.

When setting the interface parameter for IPv4 networks with a string value of Wireless Network Connection to DHCP, use double quotation marks around the string value. For example:

netsh interface ipv4>set address name="Wireless Network Connection" dhcp

In alternativa, per le interfacce di rete, è possibile usare il numero di indice al posto del nome associato. For example:

netsh interface ipv4>set address name=15 dhcp

To find the name or index of the connected interfaces, you must be in the IPv4 or IPv6 interface subcontext and type show interfaces. For example:

netsh interface ipv4>show interfaces
netsh interface ipv6>show interfaces

See also