Copy-VMGuestFile

I had a requirement to copy files to some web server VMs running in a DMZ that I could not access across the network. The only connectivity I had to them was via the Virtual Machine Console and logging on as a local user. The files to be copied were from an internal application server, I was adding a vDisk to this application server, disconnecting it and then adding the vDisk to the web servers (as there was a datastore shared between the hosts the application serer and web servers were running on).

I then discovered the PowerCLI command Copy-VMGuestFile. Using the following command I could copy the file I required from the application server to my local workstation.

Copy-VMGuestFile -Source E:\MyApp\WarFile\MyAPP.war -Destination D:\Temp\MyApp.wap  -GuestToLocal -VM MYAPPServer01

I could then copy the file up to the Web Servers with the following command

Copy-VMGuestFile -Source D:\Temp\MyApp.war -Destination D:\Tomcat\webapps\MyApp.war -LocalToGuest -VM MYAPPWeb01 -GuestUser Administrator -GuestPassword xxxxxxxxxx

Note on the command to copy the file FROM the application server I use the parameter -GuestToLocal and on the command to copy the file TO the web server I use -LocalToGuest. Also because the user I am logged on to my local workstation as does not have any permissions on the web server I have to specify a user account and password that does have permissions on the web server, I used the local administrator account, with the parameters -GuestUser Administrator -GuestPassword xxxxxxxxxx, I didn‘t need to specify an account on the command to copy from the application server because the account I was running the PowerCLI session under had permissions to this server.

If the file already exists in the destination you need to include the -Force parameter, e.g.

Copy-VMGuestFile -Source E:\MyApp\WarFile\MyAPP.war -Destination D:\Temp\MyApp.war -GuestToLocal -VM MYAPPServer01 -Force
Copy-VMGuestFile -Source D:\Temp\MyApp.war -Destination D:\Tomcat\webapps\MyApp.war -LocalToGuest -VM MYAPPWeb01 -GuestUser Administrator -GuestPassword xxxxxxxxxx -Force

Instead of listing the Guest Username and Password on the command line you could prompt for the credentials instead, e.g.

Copy-VMGuestFile -Source D:\Temp\MyApp.war -Destination D:\Tomcat\webapps\MyApp.war -LocalToGuest -VM MYAPPWeb01 -GuestCredential (Get-Credential)

As I had a number of web servers to copy the file to and they were all named MYAPPWeb with two digits at the end, e.g. MYAPPWeb01, MYAPPWeb02, e.t.c. I used the following to copy the file to all of them

$webcred = Get-Credential -Message “Please provide credentials for the Web Servers”
Copy-VMGuestFile -Source E:\MyApp\WarFile\MyAPP.war -Destination D:\Temp\MyApp.war -GuestToLocal -VM MYAPPServer01 -Force
ForEach($WebServer in Get-VM MYAPPWeb??) {
Copy-VMGuestFile -Source D:\Temp\MyApp.war -Destination D:\Tomcat\webapps\MyApp.war -LocalToGuest -VM $WebServer -GuestCredential $webcred -Force
}

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s