用于停止和启动 Azure VM 的 Powershell

Powershell for stopping and starting Azure VMs

我已经创建了一个 "Visual Studio Enterprise 2017 (latest release) on Windows Server (x64)" 虚拟机。我想通过 Powershell 启动和停止这台机器。

#Login
Add-AzureAccount

#Enterprize subscription. Id can be found by seraching for subsription in the portal
Select-AzureSubscription -SubscriptionId xxxxxx-xxxe-xxxx5-8xxxx-e2xxxxxxxx1c

#Should list VMs but returns nothing
Get-AzureVM 

#Asks for ServiceName that I cannot find 
Start-AzureVM -Name NigelsPcWUS        

如何找到与我的 VM 对应的 ServiceName?或者有更好的方法吗?

ServiceName 指定包含要关闭的虚拟机的 Azure 服务的名称。

如果您通过经典模型部署了 VM,那么您将获得 ServiceName

从描述来看,您似乎是通过ARM模型创建了VM。 我建议您使用 Get-AzureRmVM cmdlet 列出虚拟机。

要启动 VM,请使用以下 PowerShell cmdlet。

Start-AzureRmVM -ResourceGroupName "YourResourceGroupName" -Name "YourVirtualMachineName"

要停止 VM,请使用以下 PowerShell cmdlet。

Stop-AzureRmVM -ResourceGroupName "ResourceGroupName" -Name "VirtualMachineName"

我的猜测是您使用 Azure 资源管理 (ARM) 模型创建了虚拟机。上面的 PowerShell 脚本使用旧的 Azure 服务管理 (ASM) 模型。

以上脚本需要做一些修改才能使用ARM模型:

#Login using the ARM model
Login-AzureRmAccount

#Select your subscription if you got multiple
Select-AzureRmSubscription -SubscriptionId xxxxxx-xxxe-xxxx5-8xxxx-e2xxxxxxxx1c

#Get a list of exisiting VMs
Get-AzureRmVM

#Start a vm named 'linux' inside resource group 'lab-rg'
Start-AzureRmVM -Name linux -ResourceGroupName lab-rg

如果您没有安装 AzureRM PowerShell 命令并且是 运行 Windows 10,您可以通过 运行

轻松安装它们
Install-Module AzureRM