Link to home
Start Free TrialLog in
Avatar of stephen-spike
stephen-spikeFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How can I use PowerShell with ViToolkit to query the vmx file locations

I use Powershell with the ViToolkit to run various scripts on my VC environment.
I'm currently trying to figure out if there is a simple way to query the VMX file location for a list of virtual servers on a cluster listed within a text file.

Avatar of za_mkh
za_mkh
Flag of United Kingdom of Great Britain and Northern Ireland image

I must get into this quickly!
Hope this puts you on the right path.
http://communities.vmware.com/thread/135266
You must have also come across this on your travails: http://blogs.vmware.com/vipowershell/
I guess you don't want a listing of all VM's in a cluster, just a list of them, hence choosing them from a file?
 
 
Avatar of stephen-spike

ASKER

I actually need to pull the vmx locations as the script will then be used to 'add to inventory' on a new VC cluster.
Background:
VC Cluster 1 - old infrastructure.
VC Cluster 2 - new infrastructure.

Both clusters have visibility to the same LUNs so I can migrate VMs from VC Cluster 1 to VC Cluster 2 (remove from inventory / add to inventory)
Issue: This is a production environment so I wish to script this process over night. I have been investigating this of course myself, and it  is looking like it will be quite a complex script. From the 'remove from inventory' to browsing the vmx location and 'adding to inventory'.


Avatar of Bastiaan
Something like:
$report = @()
Get-VM | Foreach {
 $row = "" | select Name, Disk, CapacityGB
 Foreach ($Disk in $_.HardDisks){
  $row.Name = $_.Name
  $row.Disk = $Disk.Filename
  $row.CapacityGB = ($Disk.CapacityKB / 1MB)
  $report += $row
 }
}
$report
 
 
To move vm's from one host to another:
get-vmhost startesxhost.fq.dn | get-vm | move-vm -Destination (Get-vmhost toesxhost.fq.dn)
Thanks bleeuwen, but I will need to remove the VM from the old VC server inventory, then once removed, add it to the inventory on the new VC server inventory.
Powershell does not (as far as I can see) migrate between VC databases.


In fact if this can be achieved just by talking to the esx hosts themselves then this would also help.
Connect to VC and unregister:
Make sure your virtuals are down before stop-vm VMNAME
You should create a script to unregister the virtual from the old VC like: (get-vm VMNAME | get-view).unregisterVM()
Then disconnect and connect to the new VC:
register the virtuals
start the virtuals
I've not all the right commands (just started last week with this scripting)
A script to register all vmx files which aren't registerd yet: http://communities.vmware.com/message/1152243#1152243
Excellent bleeuwen. The unregister works a treat. However, how can I now register that VM into the the VC server?
I guess I'd need to know the VMX file location. SO the question is how can I grab the VMX location prior to unregistering the VM?

Once I have that data, I'll be closer to being able to register the VM in VC.


Didn't the script:
$report = @()
Get-VM | Foreach {
$row = "" | select Name, Disk, CapacityGB
Foreach ($Disk in $_.HardDisks){
 $row.Name = $_.Name
 $row.Disk = $Disk.Filename
 $row.CapacityGB = ($Disk.CapacityKB / 1MB)
 $report += $row
}
}
$report
gave you the vmdk locations? (i do not have access to my esx environment so i can not check)
Bleeuwen, that script isn't suitable. It locates the VMDKs and the LUNs they exist on. The VMX files do not always reside on the same LUN as the VMDK files.

However, the script in the link you mention (http://communities.vmware.com/message/1152243#1152243) is almost there. It just needs refinement so that alternatively to just import on VMs listed in a text file.
Ok, some WWW searching tells me that the link http://pubs.vmware.com/vi3/sdk/ReferenceGuide/vim.vm.FileInfo.html indicates that the property vmPathName for the object VirtualMachineFileInfo gives me the VMX locations.
However I have little to no .Net experience. So, I assume this can be used to pull the locations of the VMs I specify in a file. Then use this data to then register the VMs on the new VC Server.

Question is, what does the script look like?

Help is greatly appreciated.
Steve
ASKER CERTIFIED SOLUTION
Avatar of stephen-spike
stephen-spike
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial