PowerCLI – list virtual machines with physical SCSI controllers & inventory path within vcenter

For a project, we needed to implement a VEEAM Backup & Replication setup. During the project we encountered some virtual machines with Physical RDMs. VEEAM cannot handle this, so it was needed to write a script allowing us to list all virtual machines with their most interesting parameters, such as:

  • Virtual Machine Name
  • Version
  • PowerState
  • HardDisks
  • ProvisionedSpaceGB
  • Datacenter
  • Cluster
  • InventoryPath
  • HasPhysicalController

The function Get-VMFolderPath can be found on this blog.


Connect-VIServer VCENTER.domain.local
function Get-VMFolderPath
{
<# .Synopsis Get vm folder path. From Datacenter to folder that keeps the vm. .Description This function returns vm folder path. As a parameter it takes the current folder in which the vm resides. This function can throw either 'name' or 'moref' output. Moref output can be obtained using the -moref switch. .Example get-vm 'vm123' | get-vmfolderpath Function will take folderid parameter from pipeline .Example get-vmfolderpath (get-vm myvm123|get-view).parent Function has to take as first parameter the moref of vm parent folder. DC\VM\folfder2\folderX\vmvm123 Parameter will be the folderX moref .Example get-vmfolderpath (get-vm myvm123|get-view).parent -moref Instead of names in output, morefs will be given. .Parameter folderid This is the moref of the parent directory for vm.Our starting point.Can be obtained in serveral ways. One way is to get it by: (get-vm 'vm123'|get-view).parent or: (get-view -viewtype virtualmachine -Filter @{'name'= 'vm123'}).parent .Parameter moref Add -moref when invoking function to obtain moref values .Notes NAME: Get-VMFolderPath AUTHOR: Grzegorz Kulikowski LASTEDIT: 09/14/2012 NOT WORKING ? #powercli @ irc.freenode.net .Link http://psvmware.wordpress.com #>

param(
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[string]$folderid,
[switch]$moref
)

$folderparent=get-view $folderid
if ($folderparent.name -ne 'vm'){
if($moref){$path=$folderparent.moref.toString()+'\'+$path}
else{
$path=$folderparent.name+'\'+$path
}
if ($folderparent.parent){
if($moref){get-vmfolderpath $folderparent.parent.tostring() -moref}
else{
get-vmfolderpath($folderparent.parent.tostring())
}
}
}else {
if ($moref){
return (get-view $folderparent.parent).moref.tostring()+"\"+$folderparent.moref.tostring()+"\"+$path
}else {
return (get-view $folderparent.parent).name.toString()+'\'+$folderparent.name.toString()+'\'+$path
}
}
}

$report = @()

ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) {
ForEach ($Cluster in ($Datacenter | Get-Cluster | Sort-Object -Property Name)) {
ForEach ($VM in ($Cluster | Get-VM | Sort-Object -Property Name)) {
$vminfo = {} | select Name,Version,PowerState,HardDisks,ProvisionedSpaceGB,Datacenter,Cluster,InventoryPath,HasPhysicalController
$vminfo.name = $VM.Name
$vminfo.version = $VM.Version
$vminfo.PowerState = $VM.PowerState
$vminfo.HardDisks = $VM.HardDisks.Count
$vminfo.ProvisionedSpaceGB = $VM.ProvisionedSpaceGB
$vminfo.Datacenter = $Datacenter.Name
$vminfo.cluster = $Cluster.Name

if ($vminfo.InventoryPath -ne "") {
$vminfo.InventoryPath = Get-VM -Name $vm.Name | Get-VMFolderPath
}
else {
echo $vminfo
}

foreach ($scsidevice in (Get-ScsiController -VM $VM.Name)){
if ($scsidevice.bussharingmode -eq "Physical") {
$vminfo.HasPhysicalController = "YES"
break
} else {
$vminfo.HasPhysicalController = "NO"
}
}
$report += $vminfo
echo $report
}
}
}

$report | Export-Csv C:\test.csv -NoTypeInformation

Disconnect-VIServer

Leave a Reply