How to Get MAC Address of VMs with Azure PowerShell - 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

Related

VMs detailed properties using azure?

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.

Getting Azure VM OS name using PowerShell

I have been trying to get the VM OS name from Microsoft Azure using PowerShell.
I think I am very close to the solution but I don't know where I'm going wrong.
This is the command that I am using to get the VM details:
Get-AzureRmVM -ResourceGroupName TEST -Name VF-Test1 | Select OsType
The answer I get is just blank.
When running the following command:
Get-AzureRmVM -ResourceGroupName TEST -Name VF-Test1
I get all the details that belong to that VM.
The osType property lives inside $_.StorageProfile.osDisk
Get-AzureRmVM -ResourceGroupName TEST -Name VMNAME |
Format-Table Name, #{l='osType';e={$_.StorageProfile.osDisk.osType}}
Name osType
------ ------
VMNAME Windows
Use https://resources.azure.com to explore the object representation when in doubt, or pipe to Show-Object, like i did below.
You can get resource groups' VMs by Get-AzureRmVM and classic VMs by Get-AzureVM. Both of the returning values of the two cmdlets contain OS type properties but in different paths.
For the cmdlet Get-AzureRmVM, the OS type property path is $vm.StorageProfile.OsDisk.OsType
For the cmdlet Get-AzureVM, the OS type property path is $vm.VM.OSVirtualHardDisk.OS
There exists a sample code about fetching Azure VM OS Type here: https://gallery.technet.microsoft.com/How-to-retrieve-Azure-5a3d3751
Get-AzVM -name SERVERNAME | select name, #{n="OS";E={$_.StorageProfile.OsDisk.OsType}}, #{n="Offer";E={$_.StorageProfile.ImageReference.offer}} , #{n="SKU";E={$_.StorageProfile.ImageReference.sku}}, #{n="Publisher";E={$_.StorageProfile.ImageReference.Publisher}}
RESULT:

AzureRm get IP from created virtual machine

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

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

How to get an instance of Msvm_VirtualSystemGlobalSettingData class for particular VM in Powershell

So, I've been trying my hand at powershell automation of Hyper-V VM administration, and I can't seem to find a reliable way to find the VM Data Root for a given VM. I understand that when programmatically building one, I set it via the Msvm_VirtualSystemGlobalSettingData WMI class. How do I access this class post VM creation?
Thanks!
The GetRelated() method is the key:
PS C:\> $vm = gwmi -namespace root\virtualization Msvm_ComputerSystem -filter 'ElementName="SomeVM"'
PS C:\> $vm.GetRelated("Msvm_VirtualSystemGlobalSettingData")