vbs execute from network with credentials - batch-file

I need to run a setup.exe from the local network with credentials.
I was able to do this with a bat file, but the problem is, but i don't want the cmd window showing up when this is executing.
So from what i've read, a good option is to use a vbs script like this, except i can't find a way to use it with credentials.
Set shell = WScript.CreateObject("Wscript.Shell")
shell.Run("\\192.168.0.100\Setup\setup.exe"), 0 , True
Basically what i need is the equivalent of this batch script in vbs:
net use Z: \\192.168.0.100\sh /user:user password /persistent:yes
start /d "Z:\Setup" setup.exe

you may do this:
Set WshNetwork = WScript.CreateObject("WScript.Network")
WshNetwork.MapNetworkDrive "Z:", "\\192.168.0.100\sh", True, user, password
Set shell = WScript.CreateObject("Wscript.Shell")
shell.CurrentDirectory = "Z:\Setup"
shell.Run("setup.exe"), 0 , True
Note: MapNetworkDrive will fail if Z: is already mapped. You will need to handle that.

PSExec is what you want:
https://technet.microsoft.com/en-us/sysinternals/pstools.aspx
Like this:
Set shell = WScript.CreateObject("Wscript.Shell")
shell.Run("psexec.exe -u User -p Password -accepteula \\192.168.0.100\Setup\setup.exe"), 0 , True
Documentation:
https://technet.microsoft.com/en-us/sysinternals/pxexec
Using PsExec
Usage: psexec [\computer[,computer2[,...] | #file]][-u user [-p
psswd][-n s][-r servicename][-h][-l][-s|-e][-x][-i [session]][-c
[-f|-v]][-w directory][-d][-][-a n,n,...] cmd [arguments]
-u Specifies optional user name for login to remote computer.
-p Specifies optional password for user name. If you omit this
you will be prompted to enter a hidden password.
-accepteula This flag suppresses the display of the license dialog.

Related

.bat file to create local user and set password to never expire

I'm trying to create a .bat file that can add a local (non-admin) account and sets the password to never expire. However, when I run it, I don't see the account show up in Computer Management > Local Users and Groups > Users.
My bat file contains the following:
#echo off
net user IT password /add
echo “Adding IT user”
WMIC USERACCOUNT WHERE Name='IT' SET PasswordExpires=FALSE
echo “Setting password to never expire”
I have also tried this (with and without the double quotation marks):
#echo off
start cmd /c "net user IT password /add"
echo “Adding IT user”
start cmd /k "WMIC USERACCOUNT WHERE Name='IT' SET PasswordExpires=FALSE"
echo “Setting password to never expire”
Maybe you don't have Administrator Privileges.
Try to run it as Admin.

Executing "su" with PuTTY Plink from a batch file

I'm having trouble trying to get my batch file to also give me super user access in PuTTY/Plink. I can auto-input the ip and password but I want to also automate it to where it will also give me su access when I run this batch file. So far it runs up to the point where I need to manually enter su but I'm trying to automate that so I tried -m commands.txt and now it just crashes.. Looking for any help here thanks.
this is what I have so far
#echo off
color b
cls
echo What is the IP for the unit?:
set /p IP= :
echo What is the SSH Password for this unit?:
set /p Passw0rd= :
echo What is the Root Password for this unit?:
set /p ro0t= :
start plink.exe -ssh user#%IP% -pw %Passw0rd% -m commands.txt
exit/b
what in the commands.txt:
su
The su gives you a hint what's wrong, for sure. But you cannot see it because you start plink using start in a new console window that immediately disappears. Remove the start, at least when debugging the problem.
Anyway, you most likely get "standard in must be a tty" or a similar message. The su by default needs terminal for the password prompt. The Plink is meant an for an automation, not an interactive use. So it by default does not allocate a pseudo terminal for the session. To force use of the terminal, add -t switch:
plink.exe -t -ssh user#%IP% -pw %Passw0rd% -m commands.txt
A similar question: "Sudo" fails with "sudo requires a tty" when executed from PuTTY command line
As a next step, you will probably want to execute further commands within su. For that see Executing (sudo) subcommands using Plink.
Though note that in general automating su, particularly su with a password, is usually a bad idea: Allowing automatic command execution as root on Linux using SSH.

how to give input to a command line?

i need a help on how to give input to a command prompt using a batch file..
for example-
if i run the command mysql -u root -p to open my mysql server and it asks for password.. how i m gonna give that password to it using a batch file...
pls help me i am little new to it
Just specify the password besides -p in batch file. Eg: if username is Ankit and password is Devil, specify the following in the batch file:
mysql -uAnkit -pDevil
Ask for the input using Set /P, e.g.
Set /P "pwd=Please enter your password: "
You can then use the variable %pwd% in your command string, e.g.
mysql -u root -p %pwd% …
You could also use the same method to request the user name too:
Set /P "usr=Please enter your user name: "
Set /P "pwd=Please enter your password: "
mysql -u %usr% -p %pwd% …
Type Set /? at the Command Prompt for the command's usage information.

Is it possible to run a command headlessly (in a bat script) as another user on Windows?

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

Hiding putty terminal while running in Batch script

I am running my shell script connecting to remote server through putty, in a batch script as:
putty.exe -ssh -2 user#sever.com -pw password -m command.cmd
where, command.cmd contains
cd /path/to/the/script
./name.ksh
It is running 100% correct, as I needed.But here, the putty terminal is appearing while the batch script is running, which I donot want.
Is there any way to hide the putty terminal?
Not possible using batch, use vbscript:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "putty.exe -ssh -2 user#sever.com -pw password -m command.cmd", 0
' 0 => hide

Resources