Registry find value, take key name then search and update another value - variables

No idea if title helps here, so i'll be as descriptive as i can.
I need to update the "Use this connection’s DNS Suffix in DNS registration" value for a certain Network card, i only know what the NIC Name ("NIC3-Networkname") is and it will be different on each server i touch.
I have identified the required steps, just not sure on how to handle step 2.
1) Query for "NIC3-Networkname"
reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network" /f "NIC3-Networkname" /s
This then returns the following.
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\{EFBE1796-C4F0-4612-B3D6-E94B794E84D4}\Connection Name REG_SZ NIC3-Networkname
2) Next i need to take the {EFBE1796-C4F0-4612-B3D6-E94B794E84D4} Key name, set it as a variable - lets say %NICUID%
3) Then need to take the above variable and add the DNS Name.
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\%NICUID%" /f /v "Domain" /t REG_SZ /d "Company.com"
Any tips and advise welcomed!
Thanks
B

Parse the output of reg using \ as separator and take the 7th token:
for /f "delims=\ tokens=7" %%a in (
'reg query "HKLM\SYSTEM\CurrentControlSet\Control\Network" /f "NIC3-Networkname" /s'
) do set NICUID=%%a

Related

Use variable in batch file multiple times

I'm trying to compile a batch file to get the userID of the current user, then use the output to delete a certain registry key.
I found this to get the SID of the current logged in user:
wmic useraccount where name='%username%' get sid
This worked perfectly and gave me:
SID
S-1-5-21-XXXXX-XXXXX-XXXX-XXX
Now I would like to use the S-1-5-21.... etc. To delete a certain registry key for the current logged in user, so the S-1-5-21 etc. How can I do that? Or is there an easier way, being able to determine current SID and delete a key consequently?
thank you!
Assuming you want to delete the registry key...
HKEY_USERS\[SID-OF-USER]\test
...this script should cut it:
#ECHO OFF
SET USERNAME=SomeUserName
:: retrieve user's SID, store in file. NOTE: a user can have more than 1 SID.
wmic useraccount where name='%USERNAME%' get sid | FINDSTR "^S-" > tmp.txt
:: load result into variable. NOTE: this will load the first line only.
SET /P SID_=<tmp.txt
:: remove spaces which turned out to be appended at the end.
SET SID=%SID_: =%
:: delete key in the registry (will probably required elevated privileges)
:: the /F causes reg.exe not to ask for confirmation
REG DELETE "HKEY_USERS\%SID%\test" /F
:: cleanup
DEL tmp.txt
I'm not aware of an easier way to "determine current SID and delete a key".
Try this:
#echo off
wmic useraccount where name='%username%' get sid > temp.txt &more temp.txt > temp2.txt &del temp.txt
for /F "tokens=*" %%A in (temp2.txt) do set "userID=%%A"
del temp2.txt
echo %userID%
pause
You can use the %userID% variable after this code.
Note that you need to use the more because of the way wmic parses it's output

How can I set the line a string is found on as a variable, and then find the contents of that line number in another file?

Okay, this is kind of complicated so ill try to explain it as best as is can.
I am currently writing a simple program for my own use using notepad.
I am using the language batch and running the program through Command Prompt.
Part of the Program lets you access an account you created with username and password:
set /p USERNAME1= Username?
findstr /n "%USERNAME1%" Usernames.txt
In Usernames.txt are the usernames of each account that has been created, one per line.
If your username is found, it is displayed along with the line number before it in the program.
It then asks for your password:
set /p PASSWORD1= Password?
This is where the problem starts. When the accounts are created, the usernames are stored, one per line, in Usernames.txt , and so are the passwords but in Passwords.txt
I need the program to check if the password you typed is the same as the password on the same line number the username is on, in Passwords.txt
I know this is complicated but if anyone can help it would be greatly appreciated!
Thanks
You're using findstr /N to get the line number of usernames.txt, which is a good start. You're getting the entire line, prefixed with the line number and a colon :.
So for /F can be used to extract the number only. The option string "tokens=1 delims=:" defines to divide the found line at the (first) :, so the line number is separated from the user name.
Finally, another for /F can be used to get the line of the passwords.txt files.
Putting all those things together, the following code snippet emerges:
set /P USERNAME1=Username?
set /P PASSWORD1=Password?
set /A LINENUMBER=0
for /F "tokens=1 delims=:" %%I in (
'findstr /N /I /X /C:"%USERNAME1%" "\path\to\usernames.txt"'
) do (
set LINENUMBER=%%I
goto :CONTINUE1
)
:CONTINUE1
set /A LINENUMBER-=1
if %LINENUMBER% lss 0 (
exit /B
) else if %LINENUMBER% equ 0 (
set SKIPPING=
) else (
set SKIPPING=skip=%LINENUMBER%
)
for /F "usebackq %SKIPPING% delims=" %%I in (
"\path\to\passwords.txt"
) do (
if "%%I" equ "%PASSWORD1%" (
goto :CONTINUE2
)
exit /B
)
:CONTINUE2
rem do something...
So the variable USERNAME1 holds the entered user name to search and PASSWORD1 the entered password.
If the user name cannot be found in usernames.txt, or the entered password does not match the found one, the script is terminated using exit /B.
Note that user names are compared in a case-insensitive manner (/I switch), but the password are compared case-sensitively.

How can I resolve the IP address and then set a variable to the resolved pc name

I am trying to think of an easier way, read less prone to messing up the commands, to resolve an IP address and then set a variable to said resolved name. I tried the following:
FOR /F "TOKENS=3 DELIMS=gw" %%A IN ('PING -a -n 1 IPADDRESS') DO SET "PC=%%A"
Which on:
Pinging PCNAME [IPADDRESS} with 32 bytes of data:
Reply from IPADDRESS: bytes=32 time =15ms TTL=122
Ping statistics for IPADDRESS:
Packets: Sent = 1, Received = 1, Lost = 0 (0% loss)
Approximate round trip times in milli-seconds:
Minimum = 15ms, Maximum = 15ms, Average = 15ms
Works for most pc names. There are only a few "g" and "w" so the only line with a 3rd token is the first. The problem I experienced is that we have A LOT of pc names and a few have a "g" in them. For those resolved addresses it cuts of the variable at the 3rd "g" of the line.
I tried using it on NSLOOKUP, but the command gives me:
C:\Windows\System32>nslookup IPADDRESS
Server: SERVERNAME
Address: SERVERIP
Name: PCNAME
Address: IPADDRESS
I can skip in FOR /F (ie skip 2) but then the it reads lines 3 AND 4; ie it sets the variable to the pc name for line 3, but then changes it on line 4 to garbage I don't need.
Like I said is there a way to this easily? I can do a for in a for with calls and delayed expansion, but that is much harder to keep track of.
Filter the info you need ("Name:")
for /f "tokens=2 delims=: " %%i in ('nslookup 192.168.178.1^|find "Name:"') do set name=%%i
There is a simple method that may be used with both PING or NSLOOKUP.
With PING:
FOR /F "TOKENS=2" %%A IN ('PING -a -n 1 IPADDRESS') DO SET "PC=%%A" & GOTO CONTINUE
:CONTINUE
With NSLOOKUP:
FOR /F "SKIP=2 TOKENS=2" %%A in ('NSLOOKUP IPADDRESS') DO SET "PC=%%A" & GOTO CONTINUE
:CONTINUE
The GOTO CONTINUE is used to break the FOR after the desired information was taken.
#echo off
setlocal enableextensions
set "ip=127.0.0.1"
set "pcName="
for /f "tokens=1-3 delims=[]" %%a in ('ping -n 1 -a %ip% 2^>nul') do if not defined pcName if not "%%c"=="" (
set "pcName=%%a"
setlocal enabledelayedexpansion
for %%d in ("!pcName: =\!.") do ( endlocal & set "pcName=%%~nxd")
)
echo %pcName%
This reads the output of ping command, and splits the lines using []. The only line containing square brackets is the line with the pc name (if found). We will skip lines that are not splitted in three tokens (%%c is empty).
The first token will contain the pc name prefixed by a string indicating a ping operation is being done. This string is localized and changes depending of the windows language (in my case, spanish windows, it is "Haciendo ping a"). So, asking for a fixed token is not reliable. To avoid it, the string is converted to a false path by replacing the spaces with backslashes. Then the standard for replaceable parameter modifiers are used to retrieve the name of the last element in the false path, that is, the pc name.
Using the original approach:
FOR /F "TOKENS=2* DELIMS=gw" %%A IN ('PING -a -n 1 IPADDRESS') DO if not "%%B"=="" for /f %%C in ("%%B") do SET "PC=%%C"
(just to demo)

batch list net view and write their ip address to a variable

I basically need a script (batch) that automatically converts the users on the local network to IP addresses. What I mean is basically set every user that shows up in the command "net view" to a different variable, for example:
If I had 4 different users on the network, I would need the file to list:
1: (%1%)
2: (%2%)
3: (%3%)
4: (%4%)
So I need the script to set each user on the network to a different variable (starting at 1)
Also...
How would I set the local IP address of each computer name as a variable?
update for second part: I need to know how to set the ip address as a variable. I just need to cut out the excess stuff. If I type
ping (computername) -4
I get: well, I guess it is just easier to show you...
I think I need to use the findstr command, but I don't know.
Is this what you're looking for?
#echo off
setlocal enabledelayedexpansion
for /f "tokens=1" %%a in ('net view') do (
set comp=%%a & set comp=!comp:\\=!
for /f "tokens=2 delims=[]" %%b in (
'ping -4 !comp!'
) do (Echo !comp! - %%b)
)

Grepping `reg query` result value?

Consider the following result from a hypothetical reg query:
..>reg query HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\MediaPlayer /v "I
nstallation Directory"
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\MediaPlayer
Installation Directory REG_EXPAND_SZ %ProgramFiles%\Windows Media Play
er
How do I grep the output so that I can assign the actual value of the setting (%ProgramFiles%\...) into a variable (or temp file)?
example for batch files:
#FOR /F "tokens=2* " %%A IN ('reg query HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\MediaPlayer /v "Installation Directory"') DO #SET PARAM=%%B
If you want to use it directly in console - use single percent sign to denote variables (i.e. %A and %B)