Get VM Name and FQDN - azure-powershell

Im trying to write a Powershell Query to get the VM Name & FQDM, this is what I have but its not returning the FQDN
Get-AzureRmVm | Select-Object Name , #{Name="FullyQualifiedDomainName"; Expression={$_.ToPSVirtualMachine().FullyQualifiedDomainName.Value}} | Format-Table
Update 1
So I have been able to get the VM, and then the FQDN, and I feel its safe enough for me to do it this was because i know a single RSG will only even contain one VM and one FQDN, so its safe for me to assume they are linked.
Get-AzureRmVm | Foreach-Object{Get-AzureRmPublicIpAddress -ResourceGroupName $_.ResourceGroupName} | Select-Object ResourceGroupName, Name , Location , #{Name="FQDN"; Expression={$_.DnsSettings.Fqdn}}
However it is slow because of the Foreach, wondered if anyone has any other suggestions on this.

When you create an Azure VM, a public IP resource for the VM is automatically created. Refer to this. None of FQDN is created for a VM in the Azure portal. You could configure it once the VM is created. You could not get the FQDN directly by using the AzureRM.Compute module since the FQDN or DNS name is not a property of VM but is a public IP address configuration.
You can get an FQDN of an Azure VM using
(Get-AzureRmPublicIpAddress -ResourceGroupName $rg -Name $publicipaddress ).DnsSettings.Fqdn

Related

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".
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:-

How to get own vm name in guest environment

I want to change VM setting in my own guest VM.
I can connect to vCenter but I'm not detecting my own VM.
What is a simple way to get the VM name?
Pretty sure you can use a simple Powershell command like so:
get-vmmserver localhost | get-vm "vmname" | select Name, ComputerName,
HostName | format-list
PowerCLI allows you to do this simply with:
Connect-VIServer -Server vcenterservername.fqdn
Get-VM -Name vmname
You can also just connect to the web client and perform a search at: https://vcenterservername.fqdn/

how to connect from one ec2 instance to another ec2 instance through ssh

I have two amazon ec2 instances
i can connect to those ec2 instance from my windows using putty (by the public key generated from the private key provided by amazon)
now i want to install tungsten replicator into my ec2 instances
and tungsten replicator needs ssh access from one ec2 instance to another ec2 instance
i tried to check that ssh is working or not from one ec2 instance to another
i tried:
ssh ec2-user#public ip of destination instance
//also tried
ssh ec2-user#private ip destination instance
but its not working
i got following error:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic)
i have search on google and tried some trick but none of them worked
sometime i got following error:
Address public_ip maps to xxxx.eu-west-1.compute.amazonaws.com, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
can anyone please tell me how to connect ssh from one ec2 instance to another
I'd suggest you to create a special keypair for the tungsten user.
cd tungsten-user-home/.ssh
ssh-keygen -t rsa
mv id-rsa.pub authorized-keys
And then copy both files to the other host in the same place and permissions.
This will allow tungsten to work without requiring your own key.
Just like when you have to ssh from you local machine to an EC2 instance, you need to provide the ssh command the proper pem file:
ssh -i my_pem_file.pem ec2-user#private-or-public-ip-or-dns
Just in case anyone ponder on this question, here is my 2 cents.
Connecting one EC2 instance from another EC2 instance will work as suggested by "Uri Agassi". Considering best practices and security, it will be good idea to create and assign a role to source EC2 instance.
One way to allow one EC2 instance to connect to another is to set an ingress rule on the target EC2 instance that lets it accept traffic from the source EC2 instance's security group. Here's a Python function that uses Boto3 to do this:
import boto3
ec2 = boto3.resource('ec2')
def allow_security_group_ingress(target_security_group_id, source_security_group_name):
try:
ec2.SecurityGroup(target_security_group_id).authorize_ingress(
SourceSecurityGroupName=source_security_group_name)
logger.info("Added rule to group %s to allow traffic from instances in "
"group %s.", target_security_group_id, source_security_group_name)
except ClientError:
logger.exception("Couldn't add rule to group %s to allow traffic from "
"instances in %s.",
target_security_group_id, source_security_group_name)
raise
After you've set this, put the private key of the key pair on the source instance and use it when you SSH from the source instance:
ssh -i {key_file_name} ec2-user#{private_ip_address_of_target_instance}
There's a full Python example that shows how to do this on GitHub /awsdocs/aws-doc-sdk-examples.
See, if you have deployed both machines with the same key pair, or different, it's not a problem just go to your host ec2 machine and in .ssh folder make a key file with the same name of the key that is used to create the second machine, now use chmod 400 keypair name and then try ssh -i keyname user-name#IP

How I can I get my home network's IP address from a shell script?

I have an account at a server at school, and a home computer that I need to work with sometimes. I have exchanged keys, and now only have one problem. While my school account has a name associated with it, "account_name#school", my home network does not. My plan is to have a script that every hour retrieves my home network's IP address, ssh'es into my school account and updates my ssh config file storing my home network's IP address.
How can I retrieve my home computer's IP address from a shell script?
P.S. Is this a sensible plan?
You have two basic choices:
A dynamic DNS address (e.g. dyndns.org). Setting that up is outside the scope of Stack Overflow, but it's probably The Right Thing™ for you.
Use a tool like http://checkip.dyndns.org/ to report your external IP address, then parse the result.
lynx -dump http://checkip.dyndns.org/ | awk '{print $NF}'
Either way, you'll need to configure your router to allow inbound access for SSH, so further information needs to be asked on Super User or Unix & Linux.
My http://ipcalf.com server supports header-based or explicit content negotiation, which is a fancy way of saying that if you curl -H Accept:text/plain ipcalf.com or curl -s http://ipcalf.com/?format=text you should get your public IP address in raw form.
("Full" documentation and source code are on github.)
You can make a request to http://automation.whatismyip.com/n09230945.asp to fetch your public IP from WIMI. This command:
ssh account_name#school "echo -n "$(wget http://automation.whatismyip.com/n09230945.asp -O -)" > some_remote_file"
will write your home public IP to a file called "some_remote_file" in your home folder at school. Set it up with cron and you will have access to your home's routers public IP from your school.
If you've already set up some sort of port forwarding on your router so that you can ssh to your home computer with the routers IP, you should be good to go :)
I figured out a better way to get the IP address then to use an external service. I am SSHing into the server; therefore, the server must know who is SSHing into it, so I can use a tool like who or pinky to find out who is SSHing in.
Here is the command to get the name of the computer logged in.
echo $SSH_CLIENT | awk '{ print $1 }'

Automate Virtual Machine

I am working on a project that is similiar to Mircosoft Virtual Lab. I have created a .aspx (C#) webpage that is able to run powershell script to launch vmware-vmrc console. The webpage is deployed and run on my own computer. I also have try to publish my solution in a server, webpage work well but console do not appear on my screen. I guess it because the script is running on the server instead of the localhost. I will like to know is there any way to launch the console if my solution/programme is build in the server.
$hostname = "localhost:8333"
$vms = "VmName"
$vm = get-vm $vms
$vmid = $vm.id
$gvm = Get-View $vmid
$vmrc = "C:\Program Files (x86)\Common Files\VMware\VMware Remote Console Plug-in\vmware-vmrc.exe"
$vmrcargs = '-h ' + $hostname + ' -m "' + $gvm.Config.Files.VmPathName + '"'
[Diagnostics.Process]::Start($vmrc, $vmrcargs)
in your code you have the hostname hard coded as localhost, in theory you could read this value from the Request object of ASP.NET context and replace it with the client's host name.
Said so, I really doubt it would work because it's not granted that the server has access to the client remotely... even passing proper IP address, and even if I see there is a port specified (8333), there could be firewalls in the between...
of course in an intranet it could work, you have to try this out.
Perform a telnet on the client's name via the port 8333 to check if the port is open.
If the telnet works, then replace "localhost(or dns or Host name) with the IP of the client"
If the the telnet does not work, then open the port.
However if the port is open, it will work.
Kind Regards,
Garish