5 Replies Latest reply on Aug 1, 2016 6:11 AM by LucD

    PowerCLi Script to copy ISO files to specific Datastore folder.

    swamynaveen Novice

      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   

      }  

        • 1. Re: PowerCLi Script to copy ISO files to specific Datastore folder.
          LucD Guru
          Community WarriorsvExpertUser Moderators

          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: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: PowerCLi Script to copy ISO files to specific Datastore folder.
            swamynaveen Novice

            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.

            • 3. Re: PowerCLi Script to copy ISO files to specific Datastore folder.
              LucD Guru
              Community WarriorsUser ModeratorsvExpert

              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: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
              • 4. Re: PowerCLi Script to copy ISO files to specific Datastore folder.
                swamynaveen Novice

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

                • 5. Re: PowerCLi Script to copy ISO files to specific Datastore folder.
                  LucD Guru
                  Community WarriorsvExpertUser Moderators

                  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: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz