I am trying to add a line to my start up scrip which would automatically start the VPN if I am connected to WiFi networks outside of my work.
I can get the names from netsh wlan commands, but I am not able to just "pick" the SSID/network name from that.
Is there a way to extract particular information from the netsh output or is there a way to output just the wifi name?
Thanks in advance.
You can use the findstr command to find a string in a commands, specifically the SSID.
Script:
#echo off
set "wifiNetwork=example network"
(netsh wlan show networks | findstr /C:": %wifiNetwork%" >nul 2>&1)&&goto :found
echo Wi-Fi network "%wifiNetwork%" not found
goto :end
:found
echo Wi-Fi network "%wifiNetwork%" found!
:end
pause
NOTE: The findstr command is case-sensitive unless you add the /I option
I am writing a shell script which runs on cygwin console for windows, my scritp looks like this
#!/usr/bin/bash
cmd /c start /d "C:\cygwin\bin" ipconfig | findstr /R /C:"IPv4 Address"
ipconfig #to print
route add 10.2.139.1 192.168.104.1
But when i execute this script on cygwin console it shows below error and even i changed to ipconfig \all it doesn't work
Error: unrecognized or incomplete command line.
USAGE:
ipconfig [/allcompartments] [/? | /all |
i am trying to get ip address dynamically by executing the script and adding to the route table
Thanks,
Rohith
I don't know why you did this with cygwin instead of cmd.exe, what you use it for and why use start, but, all you missing is one option /b:
cmd /c start /d "C:\cygwin\bin" /b ipconfig | findstr /R /C:"IPv4 Address"
And start is redudant as follows:
cmd /c ipconfig | findstr /R /C:"IPv4 Address"
/b just suppresses the newly created cmd.exe background window.
I am on a work computer with dynamic IP address (Ipv4), which usually changes when I restart it. Since I sometimes need to use this PC through remote desktop connection at home I like to keep its IP address handy. But I sometimes forget to check the IP at restart, so is there a batch file or some code which can start up the cmd and display the IPv4 address everytime the computer restarts ?
thanks.
Simple, just create a batch file that runs ipconfig and displays the output:
start cmd /k ipconfig
exit
You'll see a console window appear on your screen each time you execute this batch file that contains the output of the ipconfig command. Among the information displayed will be the IP address for each of your computer's network adapters.
You can configure the command as appropriate, adding switches to ipconfig as desired. For example, adding the /all switch will cause additional information to be displayed.
If you wanted, you could parse the output of ipconfig, extract the IP address assigned to a particular network adapter, and display just that on the screen. That might reduce the cognitive overload. But any good Windows sysadmin can scan the output of ipconfig rather easily.
You can set a batch file to run upon startup.
The batch file should contain:
ipconfig
pause
I used this in a .bat file I created some days ago and it works fine:
#echo off
for /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "IPv4"') do set ip=%%b
set ip=%ip:~1%
echo %ip%
This way the IP is defined as a variable named "ip", so you can use this to do other things with your current IP.
I hope it helped somehow.
When running a command-line script, is it possible to get the name of the current user?
You can use the username variable: %USERNAME%
Username:
echo %USERNAME%
Domainname:
echo %USERDOMAIN%
You can get a complete list of environment variables by running the command set from the command prompt.
Just use this command in command prompt
C:\> whoami
It should be in %USERNAME%. Obviously this can be easily spoofed, so don't rely on it for security.
Useful tip: type set in a command prompt will list all environment variables.
%USERNAME% is the correct answer in batch and other in Windows environments.
Another option is to use %USERPROFILE% to get the user's path, like C:\Users\username.
The answer depends on which "command-line script" language you are in.
Cmd
In the old cmd.exe command prompt or in a .bat or .cmd script, you can use the following:
%USERNAME% - Gets just the username.
%USERDOMAIN% - Gets the user's domain.
PowerShell
In the PowerShell command prompt or a .ps1 or .psm1 script, you can use the following:
[System.Security.Principal.WindowsIdentity]::GetCurrent().Name - Gives you the fully qualified username (e.g. Domain\Username). This is also the most secure method because it cannot be overridden by the user like the other $Env variables below.
$Env:Username - Gets just the username.
$Env:UserDomain - Gets the user's domain.
$Env:ComputerName - Gets the name of the computer.
Any Shell
whoami - Gets the user's domain and username in the format "domain\username".
This also works on Unix systems as well, not just Windows, so it's a nice cross-platform solution.
%USERNAME% will get you the username of the currently running process. Depending on how you are running your batch file, this is not necessarily the same as the name of the current user. For example, you might be running your batch file through a scheduled task, from a service, etc.
Here is a more sure way of getting the username of the currently logged on user by scraping the name of the user that started the explorer.exe task:
for /f "TOKENS=1,2,*" %%a in ('tasklist /FI "IMAGENAME eq explorer.exe" /FO LIST /V') do if /i "%%a %%b"=="User Name:" set _currdomain_user=%%c
for /f "TOKENS=1,2 DELIMS=\" %%a in ("%_currdomain_user%") do set _currdomain=%%a & set _curruser=%%b
I use this method in writing batch files for testing.
echo %userdomain%\%username%
Since you must include the password in plain text if authentication is required, I will only use it in a completely private environment where other users cannot view it or if a user seeing the password would bear no consequences.
Hope this helps someone out.
In most cases, the %USERNAME% variable will be what you want.
echo %USERNAME%
However, if you're running an elevated cmd shell, then %USERNAME% will report the administrator name instead of your own user name. If you want to know the latter, run:
for /f "tokens=2" %u in ('query session ^| findstr /R "^>"') do #echo %u
It's always annoyed me how Windows doesn't have some of more useful little scripting utilities of Unix, such as who/whoami, sed and AWK. Anyway, if you want something foolproof, get Visual Studio Express and compile the following:
#include <windows.h>
#include <stdio.h>
int main(int argc, char **argv) {
printf("%s", GetUserName());
}
And just use that in your batch file.
Just type whoami in command prompt and you'll get the current username.
This is the main difference between username variable and whoami command:
C:\Users\user.name>echo %username%
user.name
C:\Users\user.name>whoami
domain\user.name
DOMAIN = bios name of the domain (not fqdn)
Via powershell (file.ps1)
I use the following
$username = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
It returns the name of the user in the "Domain\Username" format.
If you just want the username just write
$username = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name.Split("\")[1]
The advantage is that It works with windows 10 windows 8 server 2016. As far as I remember with also other OS like Win7 etc. (not older)
. And yeah via batch you can simply use
$username = &whoami
In a standard context, each connected user holds an explorer.exe process: The command [tasklist /V|find "explorer"] returns a line that contains the explorer.exe process owner's, with an adapted regex it is possible to obtain the required value. This also runs perfectly under Windows 7.
In rare cases explorer.exe is replaced by another program, the find filter can be adapted to match this case. If the command return an empty line then it is likely that no user is logged on. With Windows 7 it is also possible to run [query session|find ">"].
As far as find BlueBearr response the best (while I,m running my batch script with eg. SYSTEM rights) I have to add something to it.
Because in my Windows language version (Polish) line that is to be catched by "%%a %%b"=="User Name:" gets REALLY COMPLICATED (it contains some diacritic characters in my language) I skip first 7 lines and operate on the 8th.
#for /f "SKIP= 7 TOKENS=3,4 DELIMS=\ " %%G in ('tasklist /FI "IMAGENAME eq explorer.exe" /FO LIST /V') do #IF %%G==%COMPUTERNAME% set _currdomain_user=%%H
I have a batch file that uses this idiom (many times) to read a registry value into an environment variable:
FOR /F "tokens=2* delims= " %%A IN ('REG QUERY "HKLM\SOFTWARE\Path\To\Key" /v ValueName') DO SET MyVariable=%%B
(There's a tab character after delims=)
This works fine on thousands of customer's computers. But on one customer's computer (running Windows Server 2003, command extensions enabled),
it fails with 'REG QUERY "HKLM\SOFTWARE\Path\To\Key" /v ValueName' is not recognized as an internal or external command, operable program or batch file.' Running the "reg query" command alone works fine. Reg.exe is present in C:\Windows\System32.
I was able to work around the problem by changing the code to
REG QUERY "HKLM\SOFTWARE\Path\To\Key" /v ValueName > temp.txt
FOR /F "tokens=2* delims= " %%A IN (temp.txt) DO SET MyVariable=%%B
This got the customer up and running, but I would like to understand why the problem occurred so I can avoid it in the future.
Slightly off the primary topic - a more direct way to get a registry value (string or DWORD) into an environment variable would also be useful.
I would check:
The customer's role on the machine - are they an admin?
Where is reg.exe on the box - is there more than one copy of copy of reg.exe in the path?
Is there any locale difference on the customer's machine from the machines where this normally works?
Basically, enumerate everything that differs between this machine and machines where it works as expected. Include service packs, domain membership, etc.
Wow, that is odd.
If the same commands work when split into two lines, then I'd guess it has something to do with the way the command gets run in a subshell in the FOR command.
If you were really dying to figure out why it's dying in this particular case, you could run commands like "SET > envvars.txt" as the FOR command and compare that with the top shell.
Or maybe start off simple and try running the REG command via CMD /C to see if that does anything?
One quick guess here, what's the values of COMSPEC and SHELL ?
I had a similar situation to this. In my case it was a bad value in COMSPEC. I fixed that and the script started working as expected.
The /F switch needs command extensions to be turned on. Usually they are turned on by default, but I'd check that. On XP systems you can turn them on doing something like
cmd /e:on
or checking the registry under
HKCU\Software\Microsoft\Command Processor\EnableExtensions
Dunno about Windows Server.
Doing help for and help cmd could provide some hints as well.