PowerShell Friday: Copying files with Copy-VMGuestFile
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?
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:
- 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
Other articles in the series PowerShell Friday:
- PowerShell Friday: Getting Started with PowerShell and PowerCLI
- PowerShell Friday: Connecting to vCenter
- PowerShell Friday: Starting VMs
- PowerShell Friday: stopping VMs
- PowerShell Friday: Creating Virtual Machines
- PowerShell Friday: Snapshots
- PowerShell Friday: Adding CPU’s with PowerCLI
- PowerShell Friday: Adding Memory with PowerCLI
- PowerShell Friday: ExtensionData
- PowerShell Friday: Retrieving IP addresses for VMs
- PowerShell Friday: Copying files with Copy-VMGuestFile
- PowerShell Friday: Setting Reservations with PowerCLI
- PowerShell Friday: Enabling SSH with PowerCLI
- PowerShell Friday: Christmas Special
- PowerShell Friday: Configuring vSphere MTU Size
- PowerShell Friday: Load PowerCLI from your own script
- PowerShell Friday: Using the Cisco ACI API
- action-ebs-item by Dimitri De Swart
- Create Custom Names with vRealize Automation 8 by Dimitri De Swart
- Gain insight in Kubernetes by Dimitri De Swart
- Kubernetes persistent volumes with NetApp Trident - Part 2 by Dimitri De Swart
- Using input parameters to create dynamic blueprint… by Anne Jan Elsinga