VMware

This Question is Answered

1 "correct" answer available (10 pts)
8 Replies Last post: Nov 17, 2009 4:20 AM by alasdair.carn…  

Copy files (vmx) , how to script it ?? posted: Apr 30, 2009 2:21 AM

Click to view ugauchr's profile Novice 6 posts since
Jan 10, 2007

Hi,

Quite new in VI Toolkit / Powershell but for a SRM project I need to automate so stuff.

Is there a way that a can take a copy from a vmx file and rename it. (Will be stored in the same folder / datastore but with different name)

Assume can't be so difficult but it didn't get it work yet?

Any idea, some scriptlets

Thanks

Christof

Re: Copy files (vmx) , how to script it ??

1. Apr 30, 2009 5:10 AM in response to: ugauchr
Click to view LucD's profile Champion 2,443 posts since
Oct 31, 2005
Afaik there is currently no VITK cmdlet that lets you do this.
But we can use the SDK method CopyDatastoreFile_Task to accomplish this task.

$vmName = <VM-name>

$vmImpl = Get-VM $vmName
$vm = $vmImpl | Get-View

$fileMgr = Get-View FileManager
$source = $vm.Config.Files.VmPathName
$sourceDc = ($vmImpl | Get-Datacenter | Get-View).MoRef
$dest = $source.Replace(".vmx","-COPY.vmx")
$destDc = $sourceDc
$force = $false
$taskMoRef = $fileMgr.CopyDatastoreFile_Task($source,$sourceDc,$dest,$destDc, $force)
$task = Get-View -Id $taskMoRef 
while($task.Info.State -eq "running" -or $task.Info.State -eq "queued"){
  sleep 2
  $task.UpdateViewData("Info.State")
}



Note that I just added "-COPY" to the filename.
I.e. the file "ABC.vmx" becomes "ABC-COPY.vmx".
But you can of course change this ;-)

Re: Copy files (vmx) , how to script it ??

4. Apr 30, 2009 3:44 AM in response to: ugauchr
Click to view LucD's profile Champion 2,443 posts since
Oct 31, 2005
I'm not sure I understand exactly which rights you mean.
Wat are you trying to do with the copied VMX file ?

Re: Copy files (vmx) , how to script it ??

6. Apr 30, 2009 5:34 AM in response to: ugauchr
Click to view LucD's profile Champion 2,443 posts since
Oct 31, 2005
Ok, I understand.
To change the permissions you need to use the plink tool.
This should do what you want
$vmName = <VM-name>
$vm = Get-VM $vmName | Get-View
$logdir = $vm.Config.Files.LogDirectory
$logdir -match "\[(\w+)\]"
$dummy = $dsName = $matches[1]

$User = <ESX-account>
$Pswd = <ESX-password>
$Computer = (Get-View -Id $vm.Runtime.Host).Name
$plink = "<path to Putty directory>\plink.exe"
$plinkoptions = " -v -batch -pw $Pswd"
$cmd1 = 'chmod 754 /vmfs/volumes/' + $dsName + '/' + $vmName + '/' + $vmName + '-COPY.vmx'

$remoteCommand = '"' + $cmd1 + '"'
$command = $plink + " " + $plinkoptions + " " + $User + "@" + $computer + " " + $remoteCommand

$msg = Invoke-Expression -command $command 

Note that you will have to set up sudo for this to work.
See Script to run single/multiple command on all ESX host for details.

Re: Copy files (vmx) , how to script it ??

8. Nov 17, 2009 4:20 AM in response to: LucD
Click to view alasdair.carnie's profile Enthusiast 47 posts since
May 18, 2007
Hi Luc,

Could this be adapted to check for the exsistance of a VM or Template and if it does not exist then copy the VM or Template from one datastore to another?

I am storing my templates in a datastore that cannot be seen by my students ESX hosts, but I need to deploy lots of VMs from this template to the student servers. At the moment I am manually copying the VM Template from the protected Datastore to one the students can see and then running a powershell script to register the template, add the student ESX hosts to a vCenter and then deploy 16 VMs (two onto each host). I'd like to store a copy of the VM Template on the public store, but be able to check that it is there before trying to deploy the VMs, in case a student has deleted the template by mistake.

I tried just deploying the template from the protected datastore, but the performance is really bad. Any help would be appreciated.

VMware Developer

SDKs, APIs, Videos, Learn and much more in the Developer community.

Learn More

Developer Sample Code

Increase your developer productivity with VMware API sample code.

Learn More

VMworld Sessions & Labs

Online access to the latest VMworld Sessions & Labs and online services.

Learn more

Purchase PSO Credits Online

Purchase credits to redeem training and consulting services online.

Buy Now

Community Hardware Software

View reported configurations or report your own.

Learn More

VMware vSphere

Come witness the next giant leap in virtualization.

Register Today

Communities