You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过PowerShell启停指定Azure虚拟机及查找对应ServiceName

Azure VM 启停:查找ServiceName与最优实现

Hey there! Let's walk through how to handle your Azure VM start/stop tasks with PowerShell, including figuring out that ServiceName and better ways to get things done.

一、查找VM对应的ServiceName

First, a key note: ServiceName is a leftover from Azure's classic deployment model, not the modern ARM (Azure Resource Manager) model that's now the default. Here's how to track it down based on your VM's deployment type:

1. 经典部署模型VM

If your VM was created using the older classic model, use these PowerShell commands:

  • List all classic VMs and their associated ServiceNames:
    Get-AzureVM | Select-Object ServiceName, Name
    
  • Filter to your specific VM directly:
    Get-AzureVM -Name "YourVMName" | Select-Object ServiceName, Name
    

The ServiceName property in the output is exactly what you're looking for.

2. ARM部署模型VM

For modern ARM VMs (the standard today), there is no formal "ServiceName"—Azure uses Resource Group + VM Name as the primary identifier for all operations. If you're trying to find related resources tied to the VM (like network interfaces or disks), you can list resources in the VM's resource group:

Get-AzResource -ResourceGroupName "YourResourceGroupName" | Where-Object {$_.Name -like "*YourVMName*"}

二、更优的VM启停实现方式

Azure now strongly recommends using the Az PowerShell module (it replaces the outdated Azure module) for all ARM-based operations. This approach is more efficient, secure, and aligned with current best practices—you won't even need a ServiceName at all.

Basic Start/Stop Commands

  • Start your VM:
    Start-AzVM -ResourceGroupName "YourResourceGroupName" -Name "YourVMName"
    
  • Stop your VM (add -Force to skip confirmation; use -StayProvisioned if you want to keep resources allocated instead of deallocating to save costs):
    # Deallocate resources (reduces compute costs when VM is idle)
    Stop-AzVM -ResourceGroupName "YourResourceGroupName" -Name "YourVMName" -Force
    
    # Shut down without deallocating (keeps resources reserved)
    Stop-AzVM -ResourceGroupName "YourResourceGroupName" -Name "YourVMName" -Force -StayProvisioned
    

Efficient Object-Based Operations

For repeated tasks, fetch the VM object first and pipe it to the start/stop commands for cleaner code:

# Get the VM object
$vm = Get-AzVM -ResourceGroupName "YourResourceGroupName" -Name "YourVMName"

# Start the VM
$vm | Start-AzVM

# Stop and deallocate the VM
$vm | Stop-AzVM -Force

Bonus: Migrate Classic VMs to ARM

If you're still running classic VMs, consider migrating them to ARM. Classic deployments are being phased out, and ARM offers better scalability, security, and management tools. You can use the Move-AzureService command or Azure Portal's built-in migration tools to make the switch.

Hope this helps you manage your VM's power state smoothly!

内容的提问来源于stack exchange,提问作者Nigel Findlater

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:59:29