Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2009-07-14
13
Medium Priority
?
1,171 Views
Last Modified: 2012-05-07
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.

0
Comment
Question by:stephen-spike
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
13 Comments
 
LVL 21

Expert Comment

by:za_mkh
ID: 24848589
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?
 
 
0
 
LVL 1

Author Comment

by:stephen-spike
ID: 24848813
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'.


0
 
LVL 13

Expert Comment

by:bleeuwen
ID: 24848937
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
 
 
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 13

Expert Comment

by:bleeuwen
ID: 24848979
To move vm's from one host to another:
get-vmhost startesxhost.fq.dn | get-vm | move-vm -Destination (Get-vmhost toesxhost.fq.dn)
0
 
LVL 1

Author Comment

by:stephen-spike
ID: 24849113
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.


0
 
LVL 1

Author Comment

by:stephen-spike
ID: 24849493
In fact if this can be achieved just by talking to the esx hosts themselves then this would also help.
0
 
LVL 13

Expert Comment

by:bleeuwen
ID: 24849626
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)
0
 
LVL 13

Expert Comment

by:bleeuwen
ID: 24849721
A script to register all vmx files which aren't registerd yet: http://communities.vmware.com/message/1152243#1152243
0
 
LVL 1

Author Comment

by:stephen-spike
ID: 24849760
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.


0
 
LVL 13

Expert Comment

by:bleeuwen
ID: 24852317
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)
0
 
LVL 1

Author Comment

by:stephen-spike
ID: 24858235
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.
0
 
LVL 1

Author Comment

by:stephen-spike
ID: 24858648
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
0
 
LVL 1

Accepted Solution

by:
stephen-spike earned 0 total points
ID: 24859740
So,

I eventually figured it out myself... (once I also parse the text file with the VMs to be migrated)

Now all I need to do is use the two arrays to register the details in the arrays ($name, $path) to the new VC Server.

Connect-VIServer vcservername
$path = new-object object[] 20
$name = new-object object[] 20
$count = 0
Foreach ($VM in (Get-VM |Get-View))
	{
	$path[$count] = ($VM.Summary.Config.VmPathName )
	$name[$count] = ($VM.Summary.Config.Name)
	$count++
	}

Open in new window

0

Featured Post

Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ransomware is a malware that is again in the list of security  concerns. Not only for companies, but also for Government security and  even at personal use. IT departments should be aware and have the right  knowledge to how to fight it.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Teach the user how to join ESXi hosts to Active Directory domains Open vSphere Client: Join ESXi host to AD domain: Verify ESXi computer account in AD: Configure permissions for domain user in ESXi: Test domain user login to ESXi host:
This video shows you how to use a vSphere client to connect to your ESX host as the root user. Demonstrates the basic connection of bypassing certification set up. Demonstrates how to access the traditional view to begin managing your virtual mac…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question