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
Related
I'm trying to ge the DNS servers of network interfaces via WMI that are static (placed by the user). I have this script that works, except for the static part of course:
Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object {$_.DNSServerSearchOrder -ne $null} | Select DnsServerSearchOrder,Index,InterfaceIndex
This leads to an output like so:
DnsServerSearchOrder Index InterfaceIndex
-------------------- ----- --------------
{192.168.122.1} 1 6
{1.1.1.1} 2 10
The interface with 192.168.122.1 has it's DNS setup to DHCP so that value is not good for me. How do I filter out interfaces where dns is not static? Any ideas?
netsh interface ip show config:
Configuration for interface "Ethernet"
DHCP enabled: Yes
IP Address: 192.168.122.130
Subnet Prefix: 192.168.122.0/24 (mask 255.255.255.0)
Default Gateway: 192.168.122.1
Gateway Metric: 0
InterfaceMetric: 35
DNS servers configured through DHCP: 192.168.122.1
Register with which suffix: Primary only
WINS servers configured through DHCP: None
Configuration for interface "Ethernet 2"
DHCP enabled: Yes
IP Address: 10.0.0.17
Subnet Prefix: 10.0.0.0/24 (mask 255.255.255.0)
Default Gateway: 10.0.0.1
Gateway Metric: 0
InterfaceMetric: 35
Statically Configured DNS Servers: 1.1.1.1
Register with which suffix: Primary only
WINS servers configured through DHCP: None
Notice the difference in terms between Statically Configured DNS Servers and DNS servers configured through DHCP. I figured I might parse this output but I'm not sure if I can rely on this text if windows language/locale is changed and I'd rather use the WMI interface if possible.
I thought this might be available in the System.Net.NetworkInformation namespace, but evidently not. I looked through a few lower-level Windows networking APIs thinking certainly it must exist somewhere in there, but no such luck. After running dumpbin on netsh.exe to see what kinds of libraries/functions it's consuming, I did get an idea of one other place to look: the registry.
As it happens, if you look in the registry under the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\ key you will see a key for each network interface with the name in GUID format. Within an interface's key you will find two values of interest: DhcpNameServer and NameServer. On my client system where the DNS server is set by DHCP, DhcpNameServer contains that DNS server's IP address and NameServer contains an empty [String]. If I manually set a DNS server while keeping the automatically-assigned address for the interface itself, NameServer then contains that manually-set DNS server address while DhcpNameServer still contains the same DNS server specified by DHCP.
Based on these observations, it would seem that...
The DhcpNameServer value always contains the DNS server(s) specified by DHCP.
The NameServer value always contains the DNS server(s) specified manually.
When you query the system for its nameservers (e.g. via the Win32_NetworkAdapterConfiguration.DNSServerSearchOrder property) the result will contain the value of NameServer, if provided, otherwise the value of DhcpNameServer, if provided. In other words, the system tells you which DNS servers are currently being used, but not how their addresses were specified.
To determine if an interface has manually-assigned DNS servers, check if NameServer has a non-empty value.
Thus, given an interface with ID $interfaceID, you can build a path to its registry key like this...
$interfaceKeyPath = "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\$interfaceID"
...and then retrieve the dynamically-assigned and manually-assigned nameserver values like this...
Get-ItemProperty -Path $interfaceKeyPath -Name 'DhcpNameServer', 'NameServer'
That just leaves the matter of from where you get the value for $interfaceID, and there are numerous sources although the trick will be excluding undesirable interfaces (for example, on my Windows 10 system I have a packet capture loopback adapter and a hypervisor adapter that I'd want to be excluded from such a query). The most compatible way (dating back to .NET 2.0) would be the Id property of the NetworkInterface class...
[System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() `
| Select-Object -ExpandProperty 'Id'
...although the only useful properties on which to filter are Name and NetworkInterfaceType.
On Windows Vista and above the Win32_NetworkAdapter class provides a GUID property...
Get-WmiObject -Class 'Win32_NetworkAdapter' -Property 'GUID' -Filter 'PhysicalAdapter = true'
...although even when filtering on PhysicalAdapter it still returns the loopback and hypervisor adapters and I'm not seeing any definitive property or class relation that can be used to select only hardware adapters.
The Win32_NetworkAdapterConfiguration class is much the same...
Get-WmiObject -Class 'Win32_NetworkAdapterConfiguration' -Property 'SettingID'
...with no properties to filter out non-hardware or even non-physical adapters.
On (I think) Windows 8 and above there's the Get-NetConnectionProfile cmdlet...
Get-NetConnectionProfile | Select-Object -ExpandProperty 'InstanceID'
which is documented to get "a connection profile associated with one or more physical network adapters" and, on my system, it does only return my physical adapter.
There is also the Get-NetAdapter cmdlet...
Get-NetAdapter -Physical `
| Where-Object -Property 'EndPointInterface' -NE -Value $true `
| Select-Object -ExpandProperty 'InterfaceGuid'
I found that passing the -Physical parameter excluded the hypervisor adapter but not the loopback adapter, so filtering out where EndPointInterface is $true was necessary to eliminate that. The HardwareInterface and Virtual properties might also be of interest.
Another option would be to invoke the Get-NetAdapterHardwareInfo cmdlet, which seems to know how to distinguish true hardware adapters, and let that determine which adapters are retrieved by Get-NetAdapter...
Get-NetAdapterHardwareInfo `
| Get-NetAdapter `
| Select-Object -ExpandProperty 'InterfaceGuid'
The Get-Net* cmdlets above return CIM instances so, for example, instead of Get-NetAdapter -Physical you could use something like...
Get-WmiObject -Namespace 'Root\StandardCimv2' -Class 'MSFT_NetAdapter' `
-Property 'InterfaceGuid' -Filter 'HardwareInterface = true AND EndPointInterface = false'
to retrieve the MSFT_NetAdapter instances just the same. I'm not really sure what the guidance is on using one versus the other. It would seem like one should prefer the cmdlets, and yet, unlike WMI/CIM, they offer limited/no parameters for efficiently filtering the output or specifying which properties are desired so you have to do that in the pipeline. I think it's noteworthy, though, that I wasn't able to find any current documentation for these MSFT_* class; they all say they're no longer updated, except for the MSFT_NetConnectionProfile class for which I couldn't find any documentation page at all. That says to me Microsoft doesn't want you relying on any definite structure of these classes, and yet if the cmdlets just pass along those class instances...I'm not sure how can you meaningfully and reliably interact with them if nothing is documented.
Also, keep in mind that you'll want to prefer Get-CimInstance and its ilk over Get-WmiObject, where possible. I don't think I've yet encountered an instance where it was any more complicated than changing Get-WmiObject to Get-CimInstance, although there are more differences (not necessarily bad) than the name.
After enough bashing I found a solution but I'm not 100% positive is the right path. On all DHCP DNS servers the DNS values contain a single ip address and that IP address is equal to the Default Gateway value. When those values match we're dealing with a DHCP DNS server and not statically configured.
is there any way to get all VM's expanded properties and not just the ones Get-azurermvm brings, using powershell?
I was trying to use Select but I don't know where to get all the names. The ones on the Portal's Column won't work (bring back empty fields)
and if I use get-member, they don't bring anything because I think they are nested inside...
As I mentioned in my comment, this is not trivial because an Azure VM is not a self contained thing like a Hyper-V VM. There is no single source of truth because it is made of many components, and you would need to collect the information from those components individually. For example: Getting all of a VM's IP addresses.
To start getting IPs for a machine you get the VM info:
$VM = Get-AzureRmVm -ResourceGroup $RG -Name 'MyUberVM'
Now you can look at the network profile for the VM, which will list the Network Interface objects that are associated with VM, but those have all of 2 properties, Primary and Id. The Primary property is just what it sounds like, it specifies the primary network interface if you have more than one. The Id property will have the full ResourceId for the Network Interface object, something like:
/subscriptions/12345abc-0000-1111-2222-ssl430asd432/resourceGroups/MyVMRG/providers/Microsoft.Network/networkInterfaces/myubervm01715
Now we can use that to get the actual Network Interface object a couple ways, but the simplest is to just run Get-AzureRmResource against it, and pipe that to Get-AzureRmNetworkInterface.
$NIs = $VM.NetworkProfile.NetworkInterfaces.Id|%{Get-AzureRmResource -ResourceId $_}|Get-AzureRmNetworkInterface
That will get you a collection of Network Interface objects. Each of these will have several properties, but the one we care about is the IpConfigurations property. Each IpConfig of each Network Interface will have a PrivateIpAddress property, and a PublicIp property. The PrivateIp property is just a string, so we can use that, but the PublicIp is an object, so we will need to refer to it's IpAddress property. Now, this is going to potentially return more than one of each of those depending on how many network interfaces you have associated with a given VM, so I'll join them with ', ' just to be safe. To break that down, we can do:
$PrivateIps = $NIs.IpConfigurations.PrivateIpAddress -join ', '
$PublicIps = $NIs.IpConfigurations.PublicIp.IpAddress -join ', '
Thus ends the walk-through of getting the IPs for an Azure VM. That takes care of 2 of your suggested columns. Admittedly, two of the harder ones, but still just 2 of them none the less. Once you go through and get all of the data you need for each individual VM I'd make a custom object for it, and output those custom objects to your CSV file.
If my understanding is right, get-azurermvm could get all vms in your subscription, but you could not get detailed properties. You could use Get-AzurermVM get-azurermvm -ResourceGroupName $rg -Name $vm.Name to get detailed properties. You could try to use the following scripts.
##resource group name
$rg="<>"
$vms=get-azurermvm -ResourceGroupName $rg
ForEach ($vm in $vms) {
get-azurermvm -ResourceGroupName $rg -Name $vm.Name
}
Note: You could use get-azurermvm -ResourceGroupName $rg -Status to get VM's status information.
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
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
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