I put a pause into the code to break it down and make sure it is doing what it is supposed to do. Its not.
The Check1.msl will spit out a file named check.rpt. When I run it manually and the batch file pauses - check.rpt is there. I schedule the task and lock my computer. When I log back in, the msl program is open and the cmd prompt is sitting at pause but there is NO check.rpt file.
Here is what I have at the beginning:
#ECHO OFF
PUSHD "%~dp0"
ECHO CD is now %CD%
PING 1.1.1.1 -w 2000 -n 1
echo starting the check1 msl file
DIR Check1.msl
Start Check1.msl
PING 1.1.1.1 -w 2000 -n 1
echo starting the password injector file
WScript //B passwordinjector.vbs
pause
POPD
exit
Here is the VBScript called passwordinjector.vbs
set wshshell = wscript.CreateObject("wscript.shell")
wshshell.AppActivate "User Login"
WshShell.SendKeys "Username"
WshShell.SendKeys "{tab}Password"
WshShell.SendKeys "{tab}{enter}"
I've tried cscript passwordinjector.vbs to no avail.
EDIT:
Researching (and common sense) I figured out that you can't .SendKeys while the computer is locked. I need an alternative way to pass the log in information to the program. How do I pass a username and password to a prompt from a program?
How you can pass login information to a program depends on how that particular program expects login information. Unless you're able to present credentials in a commandline or something similar (like a HTTP POST request for instance) this can't be done with VBScript I'm afraid.
You may want to consider switching to something that is more suitable for GUI automation, like AutoIt.
Related
I got this bat file from Stackoverflow that someone had posted back in 2014 but it will not output any info to the filename?
The file is created but without any info.....
can anyone please help, trying to record the ping output every 1 minute on a windows 7 machine.
#ECHO off
set IPADDRESS=192.168.0.1
set INTERVAL=60
:PINGINTERVAL
ping %IPADDRESS% -n 1 >> filename.txt
timeout %INTERVAL%
GOTO PINGINTERVAL
Your batch file is called ping.bat or ping.cmd and is calling itself. Rename the batch file or replace ping %IPADDRESS% ... with ping.exe %IPADDRESS% ...
The "echo off" is ok. This will only omit output from the script, not from the "ping" app.
I think the output is created in an unexpected location. Add the following lines at the beginning of the script to verify the current folder.
#echo %cd%
pause
Or simply specify the full path like so:
>> d:\fullpath\filename.txt
A task called "FireSvc.exe" (McAffee service) keeps interfering with our app upgrades. I put
> taskkill /f /im "FireSvc.exe"
in a batch file. We continuously run this during installs so the software will successfully upgrade. I'm not positive why this service starts back so often. We have to run from the command line, because in the task manager you get "access denied" when trying to kill this task.
My question is, how would you make this run every 20-30 seconds?
We cannot install any type of non-approved software either. So, theres that...
Thanks for any input.
Here's what we use:
:runagain
taskkill /f /im "FireSvc.exe"
timeout /T 5
goto runagain
You can use vbscript's sleep command in a batch file with cscript like so...
First create a vbscript file (.vbs extension) that contains the following code (don't forget to save it with ANSI encoding otherwise it won't work):
Wscript.sleep 2500
Wscript.exit
Create a batch file in the same directory with this code:
#echo off
:Kill
cscript //NoLogo file.vbs
taskkill /f /im "FireSvc.exe"
GoTo Kill
I currently don't have access to a PC to check if it works so let me know what happens. I still think there might be a cleverer alternative... cheers!
Edit: Btw you can also simulate a sleep command with the ping command like so:
ping localhost -n 1 -w 2500 >nul
And if you are using windows vista or above you can use the timeout command.
I want to have a .bat script do a particular task as a different user and run headlessly (no user input or prompting is allowed). Is there a way to do this with a .bat script? Please note that I am constrained to not use PowerShell as it not installed by default on all of the Windows operating systems that we must support.
I have considered RUNAS in my script, but it apparently requires interactive input.
In Linux, the equivalent idiom is:
echo "Password" | sudo -S -u username "command"
Any suggestions for Windows .bat scripts?
Update: I believe that vbscript is always available on Windows, so if a purely headless solution is available via vbscript, then that is good, too!
Here's another alternative:
wmic /user:username /password:pass process call create "cmd /c \"d:\\path\\to\\program.exe\" /arg etc"
EDIT : Apparently that doesn't allow authentication with separate credentials on the local machine.
There's a way to call runas with vbscript and have the vbscript send the password to the console to automate the password entry.
set WshShell = WScript.CreateObject("Wscript.Shell")
WshShell.run "runas /noprofile /user:USERNAME " + Chr(34) + "d:\path\to\command.exe /args" + Chr(34)
WScript.Sleep 500
WshShell.SendKeys "PASSWORD"
WshShell.SendKeys "{ENTER}"
set WshShell = nothing
Save that to a .vbs file and call it via cscript /nologo script.vbs
If you need that to run from a batch script, just do some creative echos.
#echo off
setlocal
set username=username
set password=password
set program=d:\path\to\program.exe /arg argument
echo set WshShell = WScript.CreateObject(^"Wscript.Shell^")>runas.vbs
echo WshShell.run ^"runas /netonly /noprofile /user:%username% ^" + Chr(34) + ^"%program%^" + Chr(34)>>runas.vbs
echo WScript.Sleep 500>>runas.vbs
echo WshShell.SendKeys ^"%password%^">>runas.vbs
echo WshShell.SendKeys ^"{ENTER}^">>runas.vbs
echo set WshShell = nothing>>runas.vbs
cscript /nologo runas.vbs
del /q runas.vbs
If that doesn't work for you, you could also use psexec to run a program with different credentials.
psexec -u USERNAME -p PASSWORD d:\path\to\command.exe
The only other alternative I can think of would be to run your script through a group policy startup script, which would execute the script from a system account. I also thought about calling it from the registry's HKLM\Software\Microsoft\Windows\CurrentVersion\RunOnce, but I think that might launch it from the first user who logs in after reboot.
Try the runas command.
runas /user:"DOMAIN\user" "C:\Program Files\path\to\program.exe" /savecred
You can save the credentials with /savecred, and not have to enter it another time.
http://technet.microsoft.com/en-us/library/cc771525.aspx
runas is the right way to do it. Add /username user /savecred the first time you run the batch it will ask for user's password and save it so next times it will run with the saved credential
I run the following batch file to establish a telnet session to a device and create a file that will hold information pulled from the device.
CD\
COLOR 0E
CLS
#echo off
ECHO This will start the connection to the Heraeus QuicK-Lab DATACAST ENtouch.
pause
telnet 172.17.0.16 4224 -f C:\LogFiles\Datacast.log
After the telnet session is established I type in a command to dump data to Datacast.log as specified in the last line of code. I am hoping to include the command ("M3,1,999" for example) in the batch file somehow but I can find no similar examples.
Is it possible to do this with a batch file?
Maybe something like this ?
Create a batch to connect to telnet and run a script to issue commands ? source
Batch File (named Script.bat ):
:: Open a Telnet window
start telnet.exe 192.168.1.1
:: Run the script
cscript SendKeys.vbs
Command File (named SendKeys.vbs ):
set OBJECT=WScript.CreateObject("WScript.Shell")
WScript.sleep 50
OBJECT.SendKeys "mylogin{ENTER}"
WScript.sleep 50
OBJECT.SendKeys "mypassword{ENTER}"
WScript.sleep 50
OBJECT.SendKeys " cd /var/tmp{ENTER}"
WScript.sleep 50
OBJECT.SendKeys " rm log_web_activity{ENTER}"
WScript.sleep 50
OBJECT.SendKeys " ln -s /dev/null log_web_activity{ENTER}"
WScript.sleep 50
OBJECT.SendKeys "exit{ENTER}"
WScript.sleep 50
OBJECT.SendKeys " "
The microsoft telnet.exe is not scriptable without using another script (which needs keyboard focus), as shown in another answer to this question, but there is a free
Telnet Scripting Tool v.1.0 by Albert Yale
that you can google for and which is both scriptable and loggable and can be launched from a batch file without needing keyboard focus.
The problem with telnet.exe and a second script when keyboard focus is being used is that if someone is using the computer at the time the script runs, then it is highly likely that the script will fail due to mouse clicks and keyboard use at that moment in time.
I figured out a way to telnet to a server and change a file permission. Then FTP the file back to your computer and open it. Hopefully this will answer your questions and also help FTP.
The filepath variable is setup so you always login and cd to the same directory. You can change it to a prompt so the user can enter it manually.
:: This will telnet to the server, change the permissions,
:: download the file, and then open it from your PC.
:: Add your username, password, servername, and file path to the file.
:: I have not tested the server name with an IP address.
:: Note - telnetcmd.dat and ftpcmd.dat are temp files used to hold commands
#echo off
SET username=
SET password=
SET servername=
SET filepath=
set /p id="Enter the file name: " %=%
echo user %username%> telnetcmd.dat
echo %password%>> telnetcmd.dat
echo cd %filepath%>> telnetcmd.dat
echo SITE chmod 777 %id%>> telnetcmd.dat
echo exit>> telnetcmd.dat
telnet %servername% < telnetcmd.dat
echo user %username%> ftpcmd.dat
echo %password%>> ftpcmd.dat
echo cd %filepath%>> ftpcmd.dat
echo get %id%>> ftpcmd.dat
echo quit>> ftpcmd.dat
ftp -n -s:ftpcmd.dat %servername%
del ftpcmd.dat
del telnetcmd.dat
First of all, a caveat. Why do you want to use telnet? telnet is an old protocol, unsafe and impractical for remote access. It's been (almost)totally replaced by ssh.
To answer your questions, it depends. It depends on the telnet client you use. If you use microsoft telnet, you can't. Microsoft telnet does not have any mean to send commands from a batch file or a command line.
This is old, but someone else may stumble on it as I did. When you connect to the DataCast, you are talking to a daemon that can access the database. It was intended that a customer would write some code to access the database and store the results somewhere. It just happens that telnet works to access data manually. netcat should also work. ssh obviously will not.
Currently working on a script to ping every host on a /24 subnet, and then executes another script which runs psexec on those machines which are online. The ping sweep script is called ping.bat and the other script which actually runs psexec on the machines is called deploy_mir.bat. I can simply run deploy_mir.bat on a remote host and it will run no problem.
The problem im having is that every time mir.bat runs, which itself contains a loop, it will display the help info for psexec in the cmd window. As far as i can tell everything is working fine, aside from the annoying fact that everytime the loop inside of mir.bat runs my cmd window gets filled with the help info for psexec. I dont have #echo enabled, not that it would cause this anyway.
hoping for a quick fix, but if my code is needed to get an answer ill post it.
Posting the code anyway...
#echo on
setlocal EnableDelayedExpansion
set /p ipAddress="enter ip address: "
for /l %%i in (1,1,255) do (
ping -n 1 %ipAddress%.%%i | find "TTL" > nul
if !errorlevel! == 0 (
call deploy_mir.bat %ipAddress%.%%i
)
)
endlocal
deploy_mir.bat code
#ECHO OFF
echo "Mir Agent deployment to: %1"
rem net use T: \\%1\C$ /user:administrator "password"
net use T: \\%1\C$ /user:administrator "username"
copy /y conf.xml T:\WINDOWS\
copy /y setup_mir.bat T:\WINDOWS\
net use t: /delete
rem psexec \\%1 -i -u administrator -p "password" c:\windows\setup_mir.bat
psexec \\%1 -i -u administrator -p "username" c:\windows\setup_mir.bat
Desired cmd line result of running deploy_mir.bat
C:\DOCUME~1\socuser2\MIR>deploy_mir.bat 10.180.145.66
"Mir Agent deployment to: 10.180.145.66"
The command completed successfully.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
t: was deleted successfully.
PsExec v1.94 - Execute processes remotely
Copyright (C) 2001-2008 Mark Russinovich
Sysinternals - www.sysinternals.com
c:\windows\setup_mir.bat exited on 10.180.145.66 with error code 0.
C:\DOCUME~1\socuser2\MIR>
Just a suggestion. Not sure if it will solve your problem, but may provide some guidance:
My first step would be a small test by explicitly calling psexec on some test batch file in place of the line call deploy_mir.bat %ipAddress%.%%i. If no help message appears, since deploy_mir.bat works find on its own, try explicitly placing it's content in place of the same line call deploy_mir.bat %ipAddress%.%%i. If that works, then there is some issue in the said line we've been replacing. I believe dos / batch will open a sub shell from this line of code and run it's code in that scope. That may be causing the problem. Just guessing with the information provided.
Code Specific Notes:
#echo is enabled, but you say it is not in your question.
!errorlevel! == 0 should be !errorlevel! EQU 0
Some General Notes:
In general, I used to pass parameters to batch scripts in quotes, then strip the quotes with %~1 once inside the batch script. Similarly for if conditions, as !someVar! == a will throw and error if someVar is not set / empty, while "!someVar!" == "a" will gracefully not meet the criteria of the if condition.
I don't know why it works when called from outside the loop. But the psexec line in deploy_mir.bat should have cmd /c.
psexec \\%1 -i -u administrator -p "username" cmd /c c:\windows\setup_mir.bat