Jump to content
Welcome to our new Citrix community!
  • 0

Retrieve Guest VM Network Information From XenServer 6.5 with Powershell


Alvin Adorno

Question

Greetings,

 

I just want to leave this script here that maybe help others. Recently we have to retrieve all the NIC MAC Address from all our guest VM and their guest OS IP. To solve this tedious task I write this simple code that could evolve in something more robust if someone tweak the code.

 

As is the code retrieve the following from the VM:

  1. VM UUID
  2. VM Name
  3. VM Description
  4. # of CPU
  5. Memory in Bytes
  6. VM Power State
  7. Is Template
  8. NIC ID (The number of the NIC device)
  9. Network Name (Attached to the NIC)
  10. NIC MAC Address
  11. NIC VLAN (From the Network Attached)
  12. NIC IP (From the Guest OS)

I run this code on multiple XenServer 6.5 & 6.0 using the XenServer 6.5 SDK without issues. The only thing that I notice is that on XS6.0 the VLAN data can not be read. Also the XenTools must be installed to retrieve the OS IP address. 

 

Any suggestions would be appreciated. 

-----------Script Start------------------

 

Import-Module XenServerPSModule
Connect-XenServer -Url https://10.20.10.xx -UserName serveruser -Password serverpassword
$vms = get-xenvm | Where-Object {$_.is_a_template -ne "False" -and $_.is_control_domain -ne "False"} #get all vms

function New-XenVMInfo
{
New-Object PSObject -Property @{
Name = ''
UUID = ''
CPUCount = ''
Description = ''
IsTemplate = ''
MemoryStaticMax =''
PowerState = ''
NICID = ''
NICNetworkName = ''
NICMAC = ''
NICVLAN = ''
NICIP = ''
}

}

$xenVMs = @()


foreach ($vm in $vms){
$gm = Get-XenVMProperty -VM $vm -XenProperty GuestMetrics
if ($vm.VIFs.Count -gt 1)
{
for ($i=0; $i -lt $vm.VIFs.Count;$i++)
{
$xenVM = New-XenVMInfo
$xenVM.Name = $vm.name_label
$xenVM.PowerState = $vm.power_state
$xenVM.UUID = $vm.uuid
$xenVM.Description = $vm.name_description
$xenVM.IsTemplate = $vm.is_a_template
$xenVM.MemoryStaticMax = $vm.memory_static_max
$xenVM.CPUCount = $vm.vcpus_max
$vif = Get-XenVIF -Ref $vm.VIFs[$i]
$net = Get-XenNetwork -Ref $vif.network
$xenVM.NICNetworkName = $net.name_label
$pif = Get-XenPIF -Ref $net.PIFs[0]
$xenVM.NICVLAN = $pif.VLAN
$xenVM.NICMAC = $vif.MAC
$xenVM.NICID = $vif.device
$xenVM.NICIP = ($gm.networks)[ $vif.device + "/ip"]
$xenVMs += $xenVM
}
}
elseif ($vm.VIFs.Count -eq 1)
{

$xenVM = New-XenVMInfo
$xenVM.Name = $vm.name_label
$xenVM.PowerState = $vm.power_state
$xenVM.UUID = $vm.uuid
$xenVM.Description = $vm.name_description
$xenVM.IsTemplate = $vm.is_a_template
$xenVM.MemoryStaticMax = $vm.memory_static_max
$xenVM.CPUCount = $vm.vcpus_max
$vif = Get-XenVIF -Ref $vm.VIFs[0]
$net = Get-XenNetwork -Ref $vif.network
$xenVM.NICNetworkName = $net.name_label
$pif = Get-XenPIF -Ref $net.PIFs[0]
$xenVM.NICVLAN = $pif.VLAN
$xenVM.NICMAC = $vif.MAC
$xenVM.NICID = $vif.device
$xenVM.NICIP = ($gm.networks)[ $vif.device + "/ip"]
$xenVMs += $xenVM

}
else
{
$xenVM = New-XenVMInfo
$xenVM.Name = $vm.name_label
$xenVM.PowerState = $vm.power_state
$xenVM.UUID = $vm.uuid
$xenVM.Description = $vm.name_description
$xenVM.IsTemplate = $vm.is_a_template
$xenVM.MemoryStaticMax = $vm.memory_static_max
$xenVM.CPUCount = $vm.vcpus_max
$xenVM.NICID = 'No NIC Attached'
$xenVM.NICNetworkName = 'No NIC Attached'
$xenVM.NICVLAN = 'No NIC Attached'
$xenVM.NICMAC = 'No NIC Attached'
$xenVM.NICIP = 'No IP Available'
$xenVMs += $xenVM
}
}

$xenVMs | Export-Csv -Path c:\NetworkInfo.csv

Disconnect-XenServer

 

Link to comment

2 answers to this question

Recommended Posts

  • 0

Hi Alvin,

 

nice work!

 

just some thoughts for additions...

 

instead of: Connect-XenServer -Url https://10.20.10.xx -UserName serveruser -Password serverpassword

 

I prefer: Connect-XenServer -Url https://10.20.10.xx -Creds $creds

 

this allows to use: $creds = Get-Credential -Message "Enter Credentials for Xenserver Logon..."  at the beginning of the script

 

which asks interactively for logon credentials and stores user's input encrypted in memory --> more secure and no need to include readable credentials in the script.

 

----

 

I added code to retrieve the Xenserver Hostname that the VMs are running on:

 

function New-XenVMInfo
{
New-Object PSObject -Property @{
Name = ''
UUID = ''
HOSTNAME = ''
CPUCount = ''
Description = ''
IsTemplate = ''
MemoryStaticMax =''
PowerState = ''
NICID = ''
NICNetworkName = ''
NICMAC = ''
NICVLAN = ''
NICIP = ''
}

}

 

and added the following line (bold face) in all places after the UUID line:

 

  for ($i=0; $i -lt $vm.VIFs.Count;$i++)
            {
            $xenVM = New-XenVMInfo
            $xenVM.Name = $vm.name_label
            $xenVM.PowerState = $vm.power_state
            $xenVM.UUID = $vm.uuid
            $xenVM.HOSTNAME = (Get-XenHost -ref $vm.resident_on).hostname
            $xenVM.Description = $vm.name_description

 

best regards

 

Harald

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...