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)