VMs detailed properties using azure? - powershell

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.

Related

Get-AzureRmResource returns different data depending on how it was called

This just blew my mind. I run these commands to access a particular resource and print out its location:
PS H:\> $hmm = Get-AzureRmResource -ResourceGroupName "RG_NAME" -ResourceName "R_NAME" -ResourceType "Microsoft.ServiceBus/namespaces"
PS H:\> $hmm.Location
East US 2
But if I run these commands I get different data for the same field:
PS H:\> $hmm2 = Get-AzureRmResource | Where-Object {$_.ResourceName -match "R_NAME"}
PS H:\> $hmm2.Location
eastus2
Before you ask, I only have one resource whose name is "R_NAME".
Why is the Azure API returning different values depending on how I try to access the data? Is there some kind of conversion happening in the background on Azure that's normalizing the data or something?
No one except for the devs will be able to answer this question (why this happens exactly). But probably this happens because when you do a get against the subscription you are talking to an Azure Resource provider and when talking to a single resource you are talking to a servicebus provider. And their responses differ. This can happen. Microsoft is a huge company. things like this happen all the time.

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

Add existing vhd to Azure VM using PowerShell Azure Resource Manager cmdlets

NOTE: This is an Azure ARM question - not an Azure Service Management question.
I have 2 vhds in a storage account - machine-os.vhd and machine-data.vhd. I can use PowerShell ARM cmdlets to create a VM with the existing OS vhd, but cannot figure out how to attach the EXISTING data vhd. All the samples I have found create EMPTY data disks - but I want to attach the existing vhd as a data disk.
I've tried to use the createOption attach switch with Add-AzureVMDataDisk like so:
$vm = New-AzureVMConfig ...
$vm = Add-AzureVMDataDisk -VM $vm -VhdUri $existingDiskUri -Name "machine-data.vhd" -Caching ReadOnly -CreateOption attach -DiskSizeInGB 200
However, the operation fails with:
Blob https://x.blob.core.windows.net/vhds/machine-data.vhd already exists. Please provide a different blob URI to create a new blank data disk 'machine-data.vhd.'
I have to specify DiskSizeInGB for the Add-AzureVMDataDisk command to work (which seems strange). I've tried specifying SourceImageUri and a different name for the VhdUri, which, according to the documentation, should copy the vhd from the sourceImageUri to the vhdUri and attach it. Trying createOption fromImage fails because "you cannot specify size with source image uri". However, the size parameter for the cmdlet is mandatory, so I don't know how you could specify an sourceUri and not a size.
This SO question presents a similar issue (though I don't get the same error), but the link in the answer shows a template with EMPTY data disks, which doesn't help.
Interestingly I've tried to add the disk to the VM from the Azure Portal - there you have to specify a name and a URI, but the operation always fails with some strange json parsing error. I can't seem to get the uri for the data disk correct.
After playing around a bit more I found a hack:
Give a new URI for the existing disk and set this as the vhdUri
Use the URI of the existing disk as the sourceImageUri
Call Add-AzureVMDataDisk using the CreateOption fromImage
Set the size of the data disk to null (this is the hack)
When calling New-AzureVM, the existing disk is copied to the new Uri
After creating the VM, I delete the original vhd
Unfortunately you have to supply the DiskSizeInGB parameter to the Add-AzureVMDataDisk command since it's mandatory. However, I set it to null, otherwise the provisioning fails (the error message says that you can't specify both size and sourceImageUri).
Here's the final code:
$vm = New-AzureVMConfig ...
$vm = Add-AzureVMDataDisk -VM $vm `
-SourceImageUri $existingDataDiskUrl -VhdUri $newDataDiskUri `
-Name $newDataDiskName -CreateOption fromImage -Caching ReadOnly `
-DiskSizeInGB 200
# hack
$vm.StorageProfile.DataDisks[0].DiskSizeGB = $null
After that you can call:
New-AzureVM -ResourceGroupName $rgName -Location $location -VM $vm
Once the VM is created, I call Remove-AzureStorageBlob to delete the original disk.
Perhaps there's a cleaner way, but I can't find one. At least this way works in the end.

Adding a generic service to cluster from powershell

I'm a newbie in clustering and I'm trying to create a generic service to a cluster using PowerShell. I can add it without any issues using the GUI, but for some reason I cannot add it from PowerShell.
Following the first example from the documentation for Add-ClusterGenericServiceRole, I've tried the following command:
Add-ClusterGenericServiceRole -ServiceName "MyService"
This throws the following error:
Static network was [network range] was not configured. Please use -StaticAddress to use this network or -IgnoreNetwork to ignore it.
What's the connection between the network and my service? And why aren't these details required when creating it from the GUI?
I also tried another approach, creating the resource with:
Add-ClusterResrouce -Name MyService -ResourceType "Generic Serice"
This command succeeded but I noticed in the GUI that the ServiceName is blank, and thus the actual service cannot be started. If I could somehow change the ServiceName property it should do the trick. Again, from PowerShell I tried the following:
$resource = Get-ClusterResrouce "MyService"
$Resource.ServiceName = "Actual name of service" //property ServiceName cannot be found on this object.
I've been struggling for a couple of hours now with no luck. Is there something basic I'm missing? I think this shouldn't be as complicated as it might look.
I had the same problem; I had to add a large amount of services and got stuck with the "ServiceName" as well.
First, a note on the Add-ClusterGenericServiceRole command: this is for creating the service resource and the role at the same time, as opposed to just adding the service resource to an existing role.
Now, the solution is that you have to set the parameter "ServiceName" with the Set-ClusterParameter command. You can do this for an existing service resource like this:
Get-ClusterResource "ServiceDisplayName" | Set-ClusterParameter -Name ServiceName -Value "ServiceName"
However, you probably want to create the resource with everything it needs in one go, like this:
Add-ClusterResource -Name "ServiceDisplayName" -Group "cluster role" -ResourceType "Generic Service" | Set-
ClusterParameter -Name ServiceName -Value "ServiceName"

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