简体   繁体   English

powerCLI从脚本重新启动VM guest虚拟机

[英]powerCLI restart VM guest from script

i'm trying to run batch script which will call ps1 in order to restart VM guest. 我正在尝试运行批处理脚本,该脚本将调用ps1以便重新启动VM guest虚拟机。 it's working when i ran it seperate but the problem is that powerCLI on CMD load without the arguments. 当我单独运行它时它正在工作,但问题是CMD上的powerCLI加载时没有参数。

i have tried run it by steps : 我试着逐步运行它:

echo on 
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noe -c ". \"C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1\" $true"

then on cmd : 然后在cmd上:

connect -viserver -server "serverName" -Protocol https -User "user"-Password "pass"

then : 然后 :

Restart-VM "VMserverName"  -RunAsync -Confirm:$false

it is all works fine separately but when try to combine it all - it is not working. 单独运行都可以,但是当尝试将所有功能组合在一起时-则不起作用。 seems like powerCLI load faster then the console write. 好像powerCLI加载速度快于控制台写入。

i have trying 我有尝试

Start-Sleep -s 10

command but with no success. 命令,但没有成功。

how can i combine all 3 commands above in one file? 如何将以上所有3条命令合并到一个文件中?

To execute PowerShell commands from a cmd, you will have to pass them along using the Command-Switch of from PowerShell. 要从cmd执行PowerShell命令,您将必须使用PowerShell的Command-Switch传递它们。

You could achieve what you want by executing the following command: 您可以通过执行以下命令来实现所需的功能:

powershell -Command "Import-Module VMware.VimAutomation.Core; Connect-VIServer -Server <server> -User <user> -Password <password>; Restart-VM <vm_name>  -RunAsync -Confirm:$false"

This is a very cumbersome way of doing this. 这是非常麻烦的方式。 I would suggest directly using PowerShell and have at least the ability to properly format the script: 我建议直接使用PowerShell,至少应具有正确格式化脚本的能力:

Import-Module VMware.VimAutomation.Core
Connect-VIServer -Server <server> -User <user> -Password <password>
Restart-VM <vm_name>  -RunAsync -Confirm:$false

You would still be able to call this PowerShell script from a cmd, by using the File-Parameter: 您仍然可以通过使用File-Parameter从cmd调用此PowerShell脚本:

powershell -File <script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM