Hello @Priya Dharshini M
Its not simple write this.
but I believe you are looking for this:
# Authenticate with Azure
Connect-AzAccount
# Specify the resource group name and VM name
$resourceGroupName = "resource-group-name"
$vmName = "vm-name"
# 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 "jumpbox-name"
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
}
}
# Check if the VM is connected to a Bastion host
$bastion = Get-AzBastionDeployment -ResourceGroupName $resourceGroupName
if ($bastion) {
$bastionSubnet = $bastion.SubnetId
if ($nic.IpConfigurations.Subnet.Id -eq $bastionSubnet) {
Write-Host "This VM is connected to a Bastion host"
exit
}
}
# Check if the VM is connected to other private network VMs
$vnet = Get-AzVirtualNetwork -ResourceGroupName $resourceGroupName -Name $nic.IpConfigurations.Subnet.VirtualNetwork.Name
$otherPrivateIps = $vnet.AddressSpace.AddressPrefixes | Where-Object { $_ -ne $nic.IpConfigurations.Subnet.AddressPrefix } | ForEach-Object {
$ip = [System.Net.IPAddress]::Parse($_.Split("/")[0])
[System.BitConverter]::ToUInt32($ip.GetAddressBytes(), 0)
}
if ($otherPrivateIps -contains [System.BitConverter]::ToUInt32([System.Net.IPAddress]::Parse($privateIpAddress).GetAddressBytes(), 0)) {
Write-Host "This VM is connected to other private network VMs"
exit
}
Write-Host "This VM is not connected to a jumpbox, Bastion host, or other private network VMs"
Please, if this help you, don't forget to accept and upvote this answer
good luck
Regards