VMware administrators who manage complex migration or configuration tasks with vMotion or ESXi should consider...
using PowerCLI cmdlets to copy files to and from local machines and VMware VMs.
VMware created the PowerCLI module on top of PowerShell to enable VMware administrators to interact with vSphere and perform tasks that are similar to the ones available in the vSphere web client. PowerCLI offers a more efficient, programmatic way to interact with vSphere compared to the relatively slow GUI.
PowerCLI is a simple tool for PowerShell users to learn because all of the syntax for the PowerCLI cmdlets follows existing PowerShell methods. Admins can use one of the most useful PowerCLI cmdlets -- Copy-VMGuestFile -- to copy files and folders from a local machine's file system into a vSphere VM file system.
There are a few prerequisites to using Copy-VMGuestFile. Admins must have VM Tools installed on the VMs with which they'll be interacting in vSphere. In the background, this PowerCLI cmdlet uses VM Tools to copy data back and forth.
If the relevant hosts are ESXi 5.0 or lower, some other conditions also apply.
Admins must run the cmdlet on the 32-bit version of Windows PowerShell and have access to the ESX that hosts the VM over TCP VM 902. They must also have VirtualMachine.Interact.ConsoleInteract privileges for vCenter Server, ESX, and ESXi versions earlier than 4.1 or VirtualMachine.Interact.GuestControl privileges for vCenter Server, ESX, and ESXi 4.1 and later.
The PowerCLI cmdlet parameters are almost self-explanatory. Source and Destination refer to the file system locations where admins want to copy data to and from. It's best to use absolute paths. The parameter VM is the name of the VM you want to interact with. There are a few ways to authenticate the VM.
I recommend admins use the GuestCredential parameter because it uses the PSCredential object type. Admins can use -GuestCredential -- Get-Credential -- the same way they would use other PowerShell cmdlets. Admins can also specify a guest user and password separately with the GuestUser and GuestPassword PowerCLI cmdlets. Note that if the local and virtual machines are both joined to Active Directory, it's likely unnecessary for you to use any guest authentication parameter.
The switch parameters GuestToLocal and LocalToGuest refer to the direction in which admins want to copy data. GuestToLocal will copy from a VM to the local machine and LocalToGuest will copy from a local directory to a guest VM directory.
In most cases, admins must be authenticated to vCenter before they can use this PowerCLI cmdlet, which they can do with Connect-VIServer. If admins want to specify the ESXi host on which the VM is running, they can use the HostCredential parameter -- Get-Credential.
Use PowerCLI cmdlets to copy files
This example will show you how to copy a file to a guest VM with PowerCLI cmdlets. First, connect to vCenter.
Next, copy a file located on the local machine -- C:\temp\test.txt -- to the guest VM. I want to copy the file to the location C:\Windows\Temp, so I will specify that in the destination.
Use PowerShell remoting to view the file and check that the PowerCLI cmdlet actually copied the file to the guest VM.
Admins can also copy an entire folder. Note that this PowerCLI cmdlet will also copy any files contained in the folder.
Copy files from VMs to a local machine
In some cases, admins might want to use PowerCLI cmdlets to copy files and folders from VMs to a local or shared machine. To do this, use the source and destination parameters so that the source points to the VM, and then set the destination as a directory on the local computer. The last parameter needed is the -GuestToLocal parameter, which switches the direction in which the data flows.
This example will copy the folder C:\Users\admin\Desktop to the local directory C:\Temp. The cmdlet won't copy the folder itself with this method -- only the contents of the folder. To get around this, specify the folder name in the destination parameter and use the -Force parameter to automatically create the folder. First, however, check that the folder doesn't already exist.
Below, you can see that the folder has been copied:
For VMware users, PowerCLI is a powerful tool that offers a programmatic way to perform tasks that the web client can't. If admins are trying to manage ESXi hosts, perform complex vMotion migrations or configure many servers at once, PowerCLI cmdlets are the best tools for the jobs.
The ability to copy data to and from guest VMs from a local computer can help save time and offer an alternative to traditional methods, such as server message block or Secure Shell.