Getting the status of an Network Interface (WMIC) - wmic

I've been researching this for about 2 hours now and and I have the basic cmd:
wmic path Win32_PerfRawData_Tcpip_NetworkInterface Get BytesReceivedPersec,BytesSentPersec,BytesTotalPersec
I don't want the bytes for all of my network interfaces.
I want to get the total bytes for a certain interface, does anybody know how to do this because I cannot find anything on here or the web.

I don't if this what you want to get or not ?
#echo off
Title Get TotalBytes
#for /f "skip=2 tokens=2 delims=," %%a in (
'wmic path Win32_PerfRawData_Tcpip_NetworkInterface Get BytesTotalPersec /format:csv'
) do (Set "TotalBytes=%%a")
echo TotalBytes = %TotalBytes%
pause

Related

Put a WMIC query result into a variable

I'm trying to put the result of:
"WMIC /Node:localhost /Namespace:\\root\SecurityCenter2 Path AntiVirusProduct get /value | find "displayName=Symantec""
into a variable so that I can then ECHO the result.
Could you please advise?
After I spent some more time on this I found the answer. Only a "^" was needed. So command looks like:
FOR /F "tokens=*" %A IN ('WMIC /Node:localhost /Namespace:\\root\SecurityCenter2 Path AntiVirusProduct get /value^| find "displayName=Symantec"') DO (SET HOST=%A)
Then if you echo HOST -> you got it (or I got it).
Thanks anyway :D

How to set batch variable to output of another script

I try to set a batch variable to an output of another command. In Linux/Unix you can simply use backticks, e.g. (in csh)
set MY_VAR = `tail /etc/passwd`
Is there something similar available in windows batch?
Actually I found already something but it is not fully working:
d:\>for /F "skip=1" %n in ('wmic OS Get CurrentTimeZone') do set TimeZone=%n
d:\>set TimeZone=120
:\>set TimeZone=
d:\>
The problem is the wmic commands returns several lines, otherwise it would work fine. The first I know to skip, however I did not manage to skip the second empty line. I tried with IF but no success.
yes - the output of wmic is a bit ugly to handle.
Use a trick: search for a number in the ouput (findstr "[0-9] will only return lines, that contain a number):
for /F %n in ('wmic OS Get CurrentTimeZone ^|findstr "[0-9]"') do set TimeZone=%n
echo Timezone is %TimeZone%.
(for use in a batchfile use %%n instead of %n)
Another way is:
for /F %n in ('wmic OS Get CurrentTimeZone') do if not defined TimeZone set TimeZone=%n
EDIT:
I prefer the first version, as findstr (or find) converts the wmic-line-endings, so the second for mentioned by MC ND is not neccessary.
I suggest following batch code:
#echo off
for /F "skip=1" %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS Get CurrentTimeZone') do (
set "TimeZone=%%I"
goto BelowLoop
)
:BelowLoop
echo Time zone difference is: %TimeZone%
The FOR loop is exited with command GOTO after the value of interest is assigned to environment variable TimeZone.
The entire FOR loop can be optimized to a single command line:
#echo off
for /F "skip=1" %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS Get CurrentTimeZone') do set "TimeZone=%%I" & goto BelowLoop
:BelowLoop
echo Time zone difference is: %TimeZone%
Exiting the FOR loop after having the value of interest avoids the problem with wrong parsing of Unicode (UTF-16 Little Endian) encoded output of WMIC by FOR which otherwise would result in deleting the environment variable TimeZone. For details on wrong parsing of Unicode output by FOR see answer on How to correct variable overwriting misbehavior when parsing output?
for /f "tokens=2 delims==" %a in ('wmic OS get CurrentTimeZone /value') do set "timeZone=%a"
(to use in a batch file, remember to double the percent signs)
The added /value in wmic changes its output to key=value format. The delims clause in for command indicates a = as a separator. The tokens clause ask to retrieve only the second token/field in the line. As the only line with two tokens is the line with the required data, only this line is processed.
BUT, wmic output includes an aditional carriage return at the end of its output, that needs to be removed from the variable. An aditional for command can be used. The resulting command will be
for /f "tokens=2 delims==" %a in ('wmic OS get CurrentTimeZone /value') do for /f %b in ("%a") do set "timeZone=%b"
Or, for a batch file
for /f "tokens=2 delims==" %%a in (
'wmic OS get CurrentTimeZone /value'
) do for /f %%b in ("%%a") do set "timeZone=%%b"
echo %timeZone%

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)
)

Batch file help needed - functions, variables and substrings

I'm trying to write a batch script for windows XP. It needs to build an IP address from ipconfig by getting the (192.168.XXX.30) XXX part of the address and supplanting it into a template. Then some fun stuff from a network shared folder.
SET _var=ipconfig |FIND "192.168"
SET _var=%_var:~25,-4%
net use z: \\192.168.%_var%.30\test_folder
... Do stuff
net use z: \DELETE
ECHO "Tasks completed"
PAUSE
At the moment I can't seem to get the result from ipconfig in to my variable, would be even better if I could get the substring from it in 1 line.
Any ideas?
The first task, per your question, is to get the IP address. To get one from www.google.com:
for /f "tokens=2 delims=[]" %f in ('ping -4 -n 1 www.google.com ^|find /i "pinging"') do echo IP=%f
Say the resulting value of IP is now 192.168.10.20. You can get the third octet of %IP% from a second FOR statement:
for /f "tokens=3 delims=." %f in ("%IP%") do echo ip3=%f
The result from the above statement, given the value of the example, would be:
ip3=10