powershell - Azure Powershell 用于停止订阅中的 VM

标签 powershell azure

我是 Powershell 新手,特别是 Azure Powershell 新手。我需要创建一个 powershell 脚本来关闭订阅中找到的所有虚拟机。我想这需要借助管理证书来完成。但不知道从哪里开始。我只是做了一些简单的代码行来列出所有虚拟机,如下所示:

Add-AzureAccount -Environment "AzureCloud"
$subscription = "my-sub-scri-ption"
Select-AzureSubscription -Current "SubName"
Set-AzureSubscription -SubscriptionName "SubName" 
Get-AzureVM -ServiceName "VM1"

收到的输出为“Get-AzureVM:值不能为空。 参数名称:subscriptionId"。

有人可以在这方面帮助我吗?

**编辑:**

我正在使用的powershell脚本如下:

Add-AzureAccount -Environment "AzureCloud"
Set-AzureSubscription -SubscriptionName "My Subs"
$serviceName = "Service01"
$vmName = "Service01"
Get-AzureVM | Stop-AzureVM -Force

尽管运行时显示脚本执行成功,但我仍然可以看到虚拟机已启动。请注意,在我的例子中,serviceName 和 vmName 是相同的。我的代码有什么问题吗?

**重新编辑** 执行代码:

Add-AzureAccount -Environment "AzureCloud"
Set-AzureSubscription -SubscriptionName "My Subs"
$serviceName = "Service01"
$vmName = "Service01"
Get-AzureVM

上述代码的错误:

Get-AzureVM : Value cannot be null.
Parameter name: subscriptionId
At D:\TC_PS\Untitled1.ps1:5 char:1
+ Get-AzureVM
+ ~~~~~~~~~~~
+ CategoryInfo          : CloseError: (:) [Get-AzureVM], ArgumentNullException
+ FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.GetAzureVMCommand

最佳答案

您可以从这里开始:Getting Started with Windows Azure PowerShell

然后您只需运行 Get-AzureVM 即可返回每个云服务的所有虚拟机。

要停止虚拟机:Stop-AzureVM -ServiceName xxx -Name vm-test-01

要停止订阅中的所有虚拟机,只需运行:Get-AzureVM |停止-AzureVM-强制

-Force 开关对于停止云服务的最后一个虚拟机是必需的,否则系统在尝试停止服务中的最后一个虚拟机时会抛出错误,以避免删除所有的虚拟机。您发布的应用程序的资源。

如果您不指定-StayProvisioned 开关,虚拟机将被释放。这是 Stop-AzureVM 的默认行为。

关于powershell - Azure Powershell 用于停止订阅中的 VM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25660225/

相关文章:

azure - Golang aztables az登录授权失败

azure - 更新到 Azure SDK 2.0 后无法加载 'Microsoft.WindowsAzure.ServiceRuntime' 程序集

azure - WebJob 没有看到 AzureWebJobsDashboard 设置

powershell - 使用 Azure Power Shell 将应用程序设置添加到现有 Azure Web 应用程序

powershell - 如何从共享服务器导入 splitpipeline 模块?

mysql - Powershell:使用 MySQL DB 中的 SELECT 作为 PowerCLI 中的变量

powershell - 将哈希表中的数组导出到没有空行的 CSV

powershell - 如何使用Powershell在Windows容器中使用环境变量

c# - 使用 .NET(PowerShell 或 .NET)获取 BCD 条目

c# - Azure WebJobs Blob 触发器 - 多次调整大小