2014-09-04 107 views
1

我是Powershell的新手,并且对Azure Powershell来说是全新的。我需要创建一个PowerShell脚本来关闭订阅中找到的所有虚拟机。我想这需要在管理证书的帮助下完成。但不知道从哪里开始。我只是做了一些代码行简单列出所有的虚拟机,如下图所示:Azure 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 

虽然在运行它显示脚本的执行是成功的,我可以看到仍然是VM已打开电源。请注意,在我的情况下,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 
+0

请问您能否打印Get-AzureVM结果,因为发布的代码似乎格式正确 – 2014-09-09 10:08:20

+0

@Michael:请参阅重新编辑! – serverstackqns 2014-09-10 06:47:51

+0

当选定的订阅无效时发生错误。现在我会运行'Get-AzureSubscription | Remove-AzureSubscription'这会删除你所有的订阅(我认为你在添加accout之前运行了Set-AzureSubscription),然后在登录后检查你刚刚运行'Get-AzureSubscription'的订阅,你就不需要运行'Add-AzureAccount'需要运行Set-AzureSubscription(用于更改订阅,例如设置当前的存储名称),在检查正确的SubscriptionName后,只需运行“Select-AzureSubscription”MySubscriptionName“'然后'Get-AzureVM' – 2014-09-10 11:00:09

回答

4

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

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

要停止VM:Stop-AzureVM -ServiceName xxx -Name vm-test-01

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

-Force开关必须停止云服务的最后一个虚拟机,否则系统会尝试停止服务中的最后一个虚拟机时发生错误,以避免丢弃已发布应用程序的所有资源。

如果您没有指定-StayProvisioned交换机,则虚拟机将被解除分配。这是Stop-AzureVM的默认行为。

+0

谢谢。我想用一个命令停止(停止释放)特定订阅的所有虚拟机。这是因为未来VM的数量可能会发生变化。所以,不能传递这些名字。这有什么可能吗? – serverstackqns 2014-09-04 08:37:27

+0

肯定有可能,只需启动'Get-AzureVM | Stop-AzureVM -Force强制停止云服务的最后一个虚拟机。 – 2014-09-04 08:48:06

+0

好的Micheal ..这很清楚,很好。我想解除虚拟机的分配(以最小化成本),同时关闭虚拟机。如何实现..? – serverstackqns 2014-09-04 09:04:17