Muhammedali
Contributor
Contributor

Copying files from local PC folder to VM folder

Jump to solution

I would like to copy files from my local PC folder to a Datastorage (which is datastore for my DataCenter )

i used the following commands:

$SourceBuild_ISO = "E:\fileslist\Image\image1.jpg"  #folder on my local PC where i have my vsphereClient installed

Copy-DatastoreItem -Item $SourceBuild_ISO -Destination SAN2:\BHM\BHM_ISO_IMAGE -Force

where SAN2 is my data storage for my VM

the error i get is cannot find drive named SAN2

however if i use the following commands

Get-Datastore -Server <my VM server IP> -Name "SAN2"

i get

Name                               FreeSpaceMB      CapacityMB
----                               -----------      ----------
SAN2                                   3938452         4766208

now i use

$datastorename

= Get-Datastore -Server <my VM server IP> -Name "SAN2" | New-DataStoreDrive -Name datastorename

Copy-DatastoreItem -Item

$SourceBuild_ISO -Destination datastorename:\BHM\BHM_ISO_IMAGE -Force

This works. (New-DataStoreDrive makes a mapping to the actual SAN2 datastore)

however, New-DatastoreDrive is an alias command (for New-PSDrive) which sometimes gives the error that New-DataStoreDrive is not a valid cmdlet and sometimes it works well

if i would like a simple copy of files from my local PC folder to a Datastorage (which is datastore for my DataCenter )

can i not use the following commands directly? why do i need to map a drive to the DataStore??

$SourceBuild_ISO = "E:\fileslist\Image\image1.jpg"  #folder on my local PC where i have my vsphereClient installed

Copy-DatastoreItem -Item $SourceBuild_ISO -Destination SAN2:\BHM\BHM_ISO_IMAGE -Force

where SAN2 is my data storage for my VM

Any Explanations? Suggestions?

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

The path to your datastore that you have to provide on the -Destination parameter needs to start with a psprovider drive.

Try it like this

New-PSDrive -Location (Get-Datastore SAN2)  -Name ds -PSProvider VimDatastore -Root '\'

Copy-DatastoreItem -Item $SourceBuild_ISO -Destination ds:\BHM\BHM_ISO_IMAGE -Force

You can also use the vmstore or vmstores drives.

These 2 are there automatically after a Connect-VIServer.

Since these drives start higher up in the vSphere hierarchy you will have to adapt the destination path.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
13 Replies
ShantanuVCP
Contributor
Contributor

Use WinSCP to copy files from your desktop to the ESX Server. WinSCP is freeware and is easily downloadable.

0 Kudos
LucD
Leadership
Leadership

The path to your datastore that you have to provide on the -Destination parameter needs to start with a psprovider drive.

Try it like this

New-PSDrive -Location (Get-Datastore SAN2)  -Name ds -PSProvider VimDatastore -Root '\'

Copy-DatastoreItem -Item $SourceBuild_ISO -Destination ds:\BHM\BHM_ISO_IMAGE -Force

You can also use the vmstore or vmstores drives.

These 2 are there automatically after a Connect-VIServer.

Since these drives start higher up in the vSphere hierarchy you will have to adapt the destination path.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
admin
Immortal
Immortal

Hi,

You can also use the DatastoreBrowserPath property of the datatore object, which is already calculated full path under the vmstores: drive.

[PowerCLI] Get-Datastore Storage1 | select DatastoreBrowserPath                                                                                                                                                                                                                                                                                                          
DatastoreBrowserPath                                                                                                                                                                         
--------------------                                                                                                                                                                         
vmstores:\testServer@443\Datacenter\Storage1   

So you can build your destination path using this property:

Copy-DatastoreItem -Source ... -Destination (Join-Path $san.DatastoreBrowserPath "Subfolder1\Subfolder2")

Vitali

PowerCLI Team

0 Kudos
Muhammedali
Contributor
Contributor

Thanks LucD for the suggestions. I tired it out and its working.

Any explanation why the New-DataStoreDrive, works sometimes and does not work sometimes??

If i now want to  map the CDDVDDrive of the VM to that particular folder on the DataStore, would i have to again use New-PSDrive to map a drive to the Vim datastore ?

can i do it directly this way

$dv=Get-CDDrive -VM $VMname

Set-CDDrive -CD $dv -IsoPath "[SAN2] /BHM/BHM_ISO_IMAGE/Setup.iso" -Confirm:$false

where SAN2 is my datastore drive used above?

or will i have to create a Mapped drive first?

0 Kudos
LucD
Leadership
Leadership

To map a CD to an ISO on your datastore, you do not need to map a psdrive.

Just use the path format you mention.

Don't forget the blank between the datastore and the folders !

[datastorename] folder/filename.iso


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Muhammedali
Contributor
Contributor

After using

New-PSDrive -Location (Get-Datastore SAN2)  -Name ds -PSProvider VimDatastore -Root '\'

Copy-DatastoreItem -Item $SourceBuild_ISO -Destination ds:\BHM\BHM_ISO_IMAGE -Force

I can successfully copy a file to the DataStore Location.

However if a try to copy another file with same name, (different size) with an intention of overwriting the existing file at Vim DataStore, it shows a succesful copy, but in actual the old file still exist at the DataStore location?

Do i need to explicitly delete the previous file and then copy another one?

How do i delete a file on the datastore via PCLI cmdlets? I didnt com across the Delete-DatastoreItem cmdlet?

Thanks

0 Kudos
LucD
Leadership
Leadership

Normally the -Force parameter will make sure that files with the same name are overwritten.

Could it be a refresh problem in the psdrive ?

Do you also see the first file when you use the Browse Datastore option in the vSphere client ?

When you have a psdrive you can use the 'regular' cmdlets to manipulate the objects in that psdrive.

You can do

Remove-Item -Path ds:\BHM\BHM_ISO_IMAGE\MyFile.iso

or use the alias

rm -Path ds:\BHM\BHM_ISO_IMAGE\MyFile.iso


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Muhammedali
Contributor
Contributor

Hi LucD,

for the ISO mapping of the CD/DVD Drive of the VM to an .iso file on the datastore,

I used;

$dv=Get-CDDrive -VM $VMName
Set-CDDrive -CD $dv -IsoPath "[SAN2] /BHM/BHM_ISO_IMAGE/Setup.iso" -Confirm:$false

the command executes well, along with a display of a progress bar.

plus $? returns a TRUE value

however at my VM console, there is no effect on the CD/DVD Drive. (I expected to be able to explore the CDDVD Drive and view the contents)

however when i do the mapping of the CD/DVD Drive of the VM manually via vSphere Client UI, it works perfectly fine.

Any Explanations?

0 Kudos
LucD
Leadership
Leadership

Stupid quest perhaps, but is the CD drive Connected ?

Get-VM MyVM | Get-CDDrive | Select *

show the ConnectionState as Connected:true ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Muhammedali
Contributor
Contributor

There was an issue with the refresh.. Thanks on the update its working well now..

0 Kudos
Muhammedali
Contributor
Contributor

Hi LucD,

I used:

Get-VM $MyVM | Get-CDDrive | Select *

and it shows ConnectionState: Connected:False

So do i need to make the ConnectionState ON? how do i do it explicitly? How come while using vsphereClient UI, this issue does not appear?

Thanks alot for your explanations..

0 Kudos
LucD
Leadership
Leadership

On the Set-CDDrive cmdlet use the -Connected switch.

Set-CDDrive -CD $dv -IsoPath "[SAN2] /BHM/BHM_ISO_IMAGE/Setup.iso"  -Confirm:$false -Connected:$true


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Muhammedali
Contributor
Contributor

Excatly, yup i just tired that, and then read your reply.. Smiley Happy so its a double confirmation from your reply too..

Thanks LucD..

0 Kudos