AzureRm get IP from created virtual machine - powershell

I am using azureRm commads in powershell to create virtual machines in azure. And after i create one I would like to get its IP address into variable, but I could not find any azureRm commands to do so. Could anybody help me?

When you are using AzureRM, the IP is no more linked to the "VM" ressource. You need to get the network adapter of your VM and then the IP configured for this on this ressource adapter. Get-AzureRmNetworkInterface
Hope this help.

I'm sure this can be made a bit more compact/efficient, but here is what I'd use in a pinch:
$vmname = "<your-vm-name>"
$ip = (Get-AzureRmNetworkInterface | Where-Object {($_.VirtualMachine.id).Split("/")[-1] -like $vmname}).IpConfigurations.PrivateIpAddress

Related

Connect to Azure VM from local

i'm absolutely beginner in power-shell scripting and windows automation.so any help would be appreciated.
i have access to a Azure-VM in my local pc at office. There are some tasks that i need to do daily on my local and VM machine. i am trying to automate these tasks with powershell.
At one step i need to copy some CSV files from my VM to local and this is where i'm stuck.
How do i connect to provided AzureVM from my local machine?(i'm not an admin for azure portal)
I have User-Name , Password & my VM name(****.cloudapp.net) to connect in Remote Desktop Connection. i want to know if i can connect to provided VM and copy some files if yes then how!
i have been searching for ways to connect but cant find any solution.
i have installed azure module for powershell, tried Get-AzureVM but this does not work. As i said earlier any help would be really appreciated. thanks
First, please check port 5985 is listening on Azure VM using
netstat -ano
Then you need to add an inbound rule to allow the port 5985 on the NSG associated with the Azure VM in the azure portal. For the error message
The WinRM client cannot process the request
you need to add the remote Azure VM to the local machine's TrustedHosts list by executing the following command in the commands prompt as an administrator:
winrm set winrm/config/client #{TrustedHosts="Azure VM public IP address"}
I can copy a file folder named test from Azure VM to a local folder named tesp with powershell commands as below:
$c = Get-Credential
$sess =New-PSSession -ComputerName ip address -Port 5985 -Credential $c
Copy-Item -FromSession $sess -Path d:\test -Destination c:\tesp -Recurse
More information about Powershell Remoting.

Compiling DSC Config that contains Get-NetAdapter in Azure Automation

As seen in the many Azure Quick Start examples, it is common to use Get-NetAdapter to get the Network Interface Name for things like DNS configuration. This is an example:
configuration MyConfig
{
$Interface=Get-NetAdapter|Where Name -Like "Ethernet*"|Select-Object -First 1
$InterfaceAlias=$($Interface.Name)
Node localhost
{
xDnsServerAddress DnsServerAddress
{
Address = $DNSServer
InterfaceAlias = $InterfaceAlias
AddressFamily = 'IPv4'
}
}
}
If the command Get-NetAdapter is in my configuration and the config is compiled by Azure Automation, I get the following error:
Cannot connect to CIM server. The specified service does not exist as an installed service.
Is there a workaround?
The answer is - its not possible. The configs are compiled on the Azure Automation server, not the target node. Even if I were to find a way to get the network adapter name in the config, it would get the name of the adapter on the DSC pull server, not the target node.
The code in the question will work if you use 1 config per node and you are pre-compiling on the target node then uploading it to Azure Automation.
Try this:
xDnsServerAddress DnsServerAddress
{
Address = $DNSServer
InterfaceAlias = (Get-NetAdapter | ? name -Like "Ethernet*" | select -First 1).Name
AddressFamily = 'IPv4'
}
Get-NetAdapter internally is using WMI to get the information which doesn't work in Azure Automation. However you can use get-netipinterface cmdlet to get information about the adapter.

How to Get MAC Address of VMs with Azure PowerShell

Does anyone know how to get Mac address of vms in Azure through Azure PowerShell?
i know i can get it with WMI or something else inside the VM, but i don't know how can i do that without logging on the VM.
Use the Get-AzureRmNetworkInterface command and the MacAddress property the resulting object has:
(Get-AzureRmNetworkInterface -ResourceGroupName %rgName%).MacAddress
this will list all the macs of the network interfaces in a resource group, to be more specific you could add the -Name parameter.
(Get-AzureRmNetworkInterface -ResourceGroupName %rgName% -Name %nicName%).MacAddress

Azure Powershell Get Public IP of Staging Cloud Service

I am using this answer to look up our cloud service's public ip.
Azure Powershell: Get public virtual IP of service
This only returns the endpoints for the production cloud services. We also need to easily look up our staging cloud service endpoints.
Does anyone know how to look these up with Azure Powershell?
Thanks!
Get-AzureDeployment and System.Net.Dns class will help.
Get-AzureDeployment -ServiceName your_service_name -Slot Staging
You will get lots of properties, including URL
Then you can run following PS command which will give you IP address (where your_url is usually XX.cloudapp.net) for the given domain name:
[System.Net.Dns]::GetHostAddresses("your_url") | foreach {echo $_.IPAddressToString }
I hope that will help.
This is one-liner and uses nothing other then Get-AzureDeployment - and returs directly the VIP, as long as you have at least one InputEndpoint defined - otherwise you can't connect to the service anyway ;) No need of heavy lifting or using DNS client ...
PS C:\> Get-AzureRole -ServiceName "your_service_name" -Slot "staging" -InstanceDetails -RoleName "your_desired_role_name_when_you_have_more_than_one_role" | select {$_.InstanceEndpoints[0].VIP }
And you will get the VIP. The important option here is -InstanceDetails which will get the Endpoints.
Here is the simplest answer I could think of. It returns a string containing the VIP of the service in the slot you specify.
PS C:\> (Get-AzureDeployment -ServiceName 'ServiceName' -Slot Staging).VirtualIPs.Address

Azure Powershell: Get public virtual IP of service

Is it possible to get the public virtual IP (VIP) of an azure service using powershell?
One approach would be to use the Get-AzureEndpoint command
Get-AzureVM -Name "thevmname" -ServiceName "theservicename" | Get-AzureEndpoint | select { $_.Vip }
I'm not sure, but I doubt there is an easy way, because it might change (although it rarely does).
Windows Azure provides a friendly DNS name like “blogsmarx.cloudapp.net” or “botomatic.cloudapp.net.” There’s a reason for providing these (other than simply being prettier than an IP address). These are a necessary abstraction layer that lets the Virtual IP addresses (VIPs) underneath change without disrupting your service. It’s rare for the VIP of an application to change, but particularly thinking ahead to geo-location scenarios, it’s important that Windows Azure reserves the right to change the VIP. The friendly DNS entries provide a consistent interface for users to get to your application.
Source: http://blog.smarx.com/posts/custom-domain-names-in-windows-azure
However, if you get the dns name you could do a dns lookup.
To obtain the Virtual IP of an Azure CloudService deployment via powershell, you can use the Get-AzureService cmdlet combined with the Get-AzureDeployment cmdlet like this:
(Get-AzureService -ServiceName "myCloudService" `
| Get-AzureDeployment -Slot Production).VirtualIPs[0].Address
(Just assign the previous command to, e.g., $CloudServiceIp to plug the IP into subsequent commands.)
You can also get a list of all cloud services and virtual IPs for your subscription by running the following:
Get-AzureService | Select-Object -Property ServiceName, `
#{Name='ProdIP';Expression={(Get-AzureDeployment -Slot Production `
-ServiceName $_.ServiceName).VirtualIPs[0].Address}} | Format-Table -AutoSize