I need to run a batch file only if it's connected to Wifi and specifically not Bluetooth LAN
I have this code but it returns this and still runs the code while an internet connection isn't present
Node - DEVICENAME
ERROR:
Description = Invalid query
Code:
#echo off
For /f "usebackq" %%A in (
`wmic path WIN32_NetworkAdapter where 'NetConnectionID="Wi-Fi"' get NetConnectionStatus`
) do if %%A equ 7 (goto end)
<code to run>
:end
You don't need a for loop:
wmic path WIN32_NetworkAdapter where 'NetConnectionID="Wi-Fi"' get NetConnectionStatus |find "7" >nul && goto :eof
echo code to run
If you want to make it more secure, instead of find "7" use findstr /rc:"^7 *$"
(your original approach fails because the = has to be escaped: ... where 'NetConnectionID^="Wi-Fi"' get ... and due to the unusual wmic output, there are CR in your %%A, which messes up the if syntax; You can see both issues with echo on (at least you can see that strange things happen))
The output of WMIC is also the reason for that strange findstr pattern, I used. (there are trailing spaces after the 7).
If you don't already know the name of the wireless interface connection, (which is a configurable property), then you could probably use something more like this:
#For /F Tokens^=6^ Delims^=^" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe NIC
Where "Not NetConnectionID Is Null And NetConnectionStatus='2'" Get
NetConnectionID /Format:MOF 2^>NUL') Do #%SystemRoot%\System32\netsh.exe WLAN^
Show Interfaces 2>NUL | %SystemRoot%\System32\findstr.exe /E /L ": %%G" 1>NUL^
&& <code to run>
If your target systems are still using Windows 7, (which has a known issue locating some of the XSL files used in the /Format option), then the following alternative may work for you:
#For /F "Skip=1 Delims=" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe NIC Where
"Not NetConnectionID Is Null And NetConnectionStatus='2'" Get NetConnectionID
2^>NUL') Do #For /F "Tokens=*" %%H In ("%%G") Do #%SystemRoot%\System32\netsh.exe^
WLAN Show Interfaces 2>NUL | %SystemRoot%\System32\findstr.exe /E /L ": %%H" 1>NUL^
&& <code to run>
You would obviously change your provided, and replicated above, <code to run>, to one or more actual valid commands
Related
Problem defined is to have a command line that takes the output of nslookup from a FOR /F loop and either sends to the IP Address to a text file or sends a error message.
The simplest iteration I have is:
FOR /F "skip=3 usebackq tokens=1*" %a IN (`^"nslookup ADROOT02.adroot.the-server-company.co.za 2^>^&1^"`) DO #echo %a | findstr /C:"Address" >nul 2>&1 && echo found || echo not found
However, this only works if an address is found, but no output is received if the findstr fails; i.e. no || echo not found is not activated for some reason.
I am looking for assistance is finding the issue/resolution to have a single command line executable that can define the IP addresses.
#ECHO OFF
SETLOCAL
FOR %%d IN (google.com ADROOT02.adroot.the-server-company.co.za ) DO (
NSLOOKUP %%d 2>&1|FIND "Non-existent domain" >nul&IF ERRORLEVEL 1 (ECHO %%d found) ELSE (ECHO %%d NOT found)
)
GOTO :EOF
Yields
google.com found
ADROOT02.adroot.the-server-company.co.za NOT found
for me.
With guidance from this forum I was able to put a command together that outputs the IP address from nslookup or error if no IP found:
FOR /F "usebackq tokens=1* delims=: " %a IN (`^"nslookup server-company.co.za 2^>^&1` ^| findstr ^/N ^/C^:^"Address^" 2^>^&1 ^"`) DO IF %a GTR 3 ( SET res2=%b & echo %res2:~10%) ELSE (echo ERROR)
I want a script that uses these options in CMD:
wmic bios get serialnumber
Then ask for a Vendor ie. HP or Dell.
Then the output should be something like vendor-serialnumber from wmic, then add a - in the serialnumber, like this:
YHT3-1234
the "-" should always be before the last 4 digits in the serialnumber,
so the final output would be something like :
HP-YHT3-1234
Here is another way to get it with PowerShell. Unless your organization has banned it, PowerShell is available on all supported Windows systems.
FOR /F "delims=" %A IN ('powershell -NoLogo -NoProfile -Command ^
"$x=Get-CimInstance CIM_BIOSElement;"
"'{0}-{1}-{2}' -f"
"#($x.Manufacturer, $x.SerialNumber.Substring(0,$x.SerialNumber.Length - 4),"
"$x.SerialNumber.Substring($x.SerialNumber.Length - 4))"') DO (SET "RESULT=%A")
ECHO RESULT is %RESULT%
Remember to double the PERCENT SIGN characters if this goes into a .bat/.cmd script.
FOR /F "delims=" %%A IN ('powershell -NoLogo -NoProfile -Command ^
"$x=Get-CimInstance CIM_BIOSElement;"
"'{0}-{1}-{2}' -f"
"#($x.Manufacturer, $x.SerialNumber.Substring(0,$x.SerialNumber.Length - 4),"
"$x.SerialNumber.Substring($x.SerialNumber.Length - 4))"') DO (SET "RESULT=%%A")
ECHO RESULT is %RESULT%
Here's a quick example to show you how to introduce the hyphen/dash into your serial number, together with some additional work to auto detect the Manufacturer, and shorten to an identifier as necessary.
#Echo Off
SetLocal EnableExtensions
Set "Vendor="
Set "Serial="
For /F Tokens^=6^ Delims^=^" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe BIOS Where "Manufacturer Like '%%Hewlett-Packard%%' Or Manufacturer Like '%%Dell%%' And SerialNumber Is Not NULL" Get Manufacturer^,SerialNumber /Format:MOF 2^>NUL') Do If Not Defined Vendor (Set "Vendor=%%G") Else Set "Serial=%%G"
If Not Defined Vendor GoTo :EOF
If "%Vendor:Dell=%" == "%Vendor%" (Set "Serial=HP-%Serial:~,-4%-%Serial:~-4%") Else Set "Serial=Dell-%Serial:~,-4%-%Serial:~-4%"
Echo %Serial%
Pause
Obviously if there are a lot of Manufacturers then additional work would need to be done.
As an unnecessary courtesy, here is the same methodology, just this time affording end user the opportunity to break the code, or corrupt the result:
#Echo Off
SetLocal EnableExtensions
Set "Serial="
For /F Tokens^=6^ Delims^=^" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe BIOS Where "SerialNumber Is Not NULL" Get SerialNumber /Format:MOF 2^>NUL') Do Set "Serial=%%G"
If Not Defined Serial GoTo :EOF
Set /P "Vendor=Type the name of the Computer Manufacturer, and press ENTER>"
Echo %Vendor%-%Serial:~,-4%-%Serial:~-4%
Pause
Based upon a very limited test of one HP notebook and the comments by KJ below, the following idea may also be useful:
#Echo Off
SetLocal EnableExtensions
Set "Serial="
Set "Vendor="
For /F Tokens^=2^,4^ Delims^=^" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe
CSProduct Assoc:List 2^>NUL') Do Set "Serial=%%G" & For /F %%I In ('
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NoProfile
"-Join '%%H'.ToUpper()[0..3]"') Do Set "Vendor=%%I"
If Not Defined Serial GoTo :EOF
Echo %Vendor%-%Serial:~,-4%-%Serial:~-4%
Pause
This uses PowerShell just to return up to the first four characters in upper case from the string returned as the Name value from the CSProduct alias of WMIC. So Compaq would return COMP, Dell as DELL, Lenovo as LENO Samsung as SAMS etc. This would then be prefixed to the serial number, (split as before), which in my test was returned as the ComputerSystemProduct IdentifyingNumber.
I have been using this batch file to collect the Serial number and UUID number and output to a CSV and now it no longer works.
#echo off
set outputfile="Y:\HP\UUDI.csv"
for /f "delims== tokens=2" %%i in ('wmic csproduct Get "UUID" /value') do SET CSPRODUCT=%%i
for /f "delims== tokens=2" %%i in ('wmic bios get serialnumber /value') do SET SERIAL=%%i
echo UUID,Serial,>>%outputfile%
echo %CSPRODUCT%,%SERIAL%,>>%outputfile%
If someone can look at this file and help me understand what went wrong I would appreciate it
I don't understand what did you mean by "No Longer Works" ? Please be more explicit when you ask a question !
here is a test and tell me if this works or not on your side and i will edit this aswer according to your response !
#echo off
set "outputfile=%~dp0UUDI.csv"
#for /f %%i in (
'wmic csproduct Get "UUID" /value ^& wmic bios get serialnumber /value'
) do (
#for /f %%j in ("%%i") do set "%%j" & echo "%%j"
)
echo UUID,SerialNumber>"%outputfile%"
echo %UUID%,%SERIALNumber%>>"%outputfile%"
If exist "%outputfile%" Start "" "%outputfile%" & Exit
The only reason I can see for your provided code to change its behavior, is that which was commented already by Mofi. That is, you've somehow caused the location of WMIC.exe to have been removed from the %Path% environment.
I have decided to provide an alternative method of achieving your goal using your chosen command utility WMIC.exe, and using its full path, to prevent such a reliance in future.
The WMIC command is traditionally one of the slower ones, so this method invokes it only once. All you should need to do is Echo your commands, currently on lines 12and 14, each separated as in line 13. If any of your commands requires to Get more than one property, you should separate those with caret escaped commas, e.g. Get Property1^,Property2. The results, (subject to line/environment length limitations), will then be saved to variables, %Title%, and %Record%, which can later be output to a file outside of the loop. Note: all commands should use /Value, or the more correct, /Format:List.
Example, (don't forget to adjust your output file path on line 4 as needed):
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "outputfile=Y:\HP\UUDI.csv"
Set "WMIC=%SystemRoot%\System32\wbem\WMIC.exe"
Set "FIND=%SystemRoot%\System32\find.exe"
Set "Title="
Set "Record="
For /F "Tokens=1,* Delims==" %%G In ('
(
Echo CSProduct Get UUID /Value
^&
Echo BIOS Get SerialNumber /Value
^)
^| %WMIC% ^| %FIND% "="
') Do (If Not Defined Title (Set "Title=%%G") Else (
SetLocal EnableDelayedExpansion
For /F "Tokens=*" %%I In ("!Title!") Do (EndLocal
Set "Title=%%I,%%G"))
If Not Defined Record (Set "Record=%%H") Else (
SetLocal EnableDelayedExpansion
For /F "Tokens=*" %%I In ("!Record!") Do (EndLocal
Set "Record=%%I,%%H")))
If Defined Title ( Echo %Title%
Echo %Record%) 1>"%outputfile%"
Im having a very weird problem where i want to print out the available usb drives connected on my laptop but when i try to do so this is what comes up on the command line.
for /f "delims=" %%a in ('wmic logicaldisk where drivetype^=2 get deviceid ^| find ":"') do set "List=!List!%%a"
:usb
echo Avaiable Drive ==^> !List!
set /p "Drive=Enter the letter of the Backup drive : "
echo !List! | find /i "%Drive::=%:" && Echo drive OK || Echo drive do not exist && goto:usb
This my code im fairly new to scripting, can someone help me fix the problem or improve the code as i dont know if its the best way to do this.
Your problem is the ugly wmic line ending, which includes an additional CR (Carriage Return). There are many ways to eliminate it, but in your situation simply use delims=: does the trick (and additionally removes the colon, which is a good idea for the following).
Let me suggest another way for User input (using choice, which eliminates the need to re-check the drive):
#echo off
setlocal enabledelayedexpansion
set "list= "
for /f "delims=:" %%a in ('wmic logicaldisk where drivetype^=2 get deviceid ^| find ":"') do set "List=!List!%%a"
choice /m "Enter the letter of the Backup drive : " /c %list%
set drive=!list:~%errorlevel%,1!:
echo Drive is %drive%
I've got a simple script that grabs a PC's manufacturer and sets a variable:
for /f "usebackq tokens=2 delims==" %%A IN (`wmic csproduct get vendor /value`) DO SET VENDOR=%%A
What I need to do next is to check if my new VENDOR variable matches anything in a list:
Acer
Gateway
Packard
Alienware
ASUS
Dell Inc.
Fujitsu
HP
Compaq
Lenovo
IBM
Samsung
Sony
Toshiba
and then DO COMMAND1 if found, and DOCOMMANDB if not found.
I think that piping the variable into findstr might work, but I only know findstr usage when you're feeding it a text file. I'm really bad at regex so I would need some guidance on that if it's the best option.
How can I accomplish the above?
If you pipe the Vendor to findstr /I /G:Vendorlist.txt and use conditional execution && for success and || for fail :
#Echo off
for /f "tokens=2 delims==" %%A IN (
'wmic csproduct get VENDOR /value'
) DO Set VENDOR=%%A
Echo:%VENDOR%|Findstr /I /G:VENDORList.txt >NUL 2>&1 &&(
Echo found %VENDOR% in List
)||(
Echo %VENDOR% not found in list
)
Slightly different approach. You could also put all the vendors in a list and read the list with a FOR /F.
#echo off
for /f "usebackq tokens=2 delims==" %%A IN (`wmic csproduct get vendor /value`) DO SET VENDOR=%%A
FOR %%G IN ("Acer"
"Gateway"
"Packard"
"Alienware"
"ASUS"
"Dell Inc."
"Fujitsu"
"HP"
"Compaq"
"Lenovo"
"IBM"
"Samsung"
"Sony"
"Toshiba") DO (
IF /I "%vendor%"=="%%~G" GOTO MATCH
)
:NOMATCH
echo Does not match
pause
GOTO :EOF
:MATCH
echo Does match
pause
GOTO :EOF
If you want to read the list where each entry is on its own line.
FOR /F "usebackq delims=" %%G IN ("filelist.txt") DO IF.......