如何使用“开始”命令在Windows“ cmd”上使用命令行参数启动程序?


33

我需要使用startWindows 7命令行上的命令在后台启动程序(虚拟机)。通常,您会这样:

start /b cmd yourprogram

但是我需要传递一些参数,并且在需要时传递这样的信息(没有/b标志以查看调试信息):

start C:\Users\USER>start "c:\Program Files\Oracle\VirtualBox\VBoxHeadless.exe" -startvm "debian604 64"

我收到此错误消息:

Windows无法找到“ -startvm”。请确保您键入正确的名称,然后再试一次。

另一方面,当我在当前命令行窗口中执行此操作start时,开始时虚拟机未正常运行-但在前台运行。

有什么办法吗?

Answers:


36
start /b "" "c:\Program Files\Oracle\VirtualBox\VBoxHeadless.exe" -startvm "debian604 64"

如果使用以下命令读取参数列表start /?:

START ["title"] [/D path] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED]
      [/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL]
      [/NODE <NUMA node>] [/AFFINITY <hex affinity mask>] [/WAIT] [/B]
      [command/program] [parameters]

    "title"     Title to display in window title bar.
    command/program
                If it is an internal cmd command or a batch file then
                the command processor is run with the /K switch to cmd.exe.
                This means that the window will remain after the command
                has been run.

                If it is not an internal cmd command or batch file then
                it is a program and will run as either a windowed application
                or a console application.

    parameters  These are the parameters passed to the command/program.

期望用title引号(")引起来。由于您的程序路径包含引号,因此将其解释为标题。添加显式标题(在这种情况下为空"")是可行的。


另一种方法是使用/d开关指定路径。特别:

start /b /d "c:\Program Files\Oracle\VirtualBox\" VBoxHeadless.exe -startvm "debian604 64"

/d即使使用引号,它似乎也将开关后的第一个参数用作路径,并且如果未引用下一个参数,则此方法有效。被识别为命令/程序之后的所有内容均作为参数传递给该命令/程序。请注意,如果命令/程序的名称中带有空格(例如)VBox Headless.exe,则此方法将不起作用,因为这将需要使用引号并将其识别为标题。


总体而言,第一种(显式标题)方法可能更好。对于Microsoft而言,这是一个错误的设计选择,他们确实应该为标题添加一个开关,而不是“引号中是否包含第一个参数?”。


非常感谢:)尽管我知道start /b不要将虚拟机置于后台。然后,我不得不提出其他建议。
— Patryk

如果只想取消输出(stdout),请>nul在末尾添加a 。最后使用>nul 2>nul可以同时抑制正常输出和错误(stderr)输出。但是,命令提示符窗口必须保持打开状态。
— 鲍勃

1
@Patryk如果您不介意使用PowerShell,则此命令将打开一个无窗口的进程,该进程未与生成(powershell.exe)进程连接。因此,可以关闭PowerShell窗口,并且VBoxHeadless将继续运行。PowerShell随Windows 7 Start-Process -FilePath 'C:\Program Files\Oracle\VirtualBox\VBoxHeadless.exe' -ArgumentList '-startvm "debian604 64"' -WindowStyle Hidden
— Bob

3

实际上,公认的答案仍然不是解决方案。关闭在其中执行命令的cmd窗口将杀死其中正在运行虚拟机的vboxheadless进程。

使用以下方法将使PowerShell运行独立的进程。

在cmd中,运行:

cd "c:\Program Files\Oracle\VirtualBox"
vboxmanage list vms

这将返回类似:

"Webserver LAP" {8748b594-7e2d-4d8d-8785-999940766754}

现在,获取UUID并运行以下命令(仍在cmd中):

powershell start-process 'C:\program files\oracle\virtualbox\vboxheadless' '-s 8748b594-7e2d-4d8d-8785-999940766754' -WindowStyle Hidden

感谢本文的作者。


在我对自己的答案的评论中,我已经提供了“无头运行VBox”的替代方法。我的回答本身就是“用start” 传递参数的问题。
— 鲍勃,

哇!cd节省了我的时间!
— T.Todua 2014年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.