azure - Powershell脚本仅启动列表中的第一个VM

标签 azure powershell

我有一个 Azure powershell 脚本来启动 VM 列表。当它运行时,它会成功启动列表中的第一个虚拟机,然后什么也没有。没有报告错误,它似乎永远不会从启动命令返回

代码如下:

$vmList = "VM1", "VM2", "VM3"

Write-Output "Starting VMs in '$($AzureResourceGroup)' resource group"; 

Foreach ($vm in $vmList) 
{ 
    Write-Output "Starting VM...$vm" 
    $result = Start-AzureRmVM -Name $vm -ResourceGroupName AzureResourceGroup
    Write-Output "Result of Start VM...$result"
}

当此运行时,它输出“正在启动 VM....VM1”,它启动 VM,然后什么也没有...

最佳答案

听起来您的 Start-AzureVM 调用只是等待虚拟机完成启动

也就是说,Start-AzureVm 默认情况下是一个阻塞、同步操作 - 尽管它的名称是[1]

要使其异步,请使用-AsJob开关,该开关使用后台作业来启动VM;创建该作业后,调用就会结束,并返回一个作业对象,您可以使用该对象稍后通过常用的 *-Job cmdlet 来跟踪启动进度,例如Receive-Job .

<小时/>

[1] 关于名称 Start-AzureVM:

这是一个用词不当,因为 PowerShell 的命名法要求使用 Start 动词来表现异步行为:

来自https://learn.microsoft.com/en-us/powershell/developer/cmdlet/approved-verbs-for-windows-powershell-commands (强调):

Invoke vs. Start The Invoke verb is used to perform an operation that is generally a synchronous operation, such as running a command. The Start verb is used to begin an operation that is generally an asynchronous operation, such as starting a process.

请注意,核心 Start-Sleep cmdlet 的命名也同样错误。

对于默认同步行为,没有好的命名解决方案,因为名称 Invoke-AzureVm 会令人困惑。

更好的方法(这显然是一个重大更改)是让 cmdlet 默认情况下异步,并提供一个 -Wait 开关来选择加入同步操作。

关于azure - Powershell脚本仅启动列表中的第一个VM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55458435/

相关文章:

适用于逻辑应用的 Azure API 管理反向代理

xml - XML文件格式-删除杂散CRLF

azure - 如何使用 PowerShell 检索逻辑应用工作流设置?

windows - 以管理员身份执行时如何在批处理文件中捕获 powershell 脚本的错误级别

powershell - 具有一个可选参数,需要另一个参数

python - 安装 virtualenvwrapper-powershell 时出现 HTTP 错误 402

c# - Azure 计算模拟器中的内部端口冲突

azure - 流分析:When using a TUMBLING WINDOW is the start time of the window start based on the earliest time in the stream or start time of the job?

Azure 服务总线 - JAVA 中的消息计数

javascript - 有没有办法在访问 SPA 时立即进行登录重定向?