Execute batch-file to start wifi hotspot as admin

血红的双手。 提交于 2019-12-13 11:26:49

问题


I'm using windows 10 and I often have to start a wifi hotspot manually from cmd. So, I was thinking maybe I could write some code so that I don't have to write the commands again and again but I have no experience with .bat files.

This is how I start the hotspot:

  1. Open cmd as administrator
  2. netsh wlan set hostednetwork mode=allow ssid=AdHoc key=password
  3. netsh wlan start hostednetwork

回答1:


The hardest part of this is to run a .bat file as admin automatically, without even right-clicking on it. You need to save this code as a .bat file:

@ECHO OFF
:: this tests if the file is running as admin
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' NEQ '0' (GOTO askAdmin)
GOTO gotAdmin
:askAdmin
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
::from this point you can execute your command as admin
netsh wlan set hostednetwork mode=allow ssid=AdHoc key=password
netsh wlan start hostednetwork

Note that this does show the "Run this program as admin?" prompt when started without administrative priviliges, but if you right-click this batch-file and choose run as admin it should immediately execute the command you want it to execute




回答2:


Oh, this is solution for you.

Let create shortcut, every you want turn on wifi, you need only click => ok !

  • Start: netsh wlan start hostednetwork

  • Stop: netsh wlan stop hostednetwork

Right click shortcut / Properties / Advanced... / Tick Run as administrator/ ok / Apply / ok.

Example: Youtube.



来源:https://stackoverflow.com/questions/33787279/execute-batch-file-to-start-wifi-hotspot-as-admin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!