You will need to change a few things in these scripts to suit your purposes:
- Change "YourAzureAccount@hotmail.com" to your Azure account. For MSDN-based Azure subscriptions like mine, this address was my Hotmail email address.
- Change "Visual Studio Premium with MSDN" to the name of your Azure Subscription. This was the name my MSDN-based account was given by default.
- Populate the $vms variable with a list of Azure VM's in the cluster you're looking to start/stop as a group, replacing "yourVMName-alwayson-dom" and so forth.
Stop a list of Azure Accounts:
Note about the -StayProvisioned tag above. Specifying this option will retain some IP settings, but will cause your VM's to continue to accrue Azure credit, even while stopped. Use with care.#if expired, Add-AzureAccount Get-AzureSubscription | ? {$_.ActiveDirectoryUserId -eq 'YourAzureAccount@hotmail.com' -and $_.SubscriptionName -match "Visual Studio Premium with MSDN" } | Set-AzureSubscription -SubscriptionId $_.SubscriptionID $vmHash =@{} $vms = "yourVMName-alwayson-dom","yourVMName-alwaysonsql1","yourVMName-alwaysonsql2","yourVMName-alwaysonWSFC" Get-AzureVM | foreach{$vmHash.Add($_.Name,$_.ServiceName)} foreach ($vm in $vms) { $currentVMService = $vmHash[$vm] Write-Host "Current VM:$($vm)" $thisvm = Get-AzureVM -ServiceName $currentVMService -Name $vm Write-Host "Stopping VM:$($thisvm.Name)" Stop-AzureVM -Name $thisvm.Name -ServiceName $thisvm.ServiceName #-StayProvisioned }
Start a list of Azure Accounts:
#if expired, Add-AzureAccount Get-AzureSubscription | ?{$_.ActiveDirectoryUserId -eq 'YourAzureAccount@hotmail.com' -and $_.SubscriptionName -match "Visual Studio Premium with MSDN" } | Set-AzureSubscription -SubscriptionId $_.SubscriptionID $vmHash =@{} $vms = "yourVMName-alwayson-dom","yourVMName-alwaysonsql1","yourVMName-alwaysonsql2","yourVMName-alwaysonWSFC" Get-AzureVM | foreach{$vmHash.Add($_.Name,$_.ServiceName)} foreach ($vm in $vms) { $currentVMService = $vmHash[$vm] Write-Host "Current VM:$($vm)" $thisvm = Get-AzureVM -ServiceName $currentVMService -Name $vm Write-Host "Starting VM:$($thisvm.Name)" Start-AzureVM -Name $thisvm.Name -ServiceName $thisvm.ServiceName }
If upon running the scripts you receive either of these errors:
get-azuresubscription : The term 'get-azuresubscription' is not recognized as the name of a cmdlet,
Get-AzureVM : The term 'Get-AzureVM' is not recognized as the name of a cmdlet, function, script file, or
Then you don't have the PowerShell module loaded, and PowerShell isn't automatically loading it for you. Use the Import-Module command. Below is the default location of the module, downloaded from here: https://azure.microsoft.com/en-us/documentation/articles/powershell-install-configure/
Import-Module 'C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Azure.psd1'
Updated 20160322 to reflect the latest module paths
4 comments:
Any particular reason for using -match and double quotes with this?
?{$_.ActiveDirectoryUserId -match "YourAzureAccount@hotmail.com" -and $_.SubscriptionName -match "Visual Studio Premium with MSDN" }
Why not just this?
?{$_.ActiveDirectoryUserId -eq 'YourAzureAccount@hotmail.com' -and $_.SubscriptionName -eq 'Visual Studio Premium with MSDN' }
The use of -match versus -eq was based on a mistaken statement I had provided to the author regarding the case sensitivity of -match. -eq would be preferable to avoid the unlikely yet 100% possible greedy matches against other users on the same Azure service.
Single quotes versus double quotes is a small-scale religious argument in PowerShell, mostly because PowerShell users come from a number of different backgrounds with differing syntaxes. They do have different functions in PowerShell, but in this instance, there is no practical difference. If your background is SQL, then single quotes may be more familiar for you to use with strings. As long as you understand the differences between the two marks in PowerShell, you're generally fine either way.
Good catch Paulo, Drew's right. I've changed it. Idea is that we want just a case insensitive comparison, that's all that we need there.
And you're also right about the single quotes, because we want to compare, not escape, any special characters in the email address, though rare.
Thanks Paulo!
Huh, so, an educational rabbit hole this is.
I think " or ' would be functionally fine, but because many special characters are valid (though rare) in email addresses, including a backtick, we DO want single quotes. Thanks for bringing it up, Paulo!
Post a Comment