Highlighted
Enthusiast
Enthusiast

PowerCLi Script to copy ISO files to specific Datastore folder.

Jump to solution

I would need help to copy ISO files to a specific Datastore/LUN and a specific folder. I've tried below mentioned script but it's only copy to the Specific LUN. But i wanna copy to specific folder within LUN.

$DS = Get-VMHost -Name "hqidvpztvh02.hqh.intra.aexp.com"|Get-Datastore "T2-Test-LUN01" | select name,DatastoreBrowserPath    

$source = "F:\ESXi550-201602001.zip"                                                                 

foreach ($datastore in $ds){    

Copy-DatastoreItem -Item $source -Destination  $datastore.DatastoreBrowserPath -Recurse   

}  

0 Kudos
1 Solution

Accepted Solutions
Highlighted
User Moderator
User Moderator

Use the VimDatastore provider, something like this

$fileName = 'F:\ESXi550-201602001.zip'

$tgtFolder = 'Test'

$ds = Get-VMHost -Name "hqidvpztvh02.hqh.intra.aexp.com" | Get-Datastore "T2-Test-LUN01"

New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root "\" > $null

if(!(Test-Path -Path "DS:/$($tgtFolder)")){

    New-Item -ItemType Directory -Path "DS:/$($tgtFolder)" > $null

}

Copy-DatastoreItem -Item $fileName -Destination "DS:/$($tgtFolder)"

Remove-PSDrive -Name DS -Confirm:$false


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

View solution in original post

0 Kudos
5 Replies
Highlighted
User Moderator
User Moderator

Use the VimDatastore provider, something like this

$fileName = 'F:\ESXi550-201602001.zip'

$tgtFolder = 'Test'

$ds = Get-VMHost -Name "hqidvpztvh02.hqh.intra.aexp.com" | Get-Datastore "T2-Test-LUN01"

New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root "\" > $null

if(!(Test-Path -Path "DS:/$($tgtFolder)")){

    New-Item -ItemType Directory -Path "DS:/$($tgtFolder)" > $null

}

Copy-DatastoreItem -Item $fileName -Destination "DS:/$($tgtFolder)"

Remove-PSDrive -Name DS -Confirm:$false


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

View solution in original post

0 Kudos
Highlighted
Enthusiast
Enthusiast

Thanks LucD, suggested script is working fine for single server. But i wanna copy to multiple servers which are located in different Clusters. while running below script it was throwing errors.

$fileName = 'F:\ESXi550-201602001.zip'

$tgtFolder = 'ISO'

$ds = Get-VMHost -Name "hpesxvh41","hpesxvh42" | Get-Datastore "T3-Test-Up-1","T3-Test-Up-2"

New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root "\" > $null

if(!(Test-Path -Path "DS:/$($tgtFolder)")){

    New-Item -ItemType Directory -Path "DS:/$($tgtFolder)" > $null

}

Copy-DatastoreItem -Item $fileName -Destination "DS:/$($tgtFolder)"

Remove-PSDrive -Name DS -Confirm:$false

Error:

=====

New-PSDrive : Cannot bind parameter 'Datastore' to the target. Exception setting "Datastore": "Invalid location type. Location accepts only VIDatastore objects."

At line:6 char:23

+ New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root "\" > $null

+                       ~~~

    + CategoryInfo          : WriteError: (:) [New-PSDrive], ParameterBindingException

    + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Commands.NewPSDriveCommand

New-Item : Cannot find drive. A drive with the name 'DS' does not exist.

At line:9 char:5

+     New-Item -ItemType Directory -Path "DS:/$($tgtFolder)" > $null

+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (DS:String) [New-Item], DriveNotFoundException

    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.NewItemCommand

Copy-DatastoreItem : Cannot find drive. A drive with the name 'DS' does not exist.

0 Kudos
Highlighted
User Moderator
User Moderator

The Location parameter on the New-PSDrive cmdlet expects a single datastore object.

If you have multiple, you will have to run through the datastores in a foreach loop.


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

0 Kudos
Highlighted
Enthusiast
Enthusiast

Thanks LucD. Could you please help me to get this by for-each loop

0 Kudos
Highlighted
User Moderator
User Moderator

Try something like this.

You probably have to update the Get-Datastire cmdlet with your specific requirements.

Also note that this script will copy the ISO to every datastore that is selected

$fileName = 'F:\ESXi550-201602001.zip'

$tgtFolder = 'Test'

$datastores = Get-VMHost -Name "hqidvpztvh02.hqh.intra.aexp.com" | Get-Datastore

foreach($ds in $datastores){

    New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root "\" > $null

   

    if(!(Test-Path -Path "DS:/$($tgtFolder)")){

        New-Item -ItemType Directory -Path "DS:/$($tgtFolder)" > $null

    }

    Copy-DatastoreItem -Item $fileName -Destination "DS:/$($tgtFolder)"

    

    Remove-PSDrive -Name DS -Confirm:$false

}


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