Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

Automatic Create an RDP Connection to Azure VM After the Deployment Has Completed

May 18, 2024
in Blog, Microsoft Azure
0
ADVERTISEMENT

Table of Contents

RDP to Azure VM After the Deployment Has Completed

When managing Azure, I often create Windows VMs. To automate this, I utilize PowerShell. However, I would like to establish an RDP connection to the newly created VM without manually inputting the public IP address, username, and password.

To do it, I’ve added the below code snippet at the end of the PowerShell script to create an Azure VM. The code snippet will:

  • Get the VM name from a variable. This is a required variable when creating a VM using PowerShell.
  • Extract the public IP address of the VM from the VM information.
  • Stores the value of the username and password variables to the cmdkey. These variables are required when creating an Azure VM as well.
  • Finally, connect to the VM using mstsc.
Note Note: I've specified the weight and height of the RSD session. If you want to start a RDP session in full-screen mode, let remove them or change them to any resolution as you need.
$vm         = Get-AzVM -Name 'vm-63424851'
$vmNicName  = $vm.NetworkProfile.NetworkInterfaces.Id.Split("/")[8]
$ipAddress  = Get-AzPublicIpAddress | Where-Object {$_.IpConfiguration.Id -like "*$vmNicName*"}
$rdpip      = $ipAddress.IpAddress
$rdpip

cmdkey /generic:$rdpip /user:"bonguides" /pass:"Pass@ord123"
mstsc /v:$rdpip /w:1366 /h:768

Additionally, below is the code I’ve used to create a Windows Azure VM using PowerShell and then connect to it automatically after the deployment.

# Don't forget to change the variables to fit with yours.
$num            = $(Get-Random)
$rgname         = "RG-$num"
$loc            = "eastus"
$vmname         = "vm-$num"
$vnetname       = "vnet-$num"
$vnetAddress    = "10.0.0.0/16"
$subnetname     = "subnet-$num"
$subnetAddress  = "10.0.2.0/24"
$NICName        = $vmname + "-nic"
$NSGName        = $vmname + "-NSG"
# $VMSize         = "Standard_DS2_v2"
$VMSize         = "Standard_D8_v4"
$PublisherName  = "microsoftwindowsdesktop"
$Offer          = "Windows-10"
$SKU            = "win10-22h2-pro-g2"
$version        = "latest"
$password       = "Pass@ord123"
$adminUsername  = 'bonguides'
$securePassword = $password | ConvertTo-SecureString -AsPlainText -Force
$Credential     = New-Object System.Management.Automation.PSCredential ($adminUsername, $securePassword)

# Creating a resource group
    Write-Host "Creating a resource group..." -ForegroundColor Green
    $null = New-AzResourceGroup -Name $rgName -Location $Loc

# Network setup
    Write-Host "Setting up the network..." -ForegroundColor Green
    Import-Module Microsoft.Azure.PowerShell.Cmdlets.Network -DisableNameChecking
    $frontendSubnet = New-AzVirtualNetworkSubnetConfig -Name $subnetname -AddressPrefix $subnetAddress -WarningAction silentlyContinue
    $vnet           = New-AzVirtualNetwork -Name $vnetname -ResourceGroupName $rgname -Location $loc -AddressPrefix $vnetAddress -Subnet $frontendSubnet -WarningAction silentlyContinue
    $nsgRuleRDP     = New-AzNetworkSecurityRuleConfig -Name RDP  -Protocol Tcp  -Direction Inbound -Priority 1001 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 3389 -Access Allow -WarningAction silentlyContinue
    $nsg            = New-AzNetworkSecurityGroup -ResourceGroupName $RGName -Location $loc -Name $NSGName  -SecurityRules $nsgRuleRDP -WarningAction silentlyContinue
    $pip            = New-AzPublicIpAddress -ResourceGroupName $rgName -Location $loc -Name "mypublicdns$(Get-Random)" -AllocationMethod Static -IdleTimeoutInMinutes 4 -WarningAction silentlyContinue
    $nic            = New-AzNetworkInterface -Name $NICName -ResourceGroupName $RGName -Location $loc -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id -EnableAcceleratedNetworking -WarningAction silentlyContinue
    $vmConfig       = New-AzVMConfig -VMName $vmName -VMSize $VMSize -WarningAction silentlyContinue
    $null           = Set-AzVMOperatingSystem -VM $vmConfig -Windows -ComputerName $vmName -Credential $Credential -WarningAction silentlyContinue
    $null           = Set-AzVMSourceImage -VM $vmConfig -PublisherName $PublisherName -Offer $Offer -Skus $SKU -Version $version -WarningAction silentlyContinue 
    $null           = Add-AzVMNetworkInterface -VM $vmConfig -Id $nic.Id -WarningAction silentlyContinue

# Creating the Azure VM
    Write-Host "Creating the Azure VM..." -ForegroundColor Green
    New-AzVM -ResourceGroupName $rgname -Location $loc -VM $vmConfig -WarningAction silentlyContinue

# Waiting for the VM starts
    Write-Host "Waiting for the VM starts..." -ForegroundColor Green
    # Start-Sleep -Seconds 30

# After the deployment has completed, create a remote desktop connection with the VM
Write-Host "Connecting using RDP..." -ForegroundColor Green
$vm         = Get-AzVM -Name $vmName
$vmNicName  = $vm.NetworkProfile.NetworkInterfaces.Id.Split("/")[8]
$ipAddress  = Get-AzPublicIpAddress | Where-Object {$_.IpConfiguration.Id -like "*$vmNicName*"}
$rdpip      = $ipAddress.IpAddress

cmdkey /generic:$rdpip /user:"bonguides" /pass:"Pass@ord123"
mstsc /v:$rdpip /w:1366 /h:768
ADVERTISEMENT

Not a reader? Watch this related video tutorial:

5/5 - (1 vote)
Previous Post

Automatic Virtual Machine Activation in Hyper-V in Windows Server

Next Post

How to Get the Azure VM’s Public IP Address Using PowerShell

Related Posts

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024
Ftr19

How to Fix Windows Cannot Read the ProductKey From the Unattend Answer File in VirtualBox

July 26, 2024
Ftr25

How to Update Windows Terminal in Windows 10/11

July 26, 2024

How to Disable The Beep Sound in WSL Terminal on Windows

July 26, 2024

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory