问题
I am very new to batch scripting and have to use console to interrogate Registry for network profile description and output only the description data to a txt file. I am using a for /f loop to do this. I first reg query the whole key so it lists every sub key for network profiles and stores this in a text document. I then for /f this text file to extract out only the subkey name using tokens to store this as a variable. I then use the variable to reg query the individual keys for the Description name and output this to another text file which should display only the Network profile description. Below is my batch script.
Echo Required to skip line for processing >>%~dp0\1SSID.txt
REG QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles" /s /v Description >>%~dp0\1SSID.txt
setlocal enableDelayedExpansion
rem get each key from 1SSID.txt
for /f "usebackq skip=1 tokens=1,2" %%i in ("%~dp0\1SSID.txt") do (
echo %%i %%j>>%~dp0\2Processingstage.txt
rem skip the first line and grab tokens 3 from the second line to show description and desription name
for /f "usebackq skip=1 tokens=3" %%k in (`reg query "%%I %%j" /v Description`) do set "Description=%%l
echo Network Description - %%l >>%~dp0\3SSIDoutput.txt
)
)
The first think I notice is the skip=1 doesn't work and look at every line. As this doesn't work it doesn't extract the correct data to place into the reg query. I have tried with different tokens, without skip, with skip, with delims (which it didn't recognise). Ive been working on this for hours and simply cant get it to work. This is probably simple but I cant find a way around this.
回答1:
From this thread : Wi-Fi SSID detail
I don't know if this code works over english machines or not, because, i just tried it until now, on my french machine. so, just give a try and tell me the results :
@echo off & setlocal enabledelayedexpansion & color 0A
Title %~n0 to get SSID With details
::::::::::::::::::::::::::::::::::::::::::::
:: Automatically check & get admin rights ::
::::::::::::::::::::::::::::::::::::::::::::
Set TmpLogFile=%tmp%\TmpLog.txt
If Exist %TmpLogFile% Del %TmpLogFile%
REM --> Check for permissions
Reg query "HKU\S-1-5-19\Environment" >%TmpLogFile% 2>&1
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
Echo.
ECHO ****************************************
ECHO ^| Running Admin shell... Please wait...^|
ECHO ****************************************
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"=""
echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:gotAdmin
::::::::::::::::::::::::::::
::START
::::::::::::::::::::::::::::
Set "TmpLog=%~dp0%~n0_Tmp.txt"
Set "Log=%~dp0%~n0.txt"
If Exist "%TmpLog%" Del "%TmpLog%"
If Exist "%Log%" Del "%Log%"
rem Populate the array
Set i=0
for /f "skip=1 tokens=2 delims=:" %%a in ('netsh wlan show profiles ^|find /i "Profil"') do (
set /A i+=1
set "list[!i!]=%%a"
)
set SSID=%i%
rem Display array elements for SSID List
cls
for /L %%i in (1,1,%SSID%) do (
echo(
echo SSID number %%i: "!list[%%i]:~1!!"
echo(
)
pause
rem Display array elements for SSID List with details
cls
for /L %%i in (1,1,%SSID%) do (
echo(
echo SSID number %%i: "!list[%%i]:~1!!"
echo(
netsh wlan show profiles "!list[%%i]:~1!!" key=clear
netsh wlan show profiles "!list[%%i]:~1!!" key=clear >> "%TmpLog%"
)
Cmd /U /C Type "%TmpLog%" > "%Log%"
If Exist "%TmpLog%" Del "%TmpLog%"
Start "" "%Log%"
pause
exit /b
回答2:
Here is an quick example which should output a file providing each GUID and Description alongside your running script.
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
(Set k=HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles)
For /F "Delims==" %%A In ('Set GUID[ 2^>Nul') Do Set "%%A="
Set "i=101"
For /F "EOL=E Tokens=1,2*" %%A In ('Reg Query "%k%" /S /V Description') Do (
If "%%~nB" NEq "%%~B" (Call Set "GUID[%%i:*1=%%]=%%~nB") Else (
Call Call Set GUID[%%i:*1=%%]="%%%%GUID[%%i:*1=%%]%%%%","%%C"
Set/A i+=1))
If %i% NEq 101 (>"%~dp0NetProfs.log" 2>Nul Set GUID[)
EndLocal
Exit/B
You will probably need to right-click and run as Administrator due to resrictions on those keys.
来源:https://stackoverflow.com/questions/39387242/extracting-network-profiles-and-displaying-only-network-profile-description-in-t