我知道如何从CLI连接到Hyper-V虚拟机:
vmconnect localhost 'machine'
我也知道如何非互动地开始他们,我通过这样的快捷方式做到这一点:
powershell.exe -ExecutionPolicy Bypass -Command "& {Start-VM -Name 'machine'}"
但是我们怎么能把这两个动作结合在一起呢? 我根本不想打开Hyper-V控制台。 我试过这个,但没有奏效:
powershell.exe -ExecutionPolicy Bypass -Command "& {Start-VM -Name 'machine' & vmconnect localhost 'machine'}"
它抛出:
At line:1 char:36 + & {Start-VM -Name 'machine' & vmconnect localhost 'machine ... + ~ The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double quotation marks ("&") to pass it as part of a string. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : AmpersandNotAllowed
另外,在我看来,开始和连接之间应该等待一段时间,因为这样的命令的立即顺序不会被成功执行。
缺less两件事情:在启动虚拟机之前必须导入Hyper-V模块, &是一个调用操作符而不是命令分隔符。
powershell.exe -ExecutionPolicy Bypass -Command "ipmo hyper-v; Start-VM machine; vmconnect localhost machine"
我已经testing了上述在Windows 10 1703上的工作。