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#
The solution for bug #2 was apparently not covering all possibilities.
Next try.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
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
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
Yes if you have the chance and you can that will be brilliant!!
Thanks LuCD
The attached script should do the trick.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
Registered VMs
$vms = @()
foreach($vmMoRef in $ds.Vm){
$vm = Get-View $vmMoRef
$row = "" | Select Cluster, Datastore, VM, PowerState, Path
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
I get
1
VirtualMachine
vm-838
The argument cannot be null or empty.
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
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
The solution for bug #2 was apparently not covering all possibilities.
Next try.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
still
Cannot index into a null array.
+ $row.VM = $matches[ <<<< 1]
hows something like this?
get-cluster |` %{$cluster = $_; get-vm -Location $_ | %{$vm = $_; $dsNames = get-datastore -vm $_ | % {$_.name}; write-host $cluster.name $vm.name $dsNames } }
The bug is finally solved
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