0

Basically im trying to open and close a virtual machine from windows 10 pro task scheduler, but im not able. Start script

SET maxvalue=200
SET minvalue=0

SET /A rand=((%RANDOM%)%%(%maxvalue%))+(%minvalue%)
ping 127.0.0.1 -n %rand% > nul

cmd.exe /C "C:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe" -T ws start 5.vmx nogui

Stop script

SET maxvalue=200
SET minvalue=0

SET /A rand=((%RANDOM%)%%(%maxvalue%))+(%minvalue%)
ping 127.0.0.1 -n %rand% > nul
"C:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe" stop 5.vmx

im not even able to start it, the script if i open it by myself it works, but in task scheduler it dosnt. i run it with highest permissions.

both scripts are in the same path thath the virtual machine file (where .vmx) is.

TylerH
  • 20,799
  • 66
  • 75
  • 101
  • 1
    Hard to give you any feedback without knowing all the settings for the scheduled task. Please update your question with that information – Squashman Jan 25 '19 at 18:02
  • what useful information i can put about the task? or exported as xml is good? – Gnubie_Host Jan 25 '19 at 18:43
  • What is the purpose of pinging local machine with a random number? That seems to be completely useless. Starting a scheduled task and waiting a random number does not make sense in my point of view. Second line is useless on using `SET /A rand=%RANDOM% %% maxvalue`. Well, `ping` with `-n 0` is not valid. So better use: `SET /A rand=%RANDOM% %% maxvalue + 1`. `cmd.exe /C` is also not really useful in my point of view. – Mofi Jan 28 '19 at 06:57
  • Now let us look on real problem which could be found out by yourself after reading [What must be taken into account on executing a batch file as scheduled task?](https://stackoverflow.com/a/41821620/3074564) The file `5.vmx` is searched by `vmrun.exe` in current directory because of specified without any path. The scheduled task property __Start in__ defines the current directory on starting `cmd.exe` for execution of the batch file which does not contain the command `cd /D "C:\Path to vmx file"` to set a different directory as current directory as initial start in directory. – Mofi Jan 28 '19 at 07:03
  • So the default `%SystemRoot%\System32` is used as current directory with __Start in__ not defined in properties of scheduled task. I doubt that the Windows system directory contains the files of the virtual machine. For that reason `vmrun.exe` cannot start a virtual machine and exits with an error message. What might work is: `start "" /D"C:\Path to vmx file" "C:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe" start 5.vmx nogui` to start the virtual machine as separate process with directory of virtual machine as current directory and exit batch file execution after starting the VM. – Mofi Jan 28 '19 at 07:07
  • For stopping the virtual machine no batch file is needed at all. The scheduled task to stop simple runs `"C:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe"` with the parameters `stop 5.vmx` and __Start in__ is defined with the directory containing the file `5.vmx`. That's it. – Mofi Jan 28 '19 at 07:09
  • its solved, thank you guys for all your comments. – Gnubie_Host Jan 29 '19 at 22:56

1 Answers1

0

I solved it. It was that easy to put the full path of the *.vmx file,

The point of randomizing when opening is that I want a random interval between virtual machine start, that's all.

https://communities.vmware.com/thread/588283

Mofi
  • 46,139
  • 17
  • 80
  • 143