Batch File execution on remote windows server via batch file on another windows server - windows

I am trying to call a batch file placed on a remote windows server from my windows server. I have freeSSHd installed on the remote server. I tried using putty/plink but of no result.
Syntax of command I am using is:
Putty: start E:\putty.exe -ssh user12#serverIP -pw "abcd12" call sample.bat
Plink: start E:\plink.exe -ssh user12#serverIP -pw "abcd12" cmd.exe -m E://sample.txt
I even tried all variants of above two commands like "-m file.txt" etc. Also I am able to execute batch file via login into putty console but not via calling batch file placed on my windows server.

This is how to start remote programs in Windows.
wmic /node:servername process call create "cmd.exe c:\\somefolder\\batch.bat"
Files executed this way are invisible on the remote computer.
For Help
wmic /?
wmic process /?
wmic process get /?
wmic process set /?
wmic process call /?
wmic process call create /?
wmic process /node /?
wmic process /user /?
wmic process /password /?

Related

How to delete the contents of temp folder in remote machine through command

I have a requirement where i need to delete the contents of the temp folder on number of remote windows machines.
i tried below wmic command to delete
WMIC /node:...** /user:xxxx /password:xxxx path cim_datafile WHERE "path='%Winddir%\temp*.tmp' AND Extension ='tmp'" delete**
But it throws an error saying
ERROR:
Description = The RPC server is unavailable.
Please suggest me a way to delete temp folder contents on remote machine.
Most simple way - if you're not strictly forced to use WMI, you can do that with psexec:
PsExec.exe \\<computer> cmd /c "rmdir /s /q %windir%\temp\"
or, if you're not logged into domain admin or global workstations admin account -
PsExec.exe -u <user> -p <password> \\<computer> cmd /c "rmdir /s /q %windir%\temp\"
If should not completely delete the folder since Windows keeps some files open and locked inside it, but anything not read-only or locked would be deleted, both files and folders.
Then, to do this with a list of computers, just use a cmd file like this (list.txt contains just computer names/ips, without \:
for /F %%s in (list.txt) do (
echo %%s
start "" /min PsExec.exe \\%%s cmd /c "rmdir /s /q %windir%\temp\"
)
Start command makes all psexec processes run in parallel and minimized so you don't have to wait for each computer to finish before starting another
psexec.exe is a part of sysinternals package from M. Russinovich, I'd like to leave a direct link but don't know if it's permitted or not.
I think it should be able to connect to remote pcs if you have access to file shares and remote computer management (and admin rights of course)

How to only run WMIC if it has already been installed?

I am trying to run a batch file that includes some WMIC queries on multiple versions of Windows. Windows 2003 causes the script to hang. It is most likely due to the first time that wmic is being run. The computer will normally output "Please wait while WMIC is being installed.."
Is there anyway to check if wmic is installed and if it is not, do not run it? I do not want to install WMIC on the computers I am running this on if it is not already installed. Should I just skip this query on all Windows 2003?
May be I'm wrong, but I think wmic is present at least from XP
This may help
#echo off
where /R c:\windows\ wmic.exe >nul 2>nul || (echo/ wmic.exe not found & exit/B)
rem wmic queries here
exit/B

WMIC open file or URL

Using cmd.exe, you can open a file or URL in the user’s preferred application.
start example.txt
start http://example.com
I see that WMIC can start a program
wmic process call create notepad.exe
However, can it open a file or URL, like start?
Start - Windows CMD
This won't open the 'favored' browser, but you can get to a browser without the need for start.
wmic process call create "C:\Program Files\Internet Explorer\iexplore.exe www.google.com"
wmic process call create "cmd /c start http://example.com"
start is an internal command of cmd.exe, so, you need to execute cmd.exe to reach start command

Terminate process SILENTLY using VBScript

I am simply trying to kill a process using a batch that calls a vbscript, but i need the vbscript to do it silently. Or can this not be done without UAC prompting for privileges?
Used WMIC instead
wmic process where name="wscript.exe" call terminate > nul
You can TASKKILL a process with PID on a remote system, using different user credentials, and TASKLIST to list them.
At a command prompt type taskkill /? or this taskkill on technet. Using PID will prevent terminating a wrong process with the same. For example, a computer with both 2007 and 2010 versions of office installed and only the 2007 version needs terminating.
See tasklist /? or this tasklist on technet.
You can even combine PSEXEC to fill the gap which TASKLIST cannot do remotely.
To run a script or application in the Windows Shell using the ShellExecute method (cf msdn.microsoft.com):
Syntax
.ShellExecute "application", "parameters", "dir", "verb", windowFlag
Key
application The file to execute (required)
parameters Arguments for the executable
dir Working directory
verb The operation to execute (runas/open/edit/print)
windowFlag View mode application window (normal=1, hide=0, ...)
You can try this:
Set denyUAC = CreateObject("Shell.Application")
denyUAC.ShellExecute "cscript", "D:\demo\vbscript.vbs", "", "runas", 0

How to stop a service in cmd only knowing the name of the .exe file?

I need to stop a windows service in a batch file without knowing the name of the service. The only thing I know is that the file running is called SomeServer.exe but the SC command requires the actual name of the service.
Currently I have to scan a config file and perform ugly string operations but I hope there is a smarter way.
Any suggestions?
for /f "tokens=2 delims=," %%a in (
'wmic service get name^,pathname^,state /format:csv ^| findstr /i /r /c:"SomeServer\.exe.*Running$"'
) do sc stop "%%a"
It retrieves the system name, service name, path name and state of the services in csv format. The list is filtered for the required executable name in Running state, splitted using the comma as separator, and the second field (the service name) is used to stop the service
this may be helpfull
http://richarddingwall.name/2009/06/18/windows-equivalents-of-ps-and-kill-commands/
If you’ve ever used Unix, you’ll no doubt be well-aquainted with the
commands ps and kill. On Windows, the graphical Task Manager performs
these roles pretty well, but if you ever find yourself needing to
resort to the command line to kill a process (e.g. for some reason on
the Vista machine I am writing this on Task Manager just sits in the
system tray flashing instead of opening), the Windows equivalents of
ps and kill are tasklist and taskkill:
tasklist /v - equivalent to ps aux
taskkill /f /im ncover* - equivalent to kill -9 ncover*
and there is also pslist http://technet.microsoft.com/en-us/sysinternals/bb896682.aspx
edit:
for services use psservice http://technet.microsoft.com/en-us/sysinternals/bb897542.aspx
or use the method described here (using the registry) https://stackoverflow.com/a/298823/1342402

Resources