My goal is to have a batch script that whenever launched if the machine is connected to my home wifi (SSID = The Sanctum Sanctorum - 5GHz) will launch Kodi from one folder ("C:\Program Files (x86)\Kodi at Home") but otherwise will launch Kodi from another ("C:\Program Files (x86)\Kodi").
Any help is much appreciated and thanks for your time I have attached updates with my code progress!
Edit #3 - Thanks to #treintje
Code Attempt #3
#echo off
set "ssid=The Sanctum Sanctorum - 5GHz"
netsh wlan show interfaces | findstr /r /c:"^ *SSID *: %ssid%$" 1>nul
if %errorlevel% equ 0 (
set "kodi=Kodi at Home\Kodi.exe" "-p"
) else (
set "kodi=Kodi\Kodi.exe"
)
"%programfiles(x86)%\%kodi%"
Now it seems even when connected to the specified network it always launches the "kodi=Kodi\Kodi.exe" version. I bet it's just a tiny syntax error I don't know to see as I'm sure treintje is right this is the way to do it!
Assuming your machine only has a single wireless network interface card installed, you could try something like this:
#echo off
set "ssid=The Sanctum Sanctorum"
netsh wlan show interfaces | findstr /r /c:"^ *SSID *: %ssid%$" 1>nul
if %errorlevel% equ 0 (
set "kodi=Kodi at Home\Kodi.exe" "-p"
) else (
set "kodi=Kodi\Kodi.exe"
)
"%programfiles(x86)%\%kodi%"
The netsh wlan show interfaces command lists information about all wireless network interfaces including the SSID of any wireless network that they might be connected with. If the findstr command finds the SSID specified by the ssid variable, the errorlevel is set to zero.
If the target application cannot handle command-line switches that are encapsulated within quotes, you could set the kodi variable and call the application as such:
set "kodi=Kodi at Home\Kodi.exe" -p"
"%programfiles(x86)%\%kodi%
Related
First of all I'm a complete noob to Batch scripting and networking, with that being said, Here is what I'm trying to accomplish.
I want to check if the server has provided this batch file executing pc to access it, if not create access to it. (many different pc's run this batch file)
This what I came up with for now in my batch file.
net use * \\ip\My_WebApp /Persistent:yes /user:Username Password
Exit
This batch file command create access to the server just fine but It creates a new access connection every time this file get executed. Which is not needed and might crash the server load.
How can I check if the Server already has provided the access, Only if not, execute the above command in a batch file. my logic like like....
boolean status = check_server_accessibility()
if(!status){
net use * \\ip\My_WebApp /Persistent:yes /user:Username Password
}
Exit
Appreciate any help, Thank you so much for your time.
You should only ever need to perform the task once, which suggests that a scripted solution isn't needed, (persistent means that!). I would assume that the following may be sufficient:
#Set "MyMap=\\ip\My_WebApp"
#%__AppDir__%wbem\WMIC.exe LogicalDisk Where "DriveType='4'" Get ProviderPath 2>NUL | %__AppDir__%findstr.exe /R /I "%MyMap:\=\\%\>" 1>NUL && GoTo :EOF
#%__AppDir__%net.exe Use * "%MyMap%" /Persistent:Yes /User:Username Password
Alternatively, if you needed to know which drive letter is currently assigned to it, then use a for-loop to retrieve the data:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Call :MapChk "\\ip\My_WebApp"
GoTo :EOF
:MapChk
Set "MyMap=%~1"
Set "MyDrv="
For /F "Skip=1 Delims=:" %%G In (
'%__AppDir__%wbem\WMIC.exe LogicalDisk Where "DriveType='4' And ProviderPath='%MyMap:\=\\%'" Get DeviceID 2^>NUL'
) Do For %%H In (%%G) Do Set "MyDrv=%%G:"
If Defined MyDrv (
Echo %MyMap% is already mapped to drive %MyDrv%.
%__AppDir__%timeout.exe /T 5 /NoBreak >NUL
GoTo :EOF
)
%__AppDir__%net.exe Use * "%MyMap%" /Persistent:Yes /User:Username Password
I've made this one into a callable label, so that you can more easily extend it for other mappings too, e.g. before the GoTo :EOF, (line 6), Call :MapChk "\\Server\My Share".
Note: These solutions are untested, I do not use a PC and have no mapped network locations to test them against. Please let me know if I have made a mistake somewhere.
With Windows 10 is it possible to setup up known networks and be able to connect to them without all the mouse movement and click?
Using Windows batch files, you can set it up to connect to networks you already know (Network1 or Network2, below) without ever touching the mouse.
#echo off
setlocal EnableDelayedExpansion
for %%i in ("Network1"
"Network2") do (
netsh wlan show networks mode=ssid | findstr /C:%%i
if !ERRORLEVEL! EQU 0 (
echo "Found %%~i - connecting..."
netsh wlan connect name=%%i
exit /b
) else (
echo "Did not find %%~i"
)
)
#echo on
Save the above to .bat and run it from cmd.exe or a program like Listary.
Some comments about the code:
If more than one of your listed networks are available, it will connect to whichever is first in the for loop list. You could also put the list in a file and change for %%i to for /F %%i
EnableDelayedExpansion and "!" around ERRORLEVEL
are needed to keep the variable ERRORLEVEL from being assigned
whatever it was at the beginning of the script. Since I don't
normally program Windows batch files, this is 2 hours of my life
gone that you won't have to deal with.
All the echoing is for debugging; the echo off at the top squelches it.
%% needed for variables in Windows batch files. The variable is referenced with % at the command line.
%%~i strips the quotation marks around the string when outputting to stdout.
I need to create a batch file that run this command
start chrome
The problem is that I'm not sure that on the machine there is Google Chrome installed. I can't rely on the registry because I will not have admin permissions.
If I run this command on a machine where Chrome is not installed I get this error dialog
(you see "chromex". I used this name to simulate the absence of the software).
Is there a way to avoid to show this dialog?
Next code snippet could help:
#ECHO OFF >NUL
SETLOCAL enableextensions
SET "_chromePath="
If DEFINED programfiles(x86) (
For /F "tokens=*" %%G in (
'where /R "%programfiles(x86)%" chrome.exe 2^>NUL'
) do SET "_chromePath=%%G"
)
If NOT DEFINED _chromePath (
For /F "tokens=*" %%G in (
'where /R "%programfiles%" chrome.exe 2^>NUL'
) do SET "_chromePath=%%G"
)
If DEFINED _chromePath (
echo start "" "%_chromePath%"
) else (
echo chrome.exe not found
)
Note start "" "%_chromePath%" is echoed merely for debugging purposes only.
Resources (required reading):
An A-Z Index of the Windows CMD command line
Windows CMD Shell Command Line Syntax
#echo off
echo Enter 1 to start up Chrome
set /p input=Type here:
if %input%==1 goto chrome
:chrome
echo Starting chrome...
start chrome.exe
pause
The dialog comes from using Start. Execute files directly to see the command prompt's equivlent.
'cat.exe' is not recognized as an internal or external command,
operable program or batch file.
Normally one would open the web site and windows will use the user's choice of browser.
Start "" www.microsoft.com
start "" "c:\windows\help\mmc\htm\mmc_1.htm"
I can't rely on the registry because I will not have admin permissions.
You have read access to most of the registry as a user and you can override most settings and file associations on a per user basis.
I've written a Windows script to change NIC interface metrics, and need to condense it to two commands, because of the manner in which it is executed. To render a long story short, I support an application (BladeLogic Server Automation [BSA]) that uses remote agents to call system commands.
I've hypothesized that when BSA runs the script, each command executes in a separate Command Prompt environment, so the environment variables used to store the route strings aren't persistent.
for /f "delims=" %a in ('netsh interface ipv4 dump ^| find "nexthop=1.1.1.1"') do #set VAR1=%a
netsh interface ipv4 set %VAR1:~4% metric=200
for /f "delims=" %a in ('netsh interface ipv4 dump ^| find "nexthop=2.2.2.1"') do #set VAR2=%a
netsh interface ipv4 set %VAR2:~4% metric=500
I've condensed the script as such and am testing it at the Command Prompt.
for /f "delims=" %a in ('netsh interface ipv4 dump ^| find "nexthop=1.1.1.1"') do #set VAR1=%a && netsh interface ipv4 set %VAR1:~4% metric=200
for /f "delims=" %a in ('netsh interface ipv4 dump ^| find "nexthop=2.2.2.1"') do #set VAR2=%a && netsh interface ipv4 set %VAR2:~4% metric=500
Unfortunately, it doesn't seem to recognize the proper syntax for the second command:
The following command was not found: interface ipv4 set %VAR1:~4% metric=200
Is there another way I could append the second command, so it's interpreted as being syntactically correct? I'm open to suggestions!
Matt,
You're correct that BladeLogic fires up separate shells for each statement, however, depending on what you're trying to achieve, it may be possible to "persuade" it to run a complete script remotely on your target.
If you're still facing this issue, please respond with a little more info about what you're trying to achieve and which approaches you've tried, and we can look a the alternatives.
-John.
I have a script that is run over a network, with VPN being the same as a LAN environment.
The script previously worked fine, as we had variables that stored the username and password for the administrator. However, due to a recent change, when we map a drive over the network and whatnot, the machine name is now needed in front of the administator username, E.g. machinename2343\administrator.
What I would like to do is take an existing command - perhaps such as nbtstat - and after entering the ip address, have the program pull the machine name and insert it into a variable.
I have found that Nbtstat can give me the machine name, but provides large amounts of unnecessary information for my task. Is there a way to filter out just the machine name in a reliable and consistent manner, or is there perhaps another network related command that perform in the same capacity?
`#echo off
FOR /f "tokens=1* delims= skip=23 " %%a IN ('nbtstat -a IPADDRESS) DO (
SET VARIABLE=%%a
GOTO Done
)
:Done
echo Computer name: %VARIABLE%`
You could do ping /a. The computer name is resolved. And this computer name is the second token. I haven't taken care of Error checking. I believe you could implement that yourself.
Try this:
#ECHO OFF
FOR /f "tokens=2* delims= " %%a IN ('PING -a -n 1 IPADDRESS') DO (
SET Variable=%%a
GOTO Done
)
:Done
echo Computer name: %Variable%
Put this in your batch file where it would fit.
You could just use the %computername% environment variable.
When I first read your post I thought you were running the batch file remotely on each machine. If that were the case having %computername% in the batch file would work, because when the batch file is executed remotely %computername% would be expanded based on the remote machine's environment variable not the local machine.
Looking back on it, it's still not very clear, but based on your comment I assume the batch file is running locally and then connecting to a set of machines to perform some operation(s).
You could use tool the WMI command-line tool to get the computer name. The solution would look similar to #Thrustmaster's, but I think it's a little cleaner since the output of wmic, in this case, does "filter out just the machine name in a reliable consistent manner." Of course you'd replace the 127.0.0.1 with the ip you want to query.
#ECHO OFF
FOR /F "tokens=*" %%A IN ('wmic /node:127.0.0.1 ComputerSystem Get Name /Value ^| FIND "="') DO (
SET COMP.%%A
)
ECHO %COMP.NAME%