Solved

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

Posted on 2009-07-14
13
1,154 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
  • 7
  • 5
13 Comments
 
LVL 21

Expert Comment

by:za_mkh
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 13

Expert Comment

by:bleeuwen
Comment Utility
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
Comment Utility
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
Comment Utility
In fact if this can be achieved just by talking to the esx hosts themselves then this would also help.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 13

Expert Comment

by:bleeuwen
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Will try to explain how to use the VMware feature TAGs in the VMs and create Veeam Backup Jobs using TAGs. Since this article is too long, I will create second article for the Veeam tasks.
In this article, I will show you HOW TO: Create your first Windows Virtual Machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, the Windows OS we will install is Windows Server 2016.
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:
Teach the user how to install log collectors and how to configure ESXi 5.5 for remote logging Open console session and mount vCenter Server installer: Install vSphere Core Dump Collector: Install vSphere Syslog Collector: Open vSphere Client: Config…

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now