Manage multiple Azure Contexts using PowerShell

PowerShell for Azure Databricks — Data Thirst

In my day-to-day work I have to deal with several customers and Azure Subscriptions, and for this reason it sometimes becomes exhausting to jump from one Azure Context to another, even when I want to switch to my personal Azure tenant to run some tests.

Today’s article will be short, but simple and useful. After all, I believe it can help in the organization and agility of those who need to manage several subscriptions like me.

Okay, let’s get straight to the point.

Log in with your Azure account;

image

As you can see in the image above, once I authenticated an Azure context comes up as the default context.

Important -What is an Azure Context? Microsoft says “Azure contexts are PowerShell objects representing your active subscription to run commands against, and the authentication information needed to connect to an Azure cloud.”

Okay, we already noticed that when I authenticate with the user above, an Azure context is already loaded and so the next command will show which Azure contexts this same user has access to.

image

So let’s suppose I want to change which default subscription I want loaded once I authenticate to PowerShell.

image

Once you’ve changed the default context, you can check along the way: “C:\Users\Username\.Azure\AzureRmContext.json”

image

You can also rename all other subscriptions to a simpler name, and then you can select them more simply.

Rename-AzContext -SourceName ‘Visual Studio Professional (xxxxxxxx-xxxx-xxxxxx-xxxx-xxxxxxxxxx) xxx.xxx@xxx’ -TargetName ‘GP_Subscription’

And then when it is selected, you can use the new name placed

Select-AzContext ‘GP_Subscription’

Here we go, now you can choose your default context and also how to rename your context. You can also save these contexts like this when I did a few steps back and then when needed just import the context directly.

Import-AzContext “C:\Users\Username\.azure\CHANGENAME-context.json”

That’s all for today folks, see you soon.

Joao Costa

Creating Network Security Group using PowerShell

NSG_01

Hi folks!

Today let’s create the network security group that has a very important role within Microsoft Azure. It works at layer 4, where we can communicate ports and IPs between internal or external networks through a VPN.

Now let’s assign the following variables:

$NSGName=”NSG-VM-01″
$RGName= “RG_GETPRACTICAL”
$LOCATION= “UKSOUTH”

NSG_02

Next, we will create a variable with the name of the port and which rule will be used. In this case, I am creating an “NSG” for RDP access.

$RULES = New-AzNetworkSecurityRuleConfig -Name ‘Default-Allow-RDP’ -Direction Inbound -Priority 1000 -Access Allow -SourceAddressPrefix ‘*’  -SourcePortRange ‘*’ -DestinationAddressPrefix ‘*’ -DestinationPortRange 3389 -Protocol TCP

NSG_03

Now let’s create the NSG, using the following command.

$NSG = New-AzNetworkSecurityGroup -Name $NSGName -ResourceGroupName $RGName -Location $LOCATION -SecurityRules $RULES

NSG_04

Your NSG was successfully created.

NSG_05

Thanks guys and until the next post, where I will demonstrate how to create a virtual machine using all these commands at once.

Joao Paulo Costa

Creating Network Interface using PowerShell

NIC_01

Hey folks,

Continuing our series of articles on how to create resources in Azure using PowerShell, let’s talk about creating the network interface using PowerShell, creating the network interface and assigning it to a VM and associating it to a VNET is easier via shell command.

Now let’s assign some variables to create the network interface.

$RGName= “RG_GETPRACTICAL”
$NIC1=”Nic-GP-VM-01″
$LOCATION= “UKSouth”
$VNETNAME=”VNet-GETPRACTICAL”
$subnetIndex=0

NIC_02

This “SubnetIndex” variable is very important in the creation process, as it will identify each of your VNETs within your environment. In the case of this article I have a single VNET so I am considering the value “0”, but if you need to pull this value, just run a “Get-AzVirtualNetwork” with the add-ons such as resource group and VNET name.

Now let’s validate if the network exists within the environment.

$VNET=Get-AzVirtualNetwork -Name $VNETName -ResourceGroupName $RGName

NIC_03

Next we will create a public IP for the network interface.

$PIP=New-AzPublicIpAddress -Name $NIC1 -ResourceGroupName $RGName -Location $LOCATION -AllocationMethod Dynamic

NIC_04

Finally, we will create the network interface associating the public IP and the VNET that exists within our environment.

$NIC=New-AzNetworkInterface -Name $NIC1 -ResourceGroupName $RGName -Location $LOCATION -SubnetId $vnet.Subnets[$subnetIndex].Id -PublicIpAddressId $PIP.Id

NIC_05

Your network interface has now been successfully created.

NIC_06

Thanks guys and until the next post!

Joao Paulo Costa

Creating Virtual Network using PowerShell

Vnet_01

Continuing from the last article, today we are going to create a virtual network to allocate Azure resources and leave it in a secure pattern. In this scope, I’m setting up the network with the segmented subnets:

  • BackEnd: 172.16.1.0/26
  • FrontEnd: 172.16.1.64/26
  • DMZ: 172.16.1.128/28
  • Gateway: 172.16.1.144/28

Before starting to create the Azure network structure, let’s understand how a network in Azure works.

In Azure, when we create a network, we first choose the “Address Space” that would be an IP block that we would use inside our virtual network and within this block we will consider that each “Subnet” will be a piece of this block, according to the drawing below.

Vnet_02

After logging into the “Cloud Shell” select PowerShell, let’s assign some variables to create the network.

$RGName= “RG_GETPRACTICAL”
$LOCATION= “UKSOUTH”
$NameVnet=”VNet-GETPRACTICAL

These variables are for choosing the resource group where we will provision, the location and the name of your virtual network.

Vnet_03

Now let’s assign the network settings pointing the “Address Block”, in this case the Address Space 172.16.1.0/24 with 256 hots.

New-AzVirtualNetwork -Name $NameVnet -ResourceGroupName $RGName -Location $location -AddressPrefix 172.16.1.0/24

Vnet_04

See that it has been provisioned but does not contain subnets. now let’s assign some variables. This variable is to validate if the network exists within the environment.

$VirtualNetwork = Get-AzVirtualNetwork -Name $NameVnet -ResourceGroupName $rgName

Vnet_05

After validating the existing network, let’s add the subnets as shown in the examples below.

Add-AzVirtualNetworkSubnetConfig -Name BackEnd -VirtualNetwork $VirtualNetwork -AddressPrefix 172.16.1.0/26

Vnet_06

Next, let’s add the rest of the network scope.

Add-AzVirtualNetworkSubnetConfig -Name FrontEnd -VirtualNetwork $VirtualNetwork -AddressPrefix 172.16.1.64/26
Add-AzVirtualNetworkSubnetConfig -Name DMZ -VirtualNetwork $VirtualNetwork -AddressPrefix 172.16.1.128/28
Add-AzVirtualNetworkSubnetConfig -Name GatewaySubnet -VirtualNetwork $VirtualNetwork -AddressPrefix 172.16.1.144/28

Vnet_07

Now let’s run the “Set” command to add the set of subnets that were assigned above.

Set-AzVirtualNetwork -VirtualNetwork $VirtualNetwork

Vnet_08

Once your virtual network has been successfully created with its segmented subnets, let’s go to the portal to validate it.

Vnet_09

That’s all for today folks, until the next post.

Joao Costa

Azure Arc – How to add a server into it

image

Hey guys!

Today I’m going to talk about Azure Arc. This is a very useful tool nowadays, after all we have to work with more complex and heterogeneous environments. Therefore, the idea of being able to manage an entire infrastructure from a single access point saves many hours of work.

Well then, that is the role of Azure Arc. In it you can add Azure or non-azure resources, that is, other resources from other public clouds, on-premises, databases, etc.

Again, this is an intuitive and practical resource to use, let’s get right to the practice again.

Log in with your Azure account on the portal and type in the search bar “Azure Arc”. Open Azure Arc and you should see the Azure Arc Center.

image

On the home screen you have three tiles options, such as: Add your infrastructure for free, Deploy Azure Services and View Azure Arc Resources.

For this demo, we will use the first tile, so in “Add your infrastructure for free” click Add and then on the next screen, in the Servers tile click Add again.

image

On the next screen you can choose if you want to add one or more servers, add servers using Azure Migrate or Update Management (Still in preview).

image

In the tile add a single server, click Generate Script.

From now on I believe you already understand what will happen, Azure will open a wizard that will help you configure and generate a script that will do everything for you, such as downloading the agent, installing the agent and registering the server in Azure. You will only need to run the script on the server you intend to add to Azure Arc.

After clicking on Generate Script, you will see the following screen:

image

On this screen, you will need to pay attention to the basic requirements for the script to work.

  • Firewall requirements, you will need port 443 to perform this task.
  • You will need permission as a local administrator on the server or servers.
  • Finally, what is the means of communication between Azure and machine, public internet, proxy server or a private endpoint (VPN or Express Route).

Click next and select the options according to your environment.

image

Click on next and if that’s the case you can add tags to better identify your environment. Or just skip to the next screen.

image

Or just skip to the next screen.

image

Now you need to copy or download the script and run it on the intended server.

And as soon as you run the script on the desired server, it will show up in Azure Arc as Connected status.

That’s all for today guys, until the next post.

Joao Costa

Azure: Creating a Windows 11 VM

virtual-machine

Hi Guys,

In today’s article I will be brief, but I want to demonstrate a subject that is well up to date: How to create a vm with Windows 11 through  Cloud Shell in Azure portal.

Let’s go straight to practice: Log into the Azure portal and hit the Cloud Shell icon located on the right side of the search bar.

01

If you have not yet used the Cloud Shell, on the first access a Resource Group will be created for the Cloud Shell to use it. In the left corner it is also possible to choose between PowerShell or Bash commands (In case you are familiar with Linux), for this example I will use PowerShell command.

Okay, the next step will be to create a resource group for this virtual machine.

02

Now run the following commands to create your virtual machine

az vm create –resource-group GetPractical –name VMWindows11 –image windows-11-Preview –public-ip-sku Standard –admin-username azureuser –admin-password “GetPractical@Windows11

03

All other parameters like disk, cpu, vnet and etc will be created automatically. If you need to customize, you will also need to customize the command or create via GUI portal.

This process should take a few minutes, but once it is finished you will be able to see in the portal that the VM was created successfully.

It’s important to say that at the time I deployed this vm, Windows 11 was still in preview. If at the time of this post the preview version is no longer available, access the following Microsoft docs :

https://docs.microsoft.com/en-us/azure/virtual-machines/windows/cli-ps-findimage

05

Finally, run the mstsc /v <Public IP Address> command to access your virtual machine with Windows 11 and the result should be as follows:

04

And that folks, if you have any doubts, leave them in the comments.

Joao Costa