【问题标题】:Powershell for stopping and starting Azure VMs用于停止和启动 Azure VM 的 Powershell
【发布时间】:2018-03-08 00:25:58
【问题描述】:

我在 Windows Server (x64) 上创建了一个“Visual Studio Enterprise 2017(最新版本)”虚拟机。我想通过 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?或者有更好的方法吗?

【问题讨论】:

    标签: powershell azure azure-automation


    【解决方案1】:

    我的猜测是您使用 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
    

    【讨论】:

      【解决方案2】:

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

      如果您通过经典模型部署了您的虚拟机,那么您将获得ServiceName

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

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

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-02-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-12
        相关资源
        最近更新 更多