Every now and then you can run into the situation that you have a file wich needs to be distributed to any number of virtual machines. Now you could logon to these servers and copy the file manually and for maybe a handfull of virtual machines this is doable. But to make it easier on yourself you could also make use of Powershell with Copy-VMGuestFile.

With Powershell you could make use of the cmdlet called Copy-Item to exchange data between your source and destionation.

Copy-Item -Path '\\target\share$\test.txt' -Destination 'c:\temp\'

This cmdlet can be used in most situations, but what if your netwerk isn’t available yet for the virtual machine. Or maybe the srouce or destionation is located in the DMZ?

Copy-VMGuestFile cmdlet

For these kind of situation you could also make use of the Copy-VMGuestFile PowerCLI command. This cmdlet enables an administrator to exchange data between source and destionation through the use of the vSPhere API and the VMware tools.

When using the Copy-VMGuestFile cmdlet you are forced to use atleast a few parameters:

  • Source
  • Destionation
  • VM
  • Direction (GuestToLocal or LocalToGuest)
  • HostUser & HostPassword
  • GuestUser & GuestPassword

If for example you want to copy the text file “test.txt” from your local drive to the folder “c:\temp” on the guest virtual machine named “myVM”, you could use this command:

Copy-VMGuestFile -Source c:\test.txt -Destination c:\temp\ -VM myVM -LocalToGuest -HostUser root -HostPassword pass1 -GuestUser user -GuestPassword pass2

For a reverse task you would only need to adjust the parameter from -LocalToGuest to GuestToLocal

Copy-VMGuestFile -Source c:\test.txt -Destination c:\temp\ -VM myVM -GuestToLocal -HostUser root -HostPassword pass1 -GuestUser user -GuestPassword pass2

Now this works fine when you need to copy data to or from a single virtual machine. But maybe you need to repeat this action on multiple servers. Well then you can make use of a piped input as described in earlier articles.

For example you are in a organisation that is using tnsnames.ora to resolve database names. A new database is added to your enviroment and you want to update all the virtual machines with a new tnsnames.ora file. And this must be applied to all virtual machines that start with the name VM.

$vm = Get-VM -Name VM*  | Copy-VMGuestFile -Source c:\tnsnames.ora -Destination c:\MyFolder -VM $vm -LocalToGuest -GuestUser -GuestPassword pass2

When making use of this cmdlet make sure you take into account these security bullits:

  • The guest user account as the correct rights on the file and/or folder
  • Check if User Account Control (UAC) isn’t preventing you from writing or reading data