I create a Win32Process (cmd.exe) on a remote machine using WMI.
C:\Users\ayush_m>wmic /node:10.0.0.0 /user:ayush_m /password:pwd PROCESS CALL Create "cmd.exe"
Executing (Win32_Process)->Create()
Method execution successful.
Out Parameters:
instance of __PARAMETERS
{
ProcessId = 10088;
ReturnValue = 0;
};
The ProcessID returned by WMI (10088) is the PID of the cmd.exe process started at the remote machine. Now i would like to run some other commands such as mkdir or copy or run an exe file using above PID .i.e use the PID that i have above (10088) and run many more commands on that machine.
I basically do not want to create a new Win32Process everytime i run a command. Please suggest any ideas or observations.
Thanks in advance for help.
From what I understand you could be looking for some remote shell - did you try using PsExec? Wmic just spawns a process but doesn't grant any kind of control over it. As RRUZ suggests, create a BAT/VBS file to execute what you need, or if your environment supports PowerShell, have PowerShell do the stuff.
To sum up:
To have an interactive console - use PsExec
To execute a set of tasks (ex. create C:\MyDirectory and put some logs there) - create a BAT/VBS file and call it using wmic just as you did with cmd.exe
PowerShell could certainly be helpful here if you need to automate things. It was created with automation in mind, so take advantage of it :-)
Best regards, AlexP
(I'm not sure if I'm not grave-digging at the moment, the question is quite old what I noticed just now, but still didn't have an answer)
Related
I'm trying to run a command remotely.
Here is what I've tried
wmic /node:"my_server" /user:my_username /password:my_pass process call create "cmd.exe \c dir C:>C:\temp\x.txt"
I can see the process id returned and I see a terminal running on the remote machine with that process id and that process is just stuck and the output x.txt is not generated.
Any idea how to make it work?
Any idea why the process is running but not doing anything?
My goal is to get the output back so it is not necessary to write to a file.
Using windows task scheduler i am running multiple commands, I'll call them task1.bat, task2.bat, and task3.bat . Each one of these scrips runs a different Psexec command (psexec version 2.11).
When running task1.bat, task2.bat, and task3.bat indivdually, these scripts run successfully; however when run in succession, task1.bat will run successfully, then task2.bat and task3.bat will usually fail with the error "Couldnt access servername. Access is denied. The syntax of the command is incorrect".
It seems like an error with Psexec, since when run individually the commands works fine. Is there a way to force Psexec to exit/end before moving onto the next script (besides just putting in a timeout)? It seems like psexec is hung which is causing the next to fail.
The .bat script will run sequentially if you create and run the batch file:
CALL task1.bat
CALL task2.bat
CALL task3.bat
Is it possible to end/stop/pause/freeze a process/program running in background with python without having administrator rights on a Windows 7/8/10? If yes how? If no why?
If it is yours, you can. Read about taskkill, taskmgr and wmic. Call them using os.system. Also, you may use os.kill, like:
os.kill (your-process-id, signal.SIGKILL)
(determining process ID requires reading process table, either parsing tasklist output or doing it via API)
If it is not yours, sorry... It means you should not kill it. But always, there is a way to kill the process - shut down the computer.
That depends.
If you have the rights to end a process running in background, you can do so.
You can execute commands (e.g. for killing) in python like in cmd by using the call method of the subprocess module.
from subprocess import call
call(['command','with','parameters'])
The command to kill a program in Windows is taskkill.
So to kill iexplore.exe by its name u use taskkill /F /IM iexplore.exe in cmd and call(['taskkill','/F','/IM','iexplore.exe']) in python code.
The documentation of call can be found here: https://docs.python.org/2/library/subprocess.html#subprocess.call
Manual of taskkill: https://technet.microsoft.com/de-de/library/bb491009.aspx
Hope this helps ^^
I have scheduled a task in windows server 2008R2 ..I want to run a VBScript so when I setup the task I call the cscript at C:\Windows\System32\cscript.exe and in the arguments section I am passing //nologo //B d:\main\programs\copy.vbs /targets:contents but it is not executing my script ..If I call my script directly in the start program section it works fine but it's not working if I call CSrcipt and pass in arguments the status changes to queued but nothing happens after that..Can someone tell me what I am doing wrong here.
Also another question I have is that can we run 2 programs one after another in one task like when one script is finished I would like to start another script .
Thanks
Put this line at the top of your script and try again:
CreateObject("WScript.Shell").LogEvent 4, "Script running"
Unless you get an Information event with source WSH and event-ID 4, your script isn't running at all. Check the eventlog and the task's History tab for clues as to why that is. Also check the permissions of the script. Is the runas account of the task able to access/run the file? You can check that by starting a CMD instance as that user
runas /user:DOM\USER cmd
and then trying to run the script in that CMD instance.
Also double-check the task settings. As which user is it configured to run? With the user logged on or not logged on? Is "Run with highest privileges" enabled (in case UAC is enabled on your server)?
If the script does produce the abovementioned event that means it's running in principle, but something is going wrong in the process. You need to debug your script.
I have the current scenario to deal with:
I have to schedule the backup of my company's Linux-based server (under Suse Linux) with ARCServe R15 (installed on Windows 2003R2SP2).
I know I have the ability in my backup software (ARCServe) to add pre/post execution scripts to my backup-jobs.
If failure of the script, ARCServe would be specified NOT to run the backup-job, and if success, specified to be run. I have no problem with this.
The problem is, I want to make a windows script (to be launched by ARCServe) for executing a Linux script on the cluster:
- If this Linux-script fails, I want my windows-script to fail, so my backup job in ARCServe wouldn't run
- If the Linux-script success, I want my windows-script to end normally with error code 0, so my ARCServe job would run normally.
I've tried creating this batch file (let's call it HPC.bat):
echo ON
start /wait "C:\Program Files\PUTTY\plink.exe" -v -l root -i "C:\IST\admin\scripts\HPC\pri.ppk" [cluster_name] /appli/admin/backup_admin
exit %errorlevel%
If I manually launch this .bat by double-clicking on it, or launching it in a command prompt under Windows, it executes normally and then ends.
If I make it being launched by ARCServe, the script seems never to end.
My job stays in "waiting" status, it seems the execution code of the linux script isn't returned to my batch file, and this one doesn't close.
In my mind, what's happening is plink just opens the connection to the Linux, send the sript execution signal, and then close the connection, so the execution code can't be returned to the batch. Am I right ?
Is what I want to do possible or am I trying something impossible to do ?
So, do I have to proceed differently ?
Do I have to use PUTTY or CygWin instead of plink ?
Please, it's giving me headaches ...
If you install Cygwin, you could do it exactly like you can do it on Linux to Linux, i.e. remotely run a command with ssh someuser#remoteserver.com somecommand
This command will return with the same return code on the calling client, as the command exited with on the remote end. If you use SSH shared keys for authentication instead of passwords, it can also be scripted without user interaction.