Batch script to enable a disabled network adapter with different names - batch-file

I'm writing a script to revert the network settings back to default which is to switch back to DHCP on LAN and re-enable the wireless adapter. All these operations were done under the local admin rights privileges for all devices that I'm managing.
While the switching over to DHCP from static works, the script was unable to re-enable the wireless adapter and showing "No more data is available". I had did a bit of troubleshooting and found out that the disabled wireless adapter is not reflected in the ipconfig /all command which I have been using it to obtain the adapter name (e.g Wi-Fi, Wi-Fi 2, etc.) thus the netsh command gave the error due to the varible being invalid.
Here is the part of the code that's responsible for the re-enabling the wireless adapter back. I'm aware that if I were to enter the netsh interface set interface name="Wi-Fi" admin=enabled manually it works based on the adapter name that's taken on my test device. But however I can't hard-code the adapter name as it may not be the same for different devices. Do share if there's any other code for the first line that I might not be not aware of as I did many googling and it only came up with either for the ipconfig or wmic command. Thanks!
::Look for the Wi-Fi adapter name
for /f "tokens=* delims=:" %%b in ('ipconfig ^| find /i "Wireless LAN adapter Wi-Fi"') do set wifiadaptername=%%b
set wifiadaptername=%wifiadaptername:~21%
set wifiadaptername=%wifiadaptername:~0,-1%
::Switch on Wi-Fi
netsh interface set interface name="%wifiadaptername%" admin=enabled

Related

netsh set IP address adaptor name conflict

I use the following netsh command when setting the IP address of the LAN Adaptor within a batch file.
netsh int ipv4 set address name="Local Area Connection" source=static address=10.000.0.1 mask=255.255.255.0 gateway=none
I have recently encountered an issue where the batch-file was run on a different system and this has failed to work (returning Element Not Found), I found the cause of the issue to be the adaptor was labelled 'Ethernet'.
This works fine providing the adaptor is labelled as 'Local Area Connection' and the easy fix is to rename the adaptor.
However i would like a solution where it isnt required to rename and the command works on any system irrelevant of how the LAN Adaptor is labelled on that system.
Is this possible?

How to force DHCP on all phyiscal ports, including Disconnected Media ports, with WMIC

Could anyone help me force all network adapters to be DHCP whether connected or disconnected.
I am relatively new and very novice in batch and I have been working on something to set fixed ipv4 details based on variable Ethernet adapter names. It has been cobbled together from other people's solutions to various problems on here. I am sure there are much more eloquent and elegant ways of doing this and happy to be pointed that way but I would like to try and figure this out.
The idea was to have a call batch script in the startup folder of a OOBE windows machine. Search the connected adapter, irrelevant of the name of said port, set the IP address as static and rest of the details were fixed. I wrote the batch in such a way as to create various IP profiles using variables to create the profile to set the fixed details i.e. a batch to write a batch
As an exercise I was thinking if I were to move the machine and plug in a different adapter on the same machine. How would I have a disconnected adapter with the IP address I want to use switch to DHCP. This would free up the address to search for connected adapter as per the rest of the script. This is where the script fails. I can have the connected adapter switch tp DHCP, disable and enable correctly.
WMIC NICConfig Where "IPEnabled='True' and DHCPEnabled='FALSE'" Call EnableDHCP
this works but only on the connected adapter.
But I am trying to flush out the IP address on the disconnected adapter to use on the connected adapter. So far I've tried
WMIC NICConfig Where NetConnectionID=7 Call EnableDHCP
returns a "Description = Invalid query"
WMIC NICConfig Where "PhysicalAdapter='True'" Call EnableDHCP
returns a "Description = Invalid query"
WMIC NICConfig Where "DHCPEnabled='FALSE'" Call EnableDHCP
returns {ReturnValue = 84;}; I thought this would search for adapters that were not DHCP and change to to be DHCP but I think the adapter has to be connected.
When I use
WMIC NICConfig Where "PhysicalAdapter='True'" Call /?
EnableDHCP looks to be an option but gives a "Description = Invalid query"
Is there a way to do what I am trying to do?
I can also post the entire script if anyone is interested in it but will leave it off until requested. Thanks in advance for any help.
I figured this out.
I was going about it the complete wrong way.
I was trying to clear all adapters fixed ip addresses by "forcing" the physical adapter to DCHP weather connected or disconnected.
But if I have had said
netsh int ip reset
wmic path win32_networkadapter where PhysicalAdapter=True call disable
wmic path win32_networkadapter where PhysicalAdapter=True call enable
This achieves what I want. This clears the IP, subnet and gateway. Sometimes the solution is much easier than expected.
Is there a way to clear the DNS information too for all physical adapters, without knowing their names, so that if IPV4 properties is open no information would be there?

Connect to an ad-hoc-network in windows 10 only via command line

I am currently writing a .bat script in which you can choose whether you want to create an ad-hoc-network or connect to one, to make it easy for everyone to connect within windows 8, 8.1 and 10 as the GUI setup was removed.
Well, creating was easy.
netsh wlan set hostednetwork mode=allow ssid=adhocnetwork key= password
netsh wlan start hosted network
pause
But when I want to connect to a network via netsh wlan connect adhocnetwork, it says there is no profile called 'adhocnetwork'.
I figured that you first have to create a profile with the network GUI.
So my question is now if there is a way to create a network profile with the command line, so that I can implement that into the bat file.
Thanks for every answer!
Best greetings,
Bant
Okay, well, here is my workaround.
Go to the network and sharing center (hope that's right, my system is in German)
Manually create wireless network and type in the name, security type and key.
Remove the tag from 'connect automatically'.
Press 'Done'
Now open the command line and type in the following lines:
netsh wlan set profileparameter name=%name% ConnectionType=IBSS
netsh wlan connect name=%name%
I am still hoping for a better solution where you don't need to leave the command shell.

Enable Disable XP WIFi using command line

Help me out to, Enable and Disable WIFI in my XP using Command Line.
Tried with:
netsh interface set interface name="Wireless Network Connection " admin=disabled
but it is showing, More data to display.
Also, "netsh interface show interface"
Does not display any Interface name.
Thanks in Advance.
That's command should work fine. Are you sure it's not disabled? is there really connection available with name Wireless Network Connection?
In your command, try replacing Wireless Network Connection with actual connection name like Local Area Connection
netsh interface set interface name="Local Area Connection" admin=disabled
Also if below command is not displaying then I doubt, you have any network interface configured. Check to make sure that.
netsh interface show interface
You can as well use the below alternative way to enable/disable a specific connection [Quoted from MS Community Answer]
Start elevated Command Prompt.
Get NIC list and index number:
wmic nic get name, index
Enable NIC with index number: (eg: 7)
wmic path win32_networkadapter where index=7 call enable
Disable NIC with index number: (eg: 7)
wmic path win32_networkadapter where index=7 call disable

Can I enable/disable a network connection from the command-line or MS-Dos?

I want to enable/disable a network connection from the command-line in Windows 8.
netsh interface set interface “Local Area Connection” DISABLED
To enable the interface, you'd then run:
netsh interface set interface “Local Area Connection” ENABLED
When I try this, it generates an error message which states that "no more data is available".
This may be enough for you if you are using DHCP.
Disable:
ipconfig /release *
Enable:
ipconfig /renew
This works for me:
netsh interface set interface name="Local Area Connection" admin=DISABLED
Technical Note: There are several names associated with a network adapter on Windows API level, occurring in Struct PIP_ADAPTER_ADDRESSES
PIP_ADAPTER_ADDRESSES::AdapterName
PIP_ADAPTER_ADDRESSES::Description
PIP_ADAPTER_ADDRESSES::FriendlyName
FriendlyName is a unique logical name like "WLAN 3", generated by the system
and the one displayed with ipconfig and required in netsh interface ....
Description is the tag displayed with ipconfig /all

Resources