-2

Connection request was completed successfully.

DHCP is already enabled on this interface.

I have a batch script to configure the network adapter. I can set it to static with specific IP or to DHCP. How would I check if the adapter is already set to static or DHCP with a batch script?

Community
  • 1
  • 1
  • 1
    You use the same command that you are already using to make those settings. It is just a different option with that command. Read the help file. Show us your existing code. – Squashman Nov 29 '18 at 14:38

1 Answers1

0

You can get the "DHCP enabled" status of a network adapter by running the following command in your batch file:

FOR /F "tokens=2 delims=:" %%a IN ('netsh interface ip show addresses "Local Area Connection" ^| FIND "DHCP enabled"') DO ECHO %%a

where "Local Area Connection" is the name of the interface you're trying to set.

The options will be either "Yes" or "No", so you can query the status by using something like this:

SET _DHCP=FALSE
FOR /F "tokens=2 delims=:" %%a IN ('netsh interface ip show addresses "Local Area Connection" ^| FIND "DHCP enabled"') DO SET _DHCP=%%a

IF "%_DHCP%"=="FALSE" (
    ECHO DHCP was not found for this interface. Please check the interface name.
) ELSE IF "%_DHCP%"=="Yes" (
    ECHO DHCP is enabled
) ELSE (
    ECHO DHCP is not enabled
)

This will query the DHCP status into an environment variable called _DHCP. You will want to set _DHCP to something such as FALSE or NULL before you query the status so that you will be able to know if the query failed.

Worthwelle
  • 1,244
  • 1
  • 16
  • 19
  • 1
    Just a note: the find string and the output are language dependent. On a German system `netsh` response would be ` DHCP aktiviert: Ja`. Also the interface name will vary from system to system (even in the same language) – Stephan Nov 29 '18 at 18:43
  • Thank you very much, just what I needed. – Eirik H.Wean Nov 30 '18 at 08:35