Riya
Riya

Reputation: 1

how to write powershell script to check if the VM is jumpbox VM from a list of VMs in azure

Powershell script to retreive Jumpbox VM from a list of VMs, which VMs are all have jumpbox connection. How to differentiate Jumpbox VM from other VMs. The output will be like this VM is Jumpbox VM.

I have tried to retrive ip addresses, security rules, tags associated with it, But all are same with other VMs, So I am not getting efficient output.I need to find unique configuration jumpbox vm has from other vms. The output will be like this "This VM is Jumpbox VM".

Upvotes: 0

Views: 141

Answers (1)

SiddheshDesai
SiddheshDesai

Reputation: 8195

I deployed one Jumpbox VM with its NIC connected to the Private IP securely and ran this PowerShell command to get the Jumpbox VM's.

Thanks @Fabricio godboy for the script. Reference- How to write powershell script to identify whether the Azure Virtual machine has Jumpbox or bastionhost or other private vms - Microsoft Q&A

I created one Jumpbox VM and ran the Powershell script like below:-

Script:-

#
Connect-AzAccount
# Specify the resource group name and VM name
$resourceGroupName = "jumpboxvm"
$vmName = "jumpboxvm12"

# Get the VM object
$vm = Get-AzVM -ResourceGroupName $resourceGroupName -Name $vmName

# Get the VM's network interface object
$nic = Get-AzNetworkInterface -ResourceId $vm.NetworkProfile.NetworkInterfaces[0].Id

# Get the VM's private IP address
$privateIpAddress = $nic.IpConfigurations.PrivateIpAddress

# Check if the VM is connected to a jumpbox
$jumpbox = Get-AzVM -ResourceGroupName $resourceGroupName -Name "jumpboxvm12"
if ($jumpbox) {
    $jumpboxNic = Get-AzNetworkInterface -ResourceId $jumpbox.NetworkProfile.NetworkInterfaces[0].Id
    $jumpboxPrivateIpAddress = $jumpboxNic.IpConfigurations.PrivateIpAddress
    if ($privateIpAddress -eq $jumpboxPrivateIpAddress) {
        Write-Host "This VM is connected to a jumpbox"
        exit
    }
}

And got the output like below:-

enter image description here

Upvotes: 0

Related Questions