?
Solved

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

Posted on 2009-07-14
13
Medium Priority
?
1,169 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
Teach the user how to delpoy the vCenter Server Appliance and how to configure its network settings Deploy OVF: Open VM console and configure networking:
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

752 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