2014-09-04 106 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