Using PowerShell with Azure
General PowerShell notes.
Subscriptions
Get-AzureRmSubscription
& az account list --output table
are equivalent.
Azure:\
PS Azure:\> Get-AzureRmSubscription
Name : Visual Studio Enterprise
Id : xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
TenantId : yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy
State : Enabled
&
Azure:\
PS Azure:\> az account list --output table
Name CloudName SubscriptionId State IsDefault
------------------------ ----------- ------------------------------------ ------- ---------
Visual Studio Enterprise AzureCloud xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx Enabled True
But Import-AzurePublishSettingsFile
isn’t interactive & thus much more suited to a CI/CD pipline.
Stoping all VMs in a test subscription
Let’s say be had a subscription "Azure Internal"
& (for cost reasons) we wanted an easy way to shut it down every night.
First we need to obtain a .publishSettings
file to (securly) hold our Azure publisher settings, this is a one-time task and is performed via Get-AzurePublishSettingsFile
. Assuming these settings have been saved to
"C:\temp\AzureInternal.publishSettings"
then the following script is our friend.
# If any issue with old values run
#
# Clear-AzureProfile
#
# Then download a new publishSettings file and save to "C:\temp\AzureInternal.publishSettings"
#
# Get-AzurePublishSettingsFile
#
# Login from already stored certificate
#
Import-AzurePublishSettingsFile -PublishSettingsFile "C:\temp\AzureInternal.publishSettings"
# Set the default to your Subscription, you can use Get-AzureSubscription to find the name
#
Set-AzureSubscription -SubscriptionName "Azure Internal"
# Shut down all the running VMs
#
Get-AzureVM | Where { $_.Status -ne "StoppedDeallocated" } | Stop-AzureVM -Force