Solved

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

Posted on 2009-07-14
13
1,167 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

When rebooting a vCenters 6.0 and try to connect using vSphere Client we get this issue "Invalid URL: The hostname could not parsed." When we get this error we need to do some changes in the vCenter advanced settings to fix the issue.
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Teach the user how to rename, unmount, delete and upgrade VMFS datastores. Open vSphere Web Client: Rename VMFS and NFS datastores: Upgrade VMFS-3 volume to VMFS-5: Unmount VMFS datastore: Delete a VMFS datastore:
Teach the user how to use create log bundles for vCenter Server or ESXi hosts Open vSphere Web Client: Generate vCenter Server and ESXi host log bundle:  Open vCenter Server Appliance Web Management interface and generate log bundle: Open vCenter Se…

691 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