Solved Batch File Wont Work

Hewlett-packard / G62
July 10, 2016 at 13:15:05
Specs: Windows 10, Pentium T4500/3GB
I am looking for a one click batch file to quickly disconnect or connect to the internet. I use Ethernet. I have no computer training but found the below batch file online. It doesnt work. If you can take a look at it and give me suggestion on what's wrong I would appreciate it. Thanks.

@echo off
netsh interface show interface | find "Connected" > nul
if errorlevel 1 goto #
netsh interface set interface "Realtek PCle FE Family Controller" disabled
goto end
:#
netsh interface set interface "Realtek PCle FE Family Controller" enabled
:end


See More: Batch File Wont Work

Report •


✔ Best Answer
July 11, 2016 at 12:09:58
See: Razor's point number 1. Your data from the "show" never mentioned "Realtek PCle FE Family Controller" or anytning like that (interface hardware/adapter). (My local tests didn't either. that should have "clued" me that your string was wrong). So you just need to change "Realtek PCle FE Family Controller" to "local area connection" in the bartch, like this (tested and working from my desktop):

@echo off & setlocal
set service=local area connection
netsh interface show interface "%service%"
set x=%1
if not defined x (
echo the following input is NOT case sensitive...
set /p x=local area connetion, On / oFF:
)
goto :%x% 2>nul
goto :eof
:on
set x=en
goto :finish
:off
set x=dis
:finish
netsh interface set interface "%service%" %x%
netsh interface show interface "%service%"
pause
::--------- end batch
note that you could substitute en/dis for on/off to shorten the batch by several steps using variable %x% in the command instead of as a label.

message edited by nbrane



#1
July 10, 2016 at 17:26:51
I am wondering if it is because you are not using a "Realtek PCle FE Family Controller". Just in case I am right, while you are waiting for a batch file person it might help them if you provide the information in Device Manager under "Network Adapters". You should have two, one for Ethernet (cable to router) and one for Wifi. Also let us know which one of the two you are using.

EDIT: My hunch was wrong - see #2.

They may have a more general solution,

Always pop back and let us know the outcome - thanks

message edited by Derek


Report •

#2
July 10, 2016 at 18:24:45
Under network adapters device manager has Realtek PCle FE Family Controller and Realtek RTL8191SE 802.11b/g/n WiFi Adapter. I don't use wifi because it's too complicated for me.

Report •

#3
July 10, 2016 at 19:49:33
This command ("netsh") is new to me, but after testing, it seems to me there might be more than one network service connected. My tests showed two live connections for my system. First you need to examine your system to see what's "out there" and what's enabled. The batchfile you posted is not adequate for what you want to do. It will branch to "enable" the realtek only if NO other service is "Connected". If ANY service is "Connected", the realtek will be disabled. Your first mission, (should you decide to accept it) is to run: "netsh interface show interface" from the command-prompt. If there's more than one line of detail, your batch needs to account for those other lines, or specifically ignore them by specifying only the realtek interface. Here's a simple example that will do that, on a "non-toggle" basis, for this (realtek) item only:
Off:
netsh interface set interface "Realtek PCle FE Family Controller" disabled
On:
netsh interface set interface "Realtek PCle FE Family Controller" enabled

This switches it on or off, regardless of its current status. If it's already in the specified state, nothing happens anyway.
I'm guessing you don't care what state it is currently in, you simply want it ON or OFF. If you want to "toggle" it ON/OFF, THEN you need to test for "Connected" and establish the identity of the connection (ie: Ethernet, wifi, etc) before you toggle it to the opposite state. I'll wait (probably forever) to see which scenario you wish to pursue.


Report •

Related Solutions

#4
July 10, 2016 at 23:59:47
Two possibilities, in order of increasing likelihood:

• NETSH doesn't see the interface as "Realtek PCle FE Family Controller." If that's the case, you'll need to match your commands to the output of NETSH INTERFACE SHOW INTERFACE.

• UAC is blocking, since changing the state of your network requires elevation. If that's the case, you'll need to write something to request elevation. There are VBScripts out there to do that.

How To Ask Questions The Smart Way


Report •

#5
July 11, 2016 at 04:09:32
nbrane

I ran the show interface command and it shows
Wireless Network Connection Admin State Enabled State Disconnected
Local Area Connection Admin State Enabled State Connected

I have the nontoggle bats on my desktop now but neither works.

Razor 2.3

I got no UAC notifications. I am running as administrator. My firewall shows "enabled" next to both bats.


Report •

#6
July 11, 2016 at 08:04:24
Neither CMD (what runs a batch file) nor NETSH attempts elevation. You could try your batch file from an already elevated Command Prompt, or setting your UAC to never notify. The latter's something of a security concern, though.

How To Ask Questions The Smart Way


Report •

#7
July 11, 2016 at 12:09:58
✔ Best Answer
See: Razor's point number 1. Your data from the "show" never mentioned "Realtek PCle FE Family Controller" or anytning like that (interface hardware/adapter). (My local tests didn't either. that should have "clued" me that your string was wrong). So you just need to change "Realtek PCle FE Family Controller" to "local area connection" in the bartch, like this (tested and working from my desktop):

@echo off & setlocal
set service=local area connection
netsh interface show interface "%service%"
set x=%1
if not defined x (
echo the following input is NOT case sensitive...
set /p x=local area connetion, On / oFF:
)
goto :%x% 2>nul
goto :eof
:on
set x=en
goto :finish
:off
set x=dis
:finish
netsh interface set interface "%service%" %x%
netsh interface show interface "%service%"
pause
::--------- end batch
note that you could substitute en/dis for on/off to shorten the batch by several steps using variable %x% in the command instead of as a label.

message edited by nbrane


Report •

#8
July 11, 2016 at 15:33:14
When I run the batch file and it stops at on/off, I type in off then it tells me "The requested operation requires elevation (run as administrator)". I checked user accounts and it lists my name and underneath it says "Local Account Administrator". Then the batch file lists the local area connection then it says "Press any key to continue" and the batch file ends and I'm still connected. Please tell me what I'm doing wrong.

Report •

#9
July 11, 2016 at 18:40:30
Did you right click the batch file and select "Run as Administrator"?.

Another way is to make a shortcut to the batch file then right click it, select Properties, then shortcut tab, then Advanced button. There you can set the shortcut to always Run as Administrator.

Always pop back and let us know the outcome - thanks


Report •

#10
July 11, 2016 at 18:45:05
That would be Razor point #2. I'm not well versed in UAC and privelages. You might be able to use the "runas" command, using machine_name\administrator, but you have to manually supply the administrator password. You would have to change this line in the batch:
netsh interface set interface "%service%" %x%
to:
runas /user:machine_name\administrator netsh interface set interface "%service%" %x%
but you will still be prompted for administrator password. Although it looks like you are elevated ("local account administrator"), apparently that's not good enough for this application.

edit: crossed in the mail. I believe Derek's solution is better, but I think you will still be prompted for the administrator password (otherwise, what would be the point?) If the administrator password is set to null, then you only have to hit ENTER.

message edited by nbrane


Report •

#11
July 12, 2016 at 00:27:11
Got it. It's working great. Thanks everyone. I'm putting you all in my will.

Report •


Ask Question