How to display my PC's IP Address on Windows (7) Startup? - cmd

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.

Related

Text file not appearing in correct path location when executing batch script

It's my first time writing scripts for a college course I'm taking. I'm trying to write a simple batch script that creates a new folder, saves all the system IP configurations to a text file INSIDE that folder, and then pings the network and appends the text file with the results. any reason why this code
#echo off
md tempinfo
ipconfig /all > C:\Users\MyUser\tempinfo\output.txt
ping (IP address) >> C:\Users\MyUser\tempinfo\output.txt
is creating a tempinfo folder with nothing in it? I know the ipconfig and ping commands are working but I cannot get the text file to show up inside the tempinfo folder.
You can write something like that :
#echo off
If not exist "%UserProfile%\tempinfo" MD "%UserProfile%\tempinfo"
(
ipconfig.exe /all
ping.exe 127.0.0.1
)>"%UserProfile%\tempinfo\output.txt"
if exist "%UserProfile%\tempinfo\output.txt" Start "" /MAX "%UserProfile%\tempinfo\output.txt"

Stuck in cmd script loop

I'm using a script that, upon logging in, loads Remote Desktop Protocol (RDP) and pushes the user to a Virtual Desktop Environment (VDI), along with their credentials. The script does not allow the next command 'Shutdown /l' to run until it is finished running.
The problem I'm having is that once in the VDI, RDP tries to load up again. How do I prevent this from happening? Thank you in advanced.
start /wait \\ph-vda\C$\VDI_Users_RDP\%username%.rdp /multimon /noConsentPrompt
Shutdown /l
I think what you're describing is that the same script is running when the user logs in, even when they log into the VDI. You don't want the script to run in the VDI. If that's right, here's one idea.
#echo off
if exist "%userprofile%\in_vdi" goto :eof
type nul >"%userprofile%\in_vdi"
start /wait \\ph-vda\C$\VDI_Users_RDP\%username%.rdp /multimon /noConsentPrompt
del "%userprofile%\in_vdi"
Shutdown /l

Automatically connect to wifi network when no internet detected

EDIT: Found a fix without using Task Scheduler.
#echo off
:loop
set addresses=192.168.1.1
for %%a in (%addresses%) do ping %%a -n 1 > nul || netsh wlan add profile filename=C:\User\path\to\wireless\xml\profile.xml user=all
timeout /t 5
goto :loop
it repeats every 5 seconds
I am halfway there but have become stuck. I already have a batch file that can connect the laptop back to the network when manually run but wanted it to run when no internet is detected. So I went ahead and bound it to Task Scheduler with the Trigger being the "Microsoft-Windows-NetworkProfile/Operational" with an ID of 10001 (which is the Disconnect ID) but have no luck with it.
I believe it only triggers when it manually gets disconnected from the internet, not when the wireless card drops the signal on its' own. Any ideas as to what I can do?
OS: Windows 8.1
Wireless Interface: Cable Matters AC600 Dual Band Wireless dongle (doesn't allow Auto Connect to SSID and keeps dropping the connection for unknown reasons)
Found a solution!
#echo off
:loop
set addresses=192.168.1.1
for %%a in (%addresses%) do ping %%a -n 1 > nul || netsh wlan add profile filename=C:\User\path\to\wireless\xml\profile.xml user=all
timeout /t 5
goto :loop
What this does is ping an address once (in this case an internal address so I don't get DDoS threats from sites) and if it responds back with an error, it will load a wifi profile.
To get the profile first you will need to type
netsh wlan show profiles
to see what profiles you have (it only loads profiles for the wireless card currently in use). After seeing the names of the profiles you can download the profile by typing
netsh wlan export profile name=[profile name]
I believe it will save to the desktop and from there you can change the code listed at the top of this comment to match the filepath of the xml file to suit your needs.
Cheers!
What about a batch script that regularly performs pings and you inspect the result?
ping some.host > pinged.txt
find "some string which occurs if the network went down" pinged.txt
If %errorlevel% is 0 than the string was found and the connection went down.

Get IP address on Windows 7 via command line

I have a list of aliases defined in a command file (aliases.cmd) that I load whenver I run a command prompt (cmd.exe /k aliases.cmd). Using doskey, I have several aliases defined and I am trying to create one to show only my IP address. I came across this question with a lot of good ways to get an IP in a batch file but none of them seem to work via doskey. For example, I have this alias defined:
doskey ip=ipconfig | findstr /R /C:"IPv4 Address"
When I run it via the command prompt (excluding the doskey portion), it works and only returns the IPv4 address. However, via doskey and the "ip" keyword, it does not work and returns the regular output of "ipconfig".
Is there any way to get an IP address (and only the IP) on Windows via a doskey alias?
So after some study, the only way I could get this to work is the following.
doskey ip=ip.cmd
And ip.cmd has the following.
#echo off
ipconfig | findstr /R /C:"IPv4 Address"
I believe your problem was that you did not escape the pipe character, as with: doskey ip=ipconfig ^| findstr /R /C:"IPv4 Address"

Why does batch file FOR fail when iterating over command output?

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.

Resources