jvm2016
Hot Shot
Hot Shot

register vms to inventory_powercli

Hi Luc,

I got following from your blog .couldyou please suggest if   orange code will loop to all datastores as i see resultof one datastore only .

this question to related to get registered count from all datastores.

$Cluster = "MyCluster"

$Datastores = "MyDS"

$VMFolder = "MyFolder"

$ESXHost = Get-Cluster $Cluster | Get-VMHost | get-random

foreach($Datastore in Get-Datastore $Datastores) {

# Collect .vmx paths of registered VMs on the datastore

$registered = @{}

Get-VM -Datastore $Datastore | %{$_.Extensiondata.LayoutEx.File | where {$_.Name -like "*.vmx"} | %{$registered.Add($_.Name,$true)}}

# Set up Search for .VMX Files in Datastore

New-PSDrive -Name TgtDS -Location $Datastore -PSProvider VimDatastore -Root '\' | Out-Null

$unregistered = @(Get-ChildItem -Path TgtDS: -Recurse | `

where {$_.FolderPath -notmatch ".snapshot" -and $_.Name -like "*.vmx" -and !$registered.ContainsKey($_.Name)})

Remove-PSDrive -Name TgtDS

#Register all .vmx Files as VMs on the datastore

foreach($VMXFile in $unregistered) {

New-VM -VMFilePath $VMXFile.DatastoreFullPath -VMHost $ESXHost -Location $VMFolder -RunAsync

}

}

do we need to write like below to get correct registered vms from all datastore

$Cluster = "cluster1"

$Datastores = Get-Datastore -RelatedObject $cluster

#$VMFolder = "MyFolder"

$ESXHost = Get-Cluster $Cluster | Get-VMHost | get-random

$fragments = @()

foreach($Datastore in $Datastores)

{

$vms_registered=Get-VM -Datastore $Datastore | %{$_.Extensiondata.LayoutEx.File | where {$_.Name -like "*.vmx"}}

$fragments += $vms_registered

}

$fragments.count

0 Kudos
7 Replies
LucD
Leadership
Leadership

Just change line 7 (in VMX Raiders Revisited) with this

foreach($Datastore in Get-Cluster -Name $Cluster | Get-Datastore) {


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

0 Kudos
jvm2016
Hot Shot
Hot Shot

well i think it is still the same .asi have defined this earlier as .

$datastores=Get-Cluster -Name $Cluster | Get-Datastore

0 Kudos
LucD
Leadership
Leadership

If you only want to find the registered VMs on the datastores, you could just do

Get-CLuster -Name MyCluster | Get-Datastore | Get-VM


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

0 Kudos
jvm2016
Hot Shot
Hot Shot

is there one liner for unregistered also and then register them to vcenter.

0 Kudos
LucD
Leadership
Leadership

I could write a very long one-liner to do that, but that wouldn't really qualify as a one-liner :smileygrin:

Seriously, you have to scan the datastore, and then for each VMX you encounter, check if it belongs to a registered VM or not.

The script in VMX Raiders Revisited is doing just that.


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

0 Kudos
jvm2016
Hot Shot
Hot Shot

thnaks for your patience and response .Smiley Happy

i cant check it right now as we dont have testenvironemt but is it calulating registered and unregistered vms from all datastores ??

as i checked for registered pice of code it collected for one datastore not for a array of datastore.

so imthinking the same thing for unregistered piece of code .

0 Kudos
LucD
Leadership
Leadership

It check for one datastore at the time.

But as answered earlier, you can do this in a loop for each datastore in a cluster.

If you just want to list the VMX files for unregistered VMs, you leave out the New-VM line (and test just before it).


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

0 Kudos