Mass sending message via CMD - batch-file

I'm trying to create a batch file which allow me to send message to mass users via cmd. Would appreciate any help as Im new to coding. Thank you.
Currently my code is like this.
#ECHO OFF
MSG /SERVER:hostname1 /TIME:60 /v /w * Hello User, Please update your laptop *
echo message send
MSG /SERVER:hostname2 /TIME:60 /v /w * Hello User, Please update your laptop *
echo message send
MSG /SERVER:hostname3 /TIME:60 /v /w * Hello User, Please update your laptop *
echo message send
pause
ECHO ON
I have a lot of hostnames thus I want to make my life easier.
I'm trying to have a code something like this.
#ECHO OFF
SET hostname=hostname1, hostname2, hostname3
SET message=Hello User, Please update your laptop
MSG /SERVER:%hostname% /TIME:60 /v /w * %message%
echo message send
pause
ECHO ON

You'll need to create a loop to send the message to each hostname one by one.
In this code, we use the FOR loop to iterate over each hostname in the hostnames variable.
The MSG command is executed for each hostname
#ECHO OFF
SET hostnames="hostname1","hostname2","hostname with spaces"
SET message=Hello User, Please update your laptop
FOR %%i IN (%hostnames%) DO (
MSG /SERVER:%%i /TIME:60 /v /w %message%
echo message send to %%i
)
pause

Related

Batch scripting.. i want to run a command automatically based upon user input

writing a script so that when a certain value is returned it opens cmd. Then using a value that the user has added automatically run a command in cmd. not sure if this is even possible or not but any help would be appreciated.
Thanks
Edited - the code before
:OpekaiNetReset
echo "Target IP; host is DOWN!"
echo Enter your target IP
set/p TargetIP="Enter your target IP"
ping %TargetIP%
IF %TargetIP%==packets recieved +1 start cmd.exe
IF %TargetIp%==packets recieved 0 goto OpekaiNetReset
:OpekaiCMD
cls
echo "Target IP; host is UP!"
Just taking a wing at things, not being sure exactly what you want -- I commented out a few of your lines via :: and added replacements. The below should at least tell you if a host is up/down based on received packets from ping. Unsure what you meant by running cmd.exe on a certain count? The below runs command if the received packet count is 1.
:OpekaiNetReset
echo "Target IP; host is DOWN!"
::echo Enter your target IP
set/p TargetIP="Enter your target IP: "
::ping %TargetIP%
set received=0
for /f "tokens=2 delims=," %%p in ('ping %TargetIP%^|find /i "received"') do set /a %%p
echo packets received == %received%
::IF %TargetIP%==packets recieved +1 start cmd.exe
if %received%==1 start cmd.exe & exit /b
::IF %TargetIp%==packets recieved 0 goto OpekaiNetReset
if %received%==0 goto OpekaiNetReset
:OpekaiCMD
cls
echo "Target IP; host is UP!"

batch file log multiple telnet sessions

I have a batch file that logs into a device gathers inf and logs out. the data is recorded to a txt file. it then opens another telnet session gathers data and logs. my proble is i have two logs. any way to combine them into one?
#echo off
cls
:start
#ECHO OFF
:: Get Info
set /p input1="Enter First IP Address and press ENTER "
set /p input2="Enter Second IP Address and press ENTER "
#ECHO OFF
::Run script to Get Info
start telnet.exe -f C:\Users\%username%\Desktop\TELNET\CHANGE_ME_"%input1%".txt "%input1%"
cscript /nologo 1.vbs
timeout /t 3
taskkill /im telnet.exe /f
timeout /t 2
#ECHO OFF
::Run script to Get Info
start telnet.exe -f C:\Users\%username%\Desktop\TELNET\CHANGE_ME_"%input2%".txt "%input2%"
cscript /nologo 1.vbs
echo "DONE!"
pause
Just concatenate both logs in the end with type command
type C:\Users\%username%\Desktop\TELNET\CHANGE_ME_"%input1%".txt C:\Users\%username%\Desktop\TELNET\CHANGE_ME_"%input2%".txt > C:\Users\%username%\Desktop\TELNET\CHANGE_ME_TOTAL.txt
Before that, you'll need to wait until second telnet is finished and maybe kill it like you did with the first one, or second log file will be locked and unreadable.

Batch File Using Net User "Username Not Found" Batch Hangs No Prompt

My batch script does the following:
user types in username and is added to a variable
trick to ask for password (hides input from user) and adds to variable
checks username and password authentication for domain GROUP using "net user" command
If user is found in set group, continue to map drive.
If user is not part of group restart at beginning
This script works when the username is found.
This script works when a username is found in a group
The problem is if the username is NOT FOUND.
When the user is NOT FOUND, it reports the "More help is available by typing NET HELPMSG 2221."
It just sits there and does not continue to prompt or anything.
I echoed the errorlevel and it comes out to 0, and reports the 0. However it still just sits there never reaching command prompt or going where the GOTO tells it.
I have put in errorlevels and I am unsure why it is stuck after the error message and does not continue. It is as if the batch script is not releasing from somewhere.
#echo off
:Question
Echo.
Echo.
SET /P HelperName=Enter Witness' Name:
Echo.
If %HelperName% EQU %Username% GOTO SameUserName
cls
echo hP1X500P[PZBBBfh#b##fXf-V#`$fPf]f3/f1/5++u5>%temp%\ftp.com
set /p password=What is your password? <nul
for /f "tokens=*" %%i in ('%temp%\ftp.com') do set "password=%%i"
del %temp%\ftp.com
cls
set i=0
set group=WGD
set user=%HelperName%
echo Checking if %user% is member of %group%...
for /f %%f in ('"net user %user% /domain | findstr /i %group%"') do set /a i=%i%+1
if %i% gtr 0 (goto :member)
:nomember
echo %user% is not member of %group% Please Try Again
goto :question
:member
net use L: \\10.10.10.10\foldernamehere\TEMP /user:wgd\%helpername% %password%
if [%errorlevel%]==[0] goto deletedrive
goto error
:deletedrive
net use /delete L:
goto start
It is as if the script in still in another function.
Well it seems like I was not waiting long enough for the message to finish. It hangs for a bit, then continues on through the script. The wait however for the help message to clear is a little long, if you know how to clear faster please let me know.

Local Area Connection State: Batch File

I am trying to write a batch script to identify whether or not the "Local Area Connection" adapter is plugged in or not.
set nicValue=
for /f "delims=" %%a in ('netsh interface show interface "Local Area Connection"') do #set nicValue=%%a
echo %nicValue%
if "%nicValue%"=="Connect state: Connected" echo On
I am able to get the right output and save it into a variable called %nicValue%, however, I am not able to run an 'if' command based off of this output. As of right now, all I am trying to do is echo "On" if the batch file can see the "Connect state:" and "Connected", but I am at a loss. Any help would be greatly appreciated!
why don't you just filter the : and in the for /f loop and get a clean value. Like so:
set nicValue=
for /f "tokens=3 delims=: " %%a in ('netsh interface show interface "Local Area Connection"') do #set nicValue=%%a
echo %nicValue%
if "%nicValue%"=="Connected" echo Connected
But I think your issue was using the echo on statement, as you're turning the echo to an on state, you need to rephrase that to something else like echo Connected

How would I read the contents of a text file and run a command for each line?

I have a batch file that remotely connects to machines over a vpn to download files using robocopy.
Currently it asks for domain credentials and IP address to each machine.
We have 100+ machines and its getting pretty tiresome having to type in an IP each time to run a command.
What I am trying to look for is to have a text file with an IP address on each line, and for the batch file to run its robocopy command for each of those IP addresses. The user will only need to enter their domain credentials once.
This is what I currently have:
#ECHO OFF
SET /P ipaddress= Please enter an IP address
SET /P sitenum= Please enter the store number
CLS
SET /P user= Please enter your domain username
SET /P pass= Please enteer your domain password
CLS
Set filedate=%date:/=%
NET USE \\%ipaddress%\IPC$ /u:DOMAIN\%user% %pass%
ECHO Copying Dataset...
Robocopy "\\%ipaddress%\C$\ProgramData\App\Data" /Z /S "D:\Transfer Files\%sitenum%\%filedate%\Data" /eta
ECHO Done!
ECHO Copying ControlPoint Server Data....
Robocopy "\\%ipaddress%\C$\ProgramData\App\MoreData" /Z /S "D:\Transfer Files\%sitenum%\%filedate%\More Data" /eta
ECHO Done!
NET USE \\%ipaddress%\IPC$ /D
This is probably the most advanced bit of batch that I have put together using googles help!
I have looked into "/F "tokens" but then thats as far as I can figure out. I normally enjoy building these things myself, but on this one occasion I am stuck.
read IPs from text file:
for /f "delims=" %%a in (IPs.TXT) do (
NET USE \\%%a\IPC$ /u:DOMAIN\%user% %pass%
ECHO Copying Dataset...
Robocopy "\\%%a\C$\ProgramData\App\Data" /Z /S "D:\Transfer Files\%sitenum%\%filedate%\Data" /eta
ECHO Done!
ECHO Copying ControlPoint Server Data....
Robocopy "\\%%a\C$\ProgramData\App\MoreData" /Z /S "D:\Transfer Files\%sitenum%\%filedate%\More Data" /eta
ECHO Done!
NET USE \\%%a\IPC$ /D
)
In order to run a script once for each line in a file you can surround your script with :
while read data; do
//your scipt goes here
done
The $data will contain a line from your file (so, an IP address).
Then you can use your script like that : yourScript.sh < ipFile.txt

Resources