VMware Cloud Community
gboskin
Enthusiast
Enthusiast
Jump to solution

Scipt to list datastores in cluster and all vm associated.

I need to write a scipt to list all the datatores in a specified cluster and all vm associted to them staing if powered on or off

Cheers#

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The solution for bug #2 was apparently not covering all possibilities.

Next try.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
15 Replies
LucD
Leadership
Leadership
Jump to solution

With the current cmdlets it's not possible to make the link cluster-datastore-vm.

That's why I went for the SDK objects.

$clusterName = <cluster-name>
$report = @()

$cluster = Get-Cluster $clusterName | Get-View
foreach($dsMoRef in $cluster.Datastore){
	$ds = Get-View -Id $dsMoRef
	foreach($vmMoRef in $ds.Vm){
		$vm = Get-View $vmMoRef
		$row = "" | Select Cluster, Datastore, VM, PowerState
		$row.Cluster = $cluster.Name
		$row.Datastore = $ds.Info.Name
		$row.VM = $vm.Name
		$row.PowerState = $vm.Summary.Runtime.PowerState
		$report += $row
	}
}
$report


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
gboskin
Enthusiast
Enthusiast
Jump to solution

thanks LuCD works a treat , however if i want to also pick up the VM that are in the datastores but not in the inventory (Orphaned VMs) ....will that be possible

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, that should be possible.

Have a look at , that script searches all datastores for VMX files that represent guests that are not in the Inventory and then registers these guests.

It shouldn't be too difficult to incorporate part of that script in the script above to also list the orphaned guests.

If you want, I can see what I can come up with later today.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
gboskin
Enthusiast
Enthusiast
Jump to solution

Yes if you have the chance and you can that will be brilliant!!

Thanks LuCD

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The attached script should do the trick.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
gboskin
Enthusiast
Enthusiast
Jump to solution

Hi LuCD thanbks for the script ...however i seem to be getting " The argumnent cannot be null or empty error"

Looks like on $vm before $row ????

$cluster = Get-Cluster $clusterName | Get-View

foreach($dsMoRef in $cluster.Datastore){

$ds = Get-View -Id $dsMoRef

  1. Registered VMs

$vms = @()

foreach($vmMoRef in $ds.Vm){

$vm = Get-View $vmMoRef

$row = "" | Select Cluster, Datastore, VM, PowerState, Path

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Could it be that you have a datastore with no VMs on there ?

Could you add a line for debugging purposes ?

...
foreach($dsMoRef in $cluster.Datastore){
	$ds = Get-View -Id $dsMoRef
	# Registered VMs
	$vms = @()
# Debug
       $ds.Vm.Count
# End debug     
	foreach($vmMoRef in $ds.Vm){
# Debug
	   $vmMoRef.Type
	   $vmMoRef.Value
# End debug     
	   $vm = Get-View $vmMoRef

...

Does this display 0 (zero) for a datastore ?

Does the 2nd debug entry show lines like

VirtualMachine
vm-4711

for all guests on all datastores ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
gboskin
Enthusiast
Enthusiast
Jump to solution

I get

1

VirtualMachine

vm-838

The argument cannot be null or empty.

0 Kudos
gboskin
Enthusiast
Enthusiast
Jump to solution

Yes there are two datstores with noting in them also .. ..Whisch is what I am looking for actually ( Monthly Tidy Up) ..datastores not been used and Vm not registred as well as powered on and powered off VM

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I think I found the problem.

There were 2 bugs in the script.

Try the new, attached version.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
gboskin
Enthusiast
Enthusiast
Jump to solution

Cannot index into a null array.

At :line:49 char:22

+ $row.VM = $matches[ &lt;&lt;&lt;&lt; 1]

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The solution for bug #2 was apparently not covering all possibilities.

Next try.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
gboskin
Enthusiast
Enthusiast
Jump to solution

still

Cannot index into a null array.

+ $row.VM = $matches[ &lt;&lt;&lt;&lt; 1]

0 Kudos
esarakaitis
Enthusiast
Enthusiast
Jump to solution

hows something like this?

get-cluster |`
  %{$cluster = $_; get-vm -Location $_ |
    %{$vm = $_; $dsNames = get-datastore -vm $_ | % {$_.name};
      write-host $cluster.name $vm.name $dsNames
    }
  }

http://www.vmwarescripting.com

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The bug is finally solved Smiley Wink

The cause was a "dash" in the name of a guest.

The regex mask I used in the script didn't foresee that character.

Attached the script that now can handle all valid characters in guest names.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos