Detect a broken internet connection using windows batch programming - windows

I'm using an internet connection which goes down after some time. If I repair my connection it works fine. I have to do it always manually. I want to do this task automatically.
I.e. I want to create a windows batch file which is constantly running in the background. When my internet connection is down, it will renew and restart the connection again without any interaction
I want to do this task using a windows batch file. I tried this:
runas /user:local\administrator
ipconfig /release
ipconfig /flushdns
ipconfig /renew
However, I don't know how to detect whether Network connection is down or up?
I'm hoping for quick and positive response.

I made this some time ago. I think that will help you. (but in is spanish)
https://github.com/edgardok/Arregla_Internet/blob/master/ARREGLA.INTERNET.bat
:INICIO
IF EXIST respuesta.tmp DEL respuesta.tmp >nul
PING %DIRECCIONREMOTA% > respuesta.tmp
FIND /C /I "perdidos = 0" respuesta.tmp >nul
GOTO CASO%ERRORLEVEL%
:CASO1
REM ***conexion fallida***
ECHO %date% %time%. NO ESTA HABILITADA LA PLACA DE RED O NO FUNCIONA.
GOTO menu
:CASO0
REM ***conexion OK***
ECHO %date% %time%. Conexion ok.
GOTO menu

You could try and ping a widely used service in your country (or ping google like crazy) to detect internet connectivity. it's not bullet proof, but will let you know when your internet is down (probably).
The result of pinging could be checked by batch/script and according to the result, decide whether you want to ipconfig /renew or not.

The problem you must be trying to address is
Why your network connection is breaking after sometime
and not
How to repair your network connection every now and then
because if you are able to identify the root cause for first problem then you are done.

f 'r sng Wndws r bv, cn tr ths:
fcnfg | grp -c "P ddrss"
Ths wll gv th nmbr f mtchng P drsss - n cs r cnnctn s dwn, 'll hv fwr thn nrmll.
'll nd grp fr ths (s t's nt prt f Wndws), bt t cn b btnd frm ths RL: nxtls.srcfrg.nt

Related

How to query and add (if not listed) a location to Windows 10 Search Index using PowerShell

I have to re-mount removable drives (which require authentication) each time I boot the computer and Windows Indexing keeps removing the removable drives (perhaps because the removable drives are not available when the computer boots). In an ideal world Windows Indexing would keep these locations and just list them as 'Unavailable' (which it sometimes does). However because it doesn't I am interested in executing a script that queries the Windows Indexing locations and if it does not list the removable drives then add them. At the bottom of this thread I pasted the Batch script that I setup to run at boot (via Start Up folder) to search for a specific folder that is available thereafter mounting one of the removable drives.
I have found several examples of how to do this on Windows 7 (links pasted below) but I can't figure out how to do it in Windows 10. The links provided to the DLL (Microsoft.Search.Interop.dll) no longer resolve.
When searching for the latest Windows Search SDK for Windows 10 I was lead to the Windows SDK here:
https://learn.microsoft.com/en-us/windows/win32/search/-search-developers-guide-entry-page
I installed the C++ related portion of the Windows SDK then searched for Microsoft.Search.Interop.dll but I couldn't find it. Perhaps the DLL has changed?
From How to rebuild Windows Search Index by using PowerShell?
Load DLL containing classes & interfaces
Add-Type -path "C:\Temp\SearchIndexSdk\Microsoft.Search.Interop.dll"
#Provides methods for controlling the Search service. This
interface manages settings and objects that affect the search engine
across catalogs.
https://msdn.microsoft.com/en-us/library/bb231485(v=vs.85).aspx
$sm = New-Object Microsoft.Search.Interop.CSearchManagerClass
#Retrieves a catalog by name and creates a new ISearchCatalogManager
object for that catalog.
$catalog = $sm.GetCatalog("SystemIndex")
#Resets the underlying catalog by rebuilding the databases and performing a full indexing.
#https://msdn.microsoft.com/en-us/library/bb266414(v=vs.85).aspx
$catalog.Reset()
From How to add a location to windows 7/8 search index using batch or vbscript?
#Code copied from "Powershell Tackles Windows Desktop Search" http://powertoe.wordpress.com/2010/05/17/powershell-tackles-windows-desktop-search/
#Microsoft.Search.Interop.dll is needed, download from http://www.microsoft.com/en-us/download/details.aspx?id=7388
#Load the dll
Add-Type -path "D:\Unattend\UserFiles\Tools\Microsoft.Search.Interop.dll"
#Create an instance of CSearchManagerClass
$sm = New-Object Microsoft.Search.Interop.CSearchManagerClass
#Next we connect to the SystemIndex catalog
$catalog = $sm.GetCatalog("SystemIndex")
#Get the interface to the scope rule manager
$crawlman = $catalog.GetCrawlScopeManager()
#add scope
$crawlman.AddUserScopeRule("file:///D:*",$true,$false,$null)
$crawlman.SaveAll()
I would add a comment to the existing threads but I am not able to because I don't have reputation of 50 (dumb rule IMO).
Last... I found this site which lists the DLL along with some code but it hasn't been updated in a long time.
https://github.com/FileMeta/WindowsSearchSample
Thanks in advance!
Batch script that runs at boot:
#echo off
echo Windows Search is being restarted to recognize the Z drive
:while
if EXIST Z:\Watch (
I WANT TO CALL POWERSHELL SCRIPT TO ADD THE LOCATION TO THE INDEX IF NEEDED HERE
sc stop WMPNetworkSvc
ping 127.0.0.1 -n 5 > nul
sc stop WSearch
ping 127.0.0.1 -n 5 > nul
sc start WSearch
ping 127.0.0.1 -n 5 > nul
sc start WMPNetworkSvc
echo Exiting this script in 5 seconds
ping 127.0.0.1 -n 5 > nul
exit
) else (
echo Waiting 60 seconds to check if Z drive is available
ping 127.0.0.1 -n 60 > nul
goto :while
)
When I do a search for Searchdll in what I believe to be the folder where the Windows SDK installed to (C:\Program Files (x86)\Windows Kits\10) I find the following. If I had to guess which DLL is the Windows 10 equivalent of Windows 7's Microsoft.Search.Interop.dll I would guess that it's the 1st one i.e. interop.searchapi.dll.
Add-Type -Path "C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\x64\interop.searchapi.dll" does return without error... however $sm = New-Object Microsoft.Search.Interop.CSearchManagerClass returns with error that it cannot find the class in the assembly.
When I cd to "C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\x64" and enter ([appdomain]::currentdomain.GetAssemblies() | Where-Object Location -Match 'interop.searchapi').gettypes() I get the following
When I enter (([appdomain]::currentdomain.GetAssemblies() | Where-Object location -match 'interop.searchapi.dll').gettypes() | Where-Object name -eq 'CSearchManagerClass').getmembers() | Format-Table name, membertype I get
From the list of commands in the previous threads I do see GetCatalog and I presume that the members GetCrawlScopeManager, AddUserScopeRule, Reset, and SaveAll exist.
I don't know how to find the fully qualified class name or I'm doing something else wrong (unknowingly).
When I enter ([appdomain]::currentdomain.GetAssemblies() | Where-Object Location -Match 'interop.searchapi').fullname I get the following
Interop.SearchAPI, Version=10.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
But when I enter $sm = New-Object Interop.SearchAPI.CSearchManagerClass I get an error that it can't find the type Interop.SearchAPI.CSearchManagerClass.

Enable Windows 10 built-in hotspot by cmd/batch/powershell

I'm searching for a way to enable/disable the Hotspot built into Windows 10 via the command prompt, powershell or a batch file. In the GUI, it can be easily done with the third button in the network panel (see image below), but I want to automate it.
I already found some hundred tutorials how to create a new hotspot using netsh, but as I understand it this would create another, different hotspot.
Instead I want to use the already configured one. Or does Windows 10 use the same and creates a new hotspot every time but in between only remembers the settings?
I played around a little bit and discovered the following:
My current WiFi driver doesn't support hosted networks. If I enter netsh wlan show drivers it says hosted network supprt: no. So for the 'common' solution I would have to update the driver.
Nevertheless, I can create a HotSpot with the built-in solution (see image).
It seems that if I activate this HotSpot, Windows creates an additional Microsoft Wi-Fi Direct Virtual Adapter #x. As soon I deactivate the HotSpot, the adapter vanishes.
So it seems that MS is using a very different technique for the built-in hotspot than the netsh variant. Which brings me again to the question: how can I automate (by script) the enabling/disabling of this hotspot?
The Hosted Network (which can be configured using the netsh wlan set hostednetwork ... command) and the "new" Mobile Hotspot use different technologies under the hood.
There's a WinRT API to control and configure the "new" mobile hotspot you're referring to. You can call it from PowerShell:
The following code snippet requires Ben N.'s await function for IAsyncOperation and IAsyncAction in PowerShell, which can be found here.
$connectionProfile = [Windows.Networking.Connectivity.NetworkInformation,Windows.Networking.Connectivity,ContentType=WindowsRuntime]::GetInternetConnectionProfile()
$tetheringManager = [Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager,Windows.Networking.NetworkOperators,ContentType=WindowsRuntime]::CreateFromConnectionProfile($connectionProfile)
# Be sure to include Ben N.'s await for IAsyncOperation:
# https://superuser.com/questions/1341997/using-a-uwp-api-namespace-in-powershell
# Check whether Mobile Hotspot is enabled
$tetheringManager.TetheringOperationalState
# Start Mobile Hotspot
Await ($tetheringManager.StartTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])
# Stop Mobile Hotspot
Await ($tetheringManager.StopTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])
The NetworkOperatorTetheringManager class also allows you to set the SSID and the passphrase of your hotspot programmatically.
TL;DR:
If anyone is interested, I've created a batch file using the above solution to turn on the Mobile Hotspot. Simply create a new .bat file and paste the following code inside:
#echo off
powershell.exe -encodedCommand WwBXAGkAbgBkAG8AdwBzAC4AUwB5AHMAdABlAG0ALgBVAHMAZQByAFAAcgBvAGYAaQBsAGUALgBMAG8AYwBrAFMAYwByAGUAZQBuACwAVwBpAG4AZABvAHcAcwAuAFMAeQBzAHQAZQBtAC4AVQBzAGUAcgBQAHIAbwBmAGkAbABlACwAQwBvAG4AdABlAG4AdABUAHkAcABlAD0AVwBpAG4AZABvAHcAcwBSAHUAbgB0AGkAbQBlAF0AIAB8ACAATwB1AHQALQBOAHUAbABsAA0ACgBBAGQAZAAtAFQAeQBwAGUAIAAtAEEAcwBzAGUAbQBiAGwAeQBOAGEAbQBlACAAUwB5AHMAdABlAG0ALgBSAHUAbgB0AGkAbQBlAC4AVwBpAG4AZABvAHcAcwBSAHUAbgB0AGkAbQBlAA0ACgAkAGEAcwBUAGEAcwBrAEcAZQBuAGUAcgBpAGMAIAA9ACAAKABbAFMAeQBzAHQAZQBtAC4AVwBpAG4AZABvAHcAcwBSAHUAbgB0AGkAbQBlAFMAeQBzAHQAZQBtAEUAeAB0AGUAbgBzAGkAbwBuAHMAXQAuAEcAZQB0AE0AZQB0AGgAbwBkAHMAKAApACAAfAAgAD8AIAB7ACAAJABfAC4ATgBhAG0AZQAgAC0AZQBxACAAJwBBAHMAVABhAHMAawAnACAALQBhAG4AZAAgACQAXwAuAEcAZQB0AFAAYQByAGEAbQBlAHQAZQByAHMAKAApAC4AQwBvAHUAbgB0ACAALQBlAHEAIAAxACAALQBhAG4AZAAgACQAXwAuAEcAZQB0AFAAYQByAGEAbQBlAHQAZQByAHMAKAApAFsAMABdAC4AUABhAHIAYQBtAGUAdABlAHIAVAB5AHAAZQAuAE4AYQBtAGUAIAAtAGUAcQAgACcASQBBAHMAeQBuAGMATwBwAGUAcgBhAHQAaQBvAG4A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UAdAB3AG8AcgBrAE8AcABlAHIAYQB0AG8AcgBzAC4ATgBlAHQAdwBvAHIAawBPAHAAZQByAGEAdABvAHIAVABlAHQAaABlAHIAaQBuAGcATwBwAGUAcgBhAHQAaQBvAG4AUgBlAHMAdQBsAHQAXQApAA==
(Although it's trivial to say, I'll remind you that the above code will also work as a CMD command).
Explanation:
What I did is to take the following PowerShell script (given in above answers by Julius Hardt), and convert it to a batch file using this converter:
[Windows.System.UserProfile.LockScreen,Windows.System.UserProfile,ContentType=WindowsRuntime] | Out-Null
Add-Type -AssemblyName System.Runtime.WindowsRuntime
$asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' })[0]
Function Await($WinRtTask, $ResultType) {
$asTask = $asTaskGeneric.MakeGenericMethod($ResultType)
$netTask = $asTask.Invoke($null, #($WinRtTask))
$netTask.Wait(-1) | Out-Null
$netTask.Result
}
Function AwaitAction($WinRtAction) {
$asTask = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and !$_.IsGenericMethod })[0]
$netTask = $asTask.Invoke($null, #($WinRtAction))
$netTask.Wait(-1) | Out-Null
}
$connectionProfile = [Windows.Networking.Connectivity.NetworkInformation,Windows.Networking.Connectivity,ContentType=WindowsRuntime]::GetInternetConnectionProfile()
$tetheringManager = [Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager,Windows.Networking.NetworkOperators,ContentType=WindowsRuntime]::CreateFromConnectionProfile($connectionProfile)
# Be sure to include Ben N.'s await for IAsyncOperation:
# https://superuser.com/questions/1341997/using-a-uwp-api-namespace-in-powershell
# Check whether Mobile Hotspot is enabled
$tetheringManager.TetheringOperationalState
# Start Mobile Hotspot
Await ($tetheringManager.StartTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])
I've also put this batch file inside the startup folder located in "C:\Users\<USERNAME>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup" so the Mobile Hotspot will turn on automatically upon Windows startup.
To toggle the Mobile Hotspot ON\OFF, I've modified the above answer (by Julius Hardt) to this:
[Windows.System.UserProfile.LockScreen,Windows.System.UserProfile,ContentType=WindowsRuntime] | Out-Null
Add-Type -AssemblyName System.Runtime.WindowsRuntime
$asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' })[0]
Function Await($WinRtTask, $ResultType) {
$asTask = $asTaskGeneric.MakeGenericMethod($ResultType)
$netTask = $asTask.Invoke($null, #($WinRtTask))
$netTask.Wait(-1) | Out-Null
$netTask.Result
}
Function AwaitAction($WinRtAction) {
$asTask = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and !$_.IsGenericMethod })[0]
$netTask = $asTask.Invoke($null, #($WinRtAction))
$netTask.Wait(-1) | Out-Null
}
$connectionProfile = [Windows.Networking.Connectivity.NetworkInformation,Windows.Networking.Connectivity,ContentType=WindowsRuntime]::GetInternetConnectionProfile()
$tetheringManager = [Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager,Windows.Networking.NetworkOperators,ContentType=WindowsRuntime]::CreateFromConnectionProfile($connectionProfile)
# Be sure to include Ben N.'s await for IAsyncOperation:
# https://superuser.com/questions/1341997/using-a-uwp-api-namespace-in-powershell
# Check whether Mobile Hotspot is enabled
$tetheringManager.TetheringOperationalState
# If Mobile Hotspot is on - turn it off.
if ($tetheringManager.TetheringOperationalState -eq 1){
# Stop Mobile Hotspot
Await ($tetheringManager.StopTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])
}
# If Mobile Hotspot is off - turn it on.
else{
# Start Mobile Hotspot
Await ($tetheringManager.StartTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])
}
The corresponding batch file:
#echo off
powershell.exe -encodedCommand WwBXAGkAbgBkAG8AdwBzAC4AUwB5AHMAdABlAG0ALgBVAHMAZQByAFAAcgBvAGYAaQBsAGUALgBMAG8AYwBrAFMAYwByAGUAZQBuACwAVwBpAG4AZABvAHcAcwAuAFMAeQBzAHQAZQBtAC4AVQBzAGUAcgBQAHIAbwBmAGkAbABlACwAQwBvAG4AdABlAG4AdABUAHkAcABlAD0AVwBpAG4AZABvAHcAcwBSAHUAbgB0AGkAbQBlAF0AIAB8ACAATwB1AHQALQBOAHUAbABsAA0ACgBBAGQAZAAtAFQAeQBwAGUAIAAtAEEAcwBzAGUAbQBiAGwAeQBOAGEAbQBlACAAUwB5AHMAdABlAG0ALgBSAHUAbgB0AGkAbQBlAC4AVwBpAG4AZABvAHcAcwBSAHUAbgB0AGkAbQBlAA0ACgAkAGEAcwBUAGEAcwBrAEcAZQBuAGUAcgBpAGMAIAA9ACAAKABbAFMAeQBzAHQAZQBtAC4AVwBpAG4AZABvAHcAcwBSAHUAbgB0AGkAbQBlAFMAeQBzAHQAZQBtAEUAeAB0AGUAbgBzAGkAbwBuAHMAXQAuAEcAZQB0AE0AZQB0AGgAbwBkAHMAKAApACAAfAAgAD8AIAB7ACAAJABfAC4ATgBhAG0AZQAgAC0AZQBxACAAJwBBAHMAVABhAHMAawAnACAALQBhAG4AZAAgACQAXwAuAEcAZQB0AFAAYQByAGEAbQBlAHQAZQByAHMAKAApAC4AQwBvAHUAbgB0ACAALQBlAHEAIAAxACAALQBhAG4AZAAgACQAXwAuAEcAZQB0AFAAYQByAGEAbQBlAHQAZQByAHMAKAApAFsAMABdAC4AUABhAHIAYQBtAGUAdABlAHIAVAB5AHAAZQAuAE4AYQBtAGUAIAAtAGUAcQAgACcASQBBAHMAeQBuAGMATwBwAGUAcgBhAHQAaQBvAG4AYAAxACcAIAB9ACkAWwAwAF0ADQA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G8AcAAgAE0AbwBiAGkAbABlACAASABvAHQAcwBwAG8AdAANAAoAIAAgACAAIABBAHcAYQBpAHQAIAAoACQAdABlAHQAaABlAHIAaQBuAGcATQBhAG4AYQBnAGUAcgAuAFMAdABvAHAAVABlAHQAaABlAHIAaQBuAGcAQQBzAHkAbgBjACgAKQApACAAKABbAFcAaQBuAGQAbwB3AHMALgBOAGUAdAB3AG8AcgBrAGkAbgBnAC4ATgBlAHQAdwBvAHIAawBPAHAAZQByAGEAdABvAHIAcwAuAE4AZQB0AHcAbwByAGsATwBwAGUAcgBhAHQAbwByAFQAZQB0AGgAZQByAGkAbgBnAE8AcABlAHIAYQB0AGkAbwBuAFIAZQBzAHUAbAB0AF0AKQANAAoAfQANAAoAIwAgAEkAZgAgAE0AbwBiAGkAbABlACAASABvAHQAcwBwAG8AdAAgAGkAcwAgAG8AZgBmACAALQAgAHQAdQByAG4AIABpAHQAIABvAG4ALgANAAoAZQBsAHMAZQB7AA0ACgAgACAAIAAgACMAIABTAHQAYQByAHQAIABNAG8AYgBpAGwAZQAgAEgAbwB0AHMAcABvAHQADQAKACAAIAAgACAAQQB3AGEAaQB0ACAAKAAkAHQAZQB0AGgAZQByAGkAbgBnAE0AYQBuAGEAZwBlAHIALgBTAHQAYQByAHQAVABlAHQAaABlAHIAaQBuAGcAQQBzAHkAbgBjACgAKQApACAAKABbAFcAaQBuAGQAbwB3AHMALgBOAGUAdAB3AG8AcgBrAGkAbgBnAC4ATgBlAHQAdwBvAHIAawBPAHAAZQByAGEAdABvAHIAcwAuAE4AZQB0AHcAbwByAGsATwBwAGUAcgBhAHQAbwByAFQAZQB0AGgAZQByAGkAbgBnAE8AcABlAHIAYQB0AGkAbwBuAFIAZQBzAHUAbAB0AF0AKQANAAoAfQANAAoADQAKAA==
The Windows 10 mobile hotspot gets started by the Windows Mobile Hotspot Service (icssvc).
Using Powershell:
Get the current state of the service:
get-service "icssvc"
Start the service:
start-service "icssvc"
Stop the service:
stop-service "icssvc"
If you want to configure the hotspot then thats another thing. You can Google "Configure Internet Connection Sharing with PowerShell" to get you started.
Based on the PowerShell comment above,
Here are the WinRT code components for mix/match to accomplish the above tasks for WiFi Direct 2.0 HotSpot activities using C++ without dependencies on other tooling etc (they work on IoT Core, etc):
Programmatically set the SSID and/or PassPhrase
Programmatically query the current WiFi Direct 2.0 HotSpot status
Programmatically turn on/off the WiFi Direct 2.0 HotSpot
#include <winrt/Windows.Networking.Connectivity.h>
#include <winrt/Windows.Networking.NetworkOperators.h>
namespace winrt { // /ZW embed in :<winrt> when `Windows` is ambiguously defined
static void af_winrt_wifi_hotspot_test() {
// start ms-settings:network-mobilehotspot
init_apartment(); // apartment_type::multi_threaded
auto connectionProfile { Windows::Networking::Connectivity::NetworkInformation::GetInternetConnectionProfile() };
auto tetheringManager = Windows::Networking::NetworkOperators::NetworkOperatorTetheringManager::CreateFromConnectionProfile(connectionProfile);
switch(tetheringManager.TetheringOperationalState()) {
case Windows::Networking::NetworkOperators::TetheringOperationalState::Off: {
auto ioAsync = tetheringManager.StartTetheringAsync();
auto fResult = ioAsync.get();
}
break;
case Windows::Networking::NetworkOperators::TetheringOperationalState::On: {
auto ioAsync = tetheringManager.StopTetheringAsync();
auto fResult = ioAsync.get();
}
break;
case Windows::Networking::NetworkOperators::TetheringOperationalState::InTransition:
default:
break;
}
clear_factory_cache();
uninit_apartment();
}
}
I can confirm #smallscript's winRT solution works. Running the script will either open/close hotspot connection depends on the current status (enabled to disabled and vice versa).
To complete his answer I will point out two additional things in order to compile the code:
You must have Windows SDK installed(I used version 10.0.18362.0) to have the winRT library.
You need to link the RuntimeObject.lib library to your project, otherwise you'll get unresolved external symbols (Using VS, Add the name to: Project -> Properties -> Linker -> Input -> Additional Dependencies)
I think the only solution at the moment is to setup an autohotkey script to click the button on start up. Windows +A, Shift Tab, down arrow, etc... I need this for a headless PC to allow wifi remote desktop.
Turn on command - netsh wlan set hostednetwork mode=allow
Turn off command - netsh wlan set hostednetwork mode=disallow
-not tested-
By Using CMD.
It is too Simple to turn PC as Hotspot. But you PC must have Hosted network supported, to check it out try this command
#echo off
REM Copyright (C) 2013
REM user49828
REM
REM Batch file for creating Wifi Hotspot
if _%1_==_payload_ goto :payload
:getadmin
echo %~nx0: elevating self
set vbs=%temp%\getadmin.vbs
echo Set UAC = CreateObject^("Shell.Application"^) >> "%vbs%"
echo UAC.ShellExecute "%~s0", "payload %~sdp0 %*", "", "runas", 1 >> "%vbs%"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
goto :eof
:payload
echo %~nx0: running payload with parameters:
echo %*
echo ---------------------------------------------------
cd /d %2
shift
shift
rem put your code here
rem e.g.: perl myscript.pl %1 %2 %3 %4 %5 %6 %7 %8 %9
:start
#ECHO off
ECHO Pleae select one of the options Programmed By Overflown.com "user49828"
ECHO --------------------------------------------------------------------------------------------------------------------------
ECHO 1 Hotspot settings
ECHO 2 Start Hotspot
ECHO 3 Stop Hotspot
ECHO --------------------------------------------------------------------------------------------------------------------------
SET /p option=Please enter one of the options:
if %option%==1 ( goto setup ) else set /a er=1
if %option%==2 ( goto start1 ) else set /a er=er+1
if %option%==3 ( goto stop ) else set /a er=er+1
:noOption
if %er% GEQ 3 (
Echo Error!
Echo Please enter a correct option
#pause
cls
goto start
)
:setup
SET /p ssid=Please enter the Hotspot name:
SET /p key=Please enter the Hotspot password greater the 8 digits:
netsh wlan set hostednetwork mode=allow ssid=%ssid% key=%key%
if %errorlevel%==0 (
ECHO Setup complete
)
#pause
cls
goto start
:start1
netsh wlan start hostednetwork
#pause
cls
goto start
:stop
netsh wlan stop hostednetwork
#pause
cls
goto start
goto :eof

Remove usb drive using cli, on success remove turn off the windows

I wondering to make a batch file that have purposed like this :
when a usb drive that ejected on my notebook is succesfully unmount, I want to make my windows is shutdown.
So, I use RemoveDrive.
Assumed, I mounted my flash drive to E, I use this command
RemoveDrive.exe E: -L
it gives me message like this :
Removing 'My drive'(E:)
success
Now, I was wondering to make a batch file (.bat), when success, it execute 'shutdown / s'. if failed, it gives me just a message error. How come ?
Amy help it so appreciated.
Edit :
based this web : FAQ
it said :
0 - successfully removed a device
1 - device identified but not removed
2 - device not found or parameters are invalid
4 - RemoveDrive.exe located on the drive to remove -> temporary copy
created and executed
so this is my code so far :
#ECHO OFF
set def="0"
SET /P uname=Enter the drive (letter:):
IF "%uname%"=="" GOTO Error
RemoveDrive.exe "%uname%" -L
if %ERRORLEVEL% == def
shutdwon /s
else
echo 'Something have problem'
GOTO End
:Error
ECHO Please enter your drive's name!
:End
if success or failed, it still gives me the syntax of command is inicnorrect message
As per if command syntax and explanation in Command-Line Reference:
if %ERRORLEVEL% == def (
shutdown /s
) else (
echo 'Something have problem'
)

How to access a ipv6 address using windows command?

I know how to access a ipv4 address using windows command.
Open command line and type
explorer.exe http://173.194.72.103
Here is the question, how to access a ipv6 address?
I tried to do this like ipv4
ping -6 www.google.com
result:Ping www.google.com [2607:f8b0:4006:808::1012]
explorer.exe http://2607:f8b0:4006:808::1012
or
explorer.exe https://2607:f8b0:4006:808::1012
Neither ok, they open "My Computer" panel.
If I put http://2607:f8b0:4006:808::1012 into the address bar of Google Chrome on Windows 7, it doesn't work (it searches for the "phrase" on Google). Likewise for IE. So I got to thinking, maybe your syntax is wrong, and it's not Explorer's fault. And I found this:
https://productforums.google.com/forum/#!topic/chrome/n3jUQROi1cA
Which says you must use brackets. And behold, this works:
explorer.exe http://[2607:f8b0:4006:808::1012]
The grammar is described in painstaking detail here: https://www.rfc-editor.org/rfc/rfc3986#section-3.2.2
The key parts are:
host = IP-literal / IPv4address / reg-name
IP-literal = "[" ( IPv6address / IPvFuture ) "]"

What does /p mean in set /p?

What does /p stand for in set /p=? I know that / enables a switch, and I'm fairly sure that I know /a is for arithmetic. I've heard numerous rumours, some saying /p is for prompt, others stating it stands for print. The only reason I slightly doubt it is prompt is because in many cases it does not ask for a prompt, yet prints on the screen, such as
<nul set /p=This will not generate a new line
But what I want to know is: Do we really know what it stands for?
The /P switch allows you to set the value of a variable to a line of input entered by the user. Displays the specified promptString before reading the line of input. The promptString can be empty.
Two ways I've used it... first:
SET /P variable=
When batch file reaches this point (when left blank) it will halt and wait for user input. Input then becomes variable.
And second:
SET /P variable=<%temp%\filename.txt
Will set variable to contents (the first line) of the txt file. This method won't work unless the /P is included. Both tested on Windows 8.1 Pro, but it's the same on 7 and 10.
For future reference, you can get help for any command by using the /? switch, which should explain what switches do what.
According to the set /? screen, the format for set /p is SET /P variable=[promptString] which would indicate that the p in /p is "prompt." It just prints in your example because <nul passes in a nul character which immediately ends the prompt so it just acts like it's printing. It's still technically prompting for input, it's just immediately receiving it.
NOTE: The answers below this point are for a previous version of the question.
/L in for /L generates a List of numbers.
From ping /?:
Usage: ping [-t] [-a] [-n count] [-l size] [-f] [-i TTL] [-v TOS]
[-r count] [-s count] [[-j host-list] | [-k host-list]]
[-w timeout] [-R] [-S srcaddr] [-4] [-6] target_name
Options:
-t Ping the specified host until stopped.
To see statistics and continue - type Control-Break;
To stop - type Control-C.
-a Resolve addresses to hostnames.
-n count Number of echo requests to send.
-l size Send buffer size.
-f Set Don't Fragment flag in packet (IPv4-only).
-i TTL Time To Live.
-v TOS Type Of Service (IPv4-only. This setting has been deprecated
and has no effect on the type of service field in the IP Header).
-r count Record route for count hops (IPv4-only).
-s count Timestamp for count hops (IPv4-only).
-j host-list Loose source route along host-list (IPv4-only).
-k host-list Strict source route along host-list (IPv4-only).
-w timeout Timeout in milliseconds to wait for each reply.
-R Use routing header to test reverse route also (IPv6-only).
-S srcaddr Source address to use.
-4 Force using IPv4.
-6 Force using IPv6.

Resources