我是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
请问您能否打印Get-AzureVM结果,因为发布的代码似乎格式正确 – 2014-09-09 10:08:20
@Michael:请参阅重新编辑! – serverstackqns 2014-09-10 06:47:51
当选定的订阅无效时发生错误。现在我会运行'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