﻿- PowerShell is a command line in scripting environment that employees cmdlets
and modules. And it has been a preferred method of automating many different actions and processes
with Microsoft system administrators. And with the Azure module, we're gonna show you
how to utilize the same basic principles to easily take charge of your Azure environment.
In this ACG Fundamental, we'll show you how to install Azure PowerShell,
access Azure PowerShell, and how to manage and use your Azure resources too. If you're
not familiar with PowerShell, check out our episode on what is PowerShell. Let's get started.
Our first task is to install Azure PowerShell. So let's go ahead and right click the Start menu
and open up Windows PowerShell.
And we're gonna install Azure PowerShell by using the Install-Module
cmdlet And for name, we're gonna put in Az and we're gonna add a Force switch at the end.
Now that that's complete, let's go ahead and connect our Azure account by using the cmdlet
Connect-AzAccount. When I hit Enter, a pop up will come up, asking you to sign into your account,
I'm gonna go ahead and select mine. And now we are logged in to Azure.
Now this is one of three different ways that you can connect to Azure PowerShell. This one is
locally, the other two are the CloudShell and the Azure portal. Let's take a second in
order for me to show you what that looks like. I'm going to open up a Google Chrome window.
Let's start first with CloudShell. I'm going to go to shell.azure.com.
It's going to ask me to pick an account, enter my password.
If this is your first time logging in,
it will say there is no storage mounted, so you can create storage
and we're logged into Azure CloudShell. Now there are two different environments that you
can pick from PowerShell and Bash. Make sure you're using the PowerShell one. And the last
place that we can access Azure PowerShell is through the Azure portal. So let's go there.
And up at the top right hand corner of the screen, there is a little command prompt looking icon.
If you click it,
it'll go ahead and open a shell at the bottom portion of the screen. Again, you can pick between
PowerShell and Bash and you can resize this as you see fit. Regardless of what method you use
in order to access Azure PowerShell, you can use the same exact commands in any one of them.
Let's talk about the basic syntax of PowerShell. The first piece is called the cmdlet.
Like this cmdlet for get AzResourceGroup that would list all the resource groups I
have available. The cmdlet contains all the code needed in order to perform an action. A lot of
cmdlets also have parameters where it is trying to get information in order to perform that action.
If I follow my cmdlet up with a space and a hyphen, should be able to just use my tab
key on the keyboard in order to go through all the different parameters that are available.
Now, some of these are required and some of them are optional. Once you reach the end,
it will go back to the very beginning. In order to supply the information to the parameter,
you need to put in an argument. So after the parameter, you'd make another space
and inside of double quotes, you would supply the information. So walking through this again,
we have a cmdlet, a parameter and an argument. Let's go ahead and hit Enter,
and we get the information back. Speaking of passing information,
PowerShell is an object-oriented language. Meaning that you can create a variable
and utilize it later in your script. So let me hit the up key in order to get our
last command back and then I'm gonna go all the way to the beginning
and I'm going to make this cmdlet a variable. Variables in PowerShell are defined by the
dollar sign. And then you can name it whatever you like. In this case, I'm just gonna use R and
G for resource group. And then after it, you put in equals. Let's run that very same command again
and you'll notice that it has no information that immediately comes back. This is because all the
information is now being stored in the variable. So if I were to query the variable again,
by just naming the variable, it comes back with the information that's already been pulled
previously. Each one of the different pieces of information that comes back
can be referenced by itself. For example,
if I were to call the variable and I use a dot and I were to search for something like location,
it'll come back and give me that information. This is going to be extremely useful in referencing
that information in your scripts. I'm gonna clear my screen real quick by using cls.
Now getting help is always useful. So let's go ahead and talk about it.
There is a cmdlet that you can use called Get-Help that is available for all the cmdlets. So you just
use the cmdlet Get-Help and whatever cmdlet that you want to get help on. Once you hit Enter,
it will give you a NAME, SYNOPSIS, the full list of syntax,
the description of what that cmdlet does, related links to it, and sometimes related commands,
and any additional remarks. You can also use things like examples.
So let's hit up again and then add a parameter for examples. And then we'll be able to give you
a couple of examples based on different information.
Let's go ahead and clear the screen again, let's create an Azure virtual machine. We need two
things, the resource group and the virtual machine. Let's start by creating the Azure
resource group. And I'm gonna make this a variable because I am definitely going to be using that
to pass information forward when I create the Azure VM.
So I'm gonna name it ResourceGroup, and the cmdlet for a new resource group is New-AzResourceGroup.
You need two things, the Name and the Location. Then we'll hit Enter.
Now let's create the Azure virtual machine. I'm gonna make that a variable
as well. And to create a new Azure virtual machine, the command line is New-AzVM.
Under ResourceGroup name, we're gonna pass the variable, the resource group name.
For location, once again, we're gonna pass that information forward. For the name of the VM,
I'm gonna call an AzurePSVM, for PowerShell virtual machine. Besides credentials,
this is the absolute mandatory items to create an Azure VM. Once I hit Enter,
it's going to ask for credentials. These are not my credentials, but the credentials for the
virtual machine. So I'm gonna use a username of Azure, Mark my name and for the password,
it still uses the same complexity requirements as in the Azure portal. I'm gonna click OK.
And it's gonna go through creating that Azure resource for me.
Now, this is good in order to track progress, but if you do not want to use the progress bar or
you're using this part of the script and you wanna do multiple commands at once, you can use a switch
called AsJob, which I'll show you right after this when we delete this resource group, how
to use it. Presumably the Azure virtual machine has been created. So let's use that same exact
variable again, and we can see that it has a fully qualified domain name,
location, VM ID. And we can see not only all of the outputs from the command,
but we can also use those as information passing into other cmdlets or scripts.
Let's go ahead and check to see that it has indeed been created by going to the Azure portal
and going to Virtual machines. And we have our Azure PowerShell virtual machine,
and it is currently running. Finally, let's go ahead and clean up
by using the Remove-AzResourceGroup by using the Remove-AzResourceGroup command.
Under name, I'm gonna use the ResourceGroup,
ResourceGroupName, and I'm going to use Force in order to force it to remove everything inside of
that resource group as well. And this is actually going to take a little while, and I don't really
feel like waiting inside of PowerShell in order for it to complete. So I'm going to use
the AsJob switch so that it will run this command as a job
so that I can continue using PowerShell in the meantime, instead of like before, when we were
waiting for the virtual machine to be created, we do not have to wait if we use it as a job.
When an Azure virtual machine gets created, it creates a whole lot of other Azure resources with
it. So with three lines of code we were able to create a resource group, create an Azure virtual
machine and all of the relevant resources and remove that resource group. There you have it.
In this video, I showed you how you could install PowerShell and the Azure module and the basics
of how you can use it to leverage managing your cloud resources. If you want to learn
more about Azure PowerShell, check the additional links I've listed in our resource. Alternatively,
head on over to A Cloud Guru for a full, and free, listing of our ACG Fundamental videos.
If you enjoyed this video, give it a thumbs up and consider subscribing to our channel
for everything cloud computing. And keep being awesome Cloud Gurus.