Azure Powershell: Basic CmdLets

Get-AzureSubscription  -Current   # List current subscription
Get-AzureSubscription  # List all subscriptions

——————————————————————————————————————————–LOGIN

– Either: Login-AzureRmAccount
Select-AzureRmSubscription -SubscriptionName GS-DevOps
Set-AzureRmContext -SubscriptionName GS-DevOps

– or: Add-AzureAccount (You can add as many accounts to Azure PowerShell)

– or: Get-AzurePublishSettingsFile

Import-AzurePublishSettingsFile C:\Users\GS\Downloads\mysubs.publishsettings

——-

Just to make sure that everything has gone right run the following cmdlet:
Get-AzureAccount / Get-AzureSubscription  -Current / Get-AzureSubscription

————————————————————————

# Select Subscription
Set-AzureRmContext -SubscriptionId 38d8d050-5c50-4eba-b06b-16124ec3b55c
Select-AzureSubscription -SubscriptionName GS-Devops
Select-AzureSubscription -Current -SubscriptionName GS-Devops

———————————————————————-

To get a list of resource groups in your subscription, use Get-AzureRmResourceGroup.

 

 

PowerShell versus ARM Templates

Let me explain why complete PowerShell deployment would be too slow, and how ARM/JSON is they best way to deploy things in Azure.

With PowerShell you describe the deployment process, one step at a time, connecting each and every dot. The deployment is serialized, with no parallelism unless you use PowerShell features to run parallel jobs. The result isn’t much faster than you doing all the clicking for yourself.

ARM or JSON templates describe the result, not the process. Once you submit the deployment, ARM divides up the job and orders the deployment based on your dependencies. That means that the deployment can be parallelized. If I need 100 web servers, all 100 will be deployed at once, not in some 1..100 loop, one at a time (or 5 at a time if you are clever).

PowerShell Still Required

PowerShell is still very useful for some fiddly deployment things that don’t have ARM options, or are once-offs and don’t have a GUI option. To be honest, I do use GUI for most of my once-offs because it is convenient and gets the end result faster than researching/tweaking/fixing PowerShell examples. When it comes to learning about settings and troubleshooting, PowerShell can be pretty awesome.

But PowerShell is much slower than ARM for deployments.

 

PowerShell common commands for Azure VMs

To get a list of resource groups in your subscription, use Get-AzureRmResourceGroup.

—–

List VMs in a subscriptionGet-AzureRmVM

Filter List:

$armVms = Get-AzurermVM | select Name, ServiceName, ResourceGroupName
$armVms

—–

List VMs in a resource group, Get-AzureRmVM -ResourceGroupName myResourceGroup

—-

Get information about a VM, Get-AzureRmVM -ResourceGroupName myRG -Name $myVM

—-

To get the virtual machine core count usage for a location: Get-AzureRmVMUsage

Manage VMs

Task Command
Start a VM Start-AzureRmVM -ResourceGroupName $myResourceGroup -Name $myVM
Stop a VM Stop-AzureRmVM -ResourceGroupName $myResourceGroup -Name $myVM
Restart a running VM Restart-AzureRmVM -ResourceGroupName $myResourceGroup -Name $myVM
Delete a VM Remove-AzureRmVM -ResourceGroupName $myResourceGroup -Name $myVM
Generalize a VM Set-AzureRmVm -ResourceGroupName $myResourceGroup -Name $myVM -Generalized

Run this command before you run Save-AzureRmVMImage.

Capture a VM Save-AzureRmVMImage -ResourceGroupName $myResourceGroup -VMName $myVM -DestinationContainerName “myImageContainer” -VHDNamePrefix “myImagePrefix” -Path “C:\filepath\filename.json”

A virtual machine must be prepared, shut down and generalized to be used to create an image. Before you run this command, run Set-AzureRmVm.

Update a VM Update-AzureRmVM -ResourceGroupName $myResourceGroup -VM $vm

Get the current VM configuration using Get-AzureRmVM, change configuration settings on the VM object, and then run this command.

https://docs.microsoft.com/en-us/azure/virtual-machines/windows/ps-common-ref

What is Powershell ISE?

The Windows PowerShell Integrated Scripting Environment (ISE) is a host application for Windows PowerShell.

In Windows PowerShell ISE, you can run commands and write, test, and debug scripts in a single Windows-based graphic user interface with multiline editing, tab completion, syntax coloring, selective execution.

You can use menu items and keyboard shortcuts to perform many of the same tasks that you would perform in the Windows PowerShell console.

To start PowerShell ISE, type powershell in search box of  Windows10 (as shown below) and click PowerShell ISE tool to open it. Or, search for powershell_ise.exe.

p3

PowerShell ISE

p4

Type your commands here and do the magic…. !!