3 Replies Latest reply on Dec 19, 2017 9:50 PM by LucD

    Import-VApp Times Out When Run With -RunAsync Parameter

    anthonyjvoss Lurker

      I'm deploying an OVA using the Import-VApp PowerCLI cmdlet. When I run the cmdlet without -RunAsync it sits at 10% complete for a while and then finally completes successfully.

       

      In an effort to run in parallel and as a background task, I attempted to run with the -RunAsync parameter as I have 300+ VMs to provision in this manner.  Adding this parameter executes the process as a background task, however it throws a timeout error after approximately 5 minutes every time.

       

      Is this a known bug or is this related to the HttpNfcLease post here (Import-VApp slow & times out )

       

      I did find a single unanswered post on Reddit by a user expressing the same issue (https://www.reddit.com/r/powercli/comments/62jo3y/importvapp_times_out_when_using_runasync/ )

       

      Any feedback would be much appreciated!!  Thanks!

       

      PowerCLI Version 6.3 Release 1

      ESXi Host Version 5.5.0

        • 1. Re: Import-VApp Times Out When Run With -RunAsync Parameter
          LucD Guru
          Community WarriorsvExpertUser Moderators

          Did you already try changing the timeout with the Set-PowerCLIConfiguration cmdlet and the WebOperationTimeoutSeconds parameter?

           

          PS: is there a reason why you are using an older PowerCLI version?

          If not I would advise to go for a more recent release.

          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: Import-VApp Times Out When Run With -RunAsync Parameter
            anthonyjvoss Lurker

            Hey LucD, thanks for your response.  I set the WebOperationTimeoutSeconds at 1800 but the task still timed out after just over 5 minutes (~5 min 10 sec).

             

            I'm currently restricted to this version of PowerCLI due to a compatibility issue w/another environment we manage.  I will try and see if I can attempt the deployment from another server running a newer version of PowerCLI.

            • 3. Re: Import-VApp Times Out When Run With -RunAsync Parameter
              LucD Guru
              Community WarriorsUser ModeratorsvExpert

              Can you try with the following script?

              It talks to the HttpNfc directly.

               

              $ovfPath = 'C:\OVFFolder'

              $ovfName = 'vApp1'

               

              $appName = 'TestVApp1'

              $clusterName = 'MyCluster'

              $dsName = 'MyDS'

               

              Get-VApp -Name $appName -ErrorAction SilentlyContinue | Remove-VApp -DeletePermanently -Confirm:$false

               

              $si = Get-View ServiceInstance

              $ovfMgr = Get-View -Id $si.Content.OvfManager

              $cluster = Get-Cluster -Name $clusterName

              $rp = Get-ResourcePool -Name Resources -Location $cluster

              $folder = Get-Folder -Name vm -Type VM

              $ds = Get-Datastore -Name $dsName

               

              $ovfDescriptor = Get-Content -Path "$($ovfPath)\$($ovfName).ovf" | Out-String

              $cisp = New-Object VMware.Vim.OvfCreateImportSpecParams

              $cisp.Locale = ''

              $cisp.DeploymentOption = ''

              $cisp.EntityName = $appName

              $iSpec = $ovfMgr.CreateImportSpec($ovfDescriptor,$rp.ExtensionData.MoRef,$ds.ExtensionData.MoRef,$cisp)

               

              $leaseMoRef = $rp.ExtensionData.ImportVApp($iSpec.ImportSpec,$folder.ExtensionData.MoRef,$cluster.ExtensionData.Host[0])

              $lease = Get-View -Id $leaseMoRef

              while($lease.State -eq [VMware.Vim.HttpNfcLeaseState]::initializing){

                  sleep 1

                  $lease.UpdateViewData('State')

              }

              $lease.UpdateViewData()

               

              if($lease.State -eq [VMware.Vim.HttpNfcLeaseState]::ready){

                  Write-Host "Lease timeout $($lease.Info.LeaseTimeout)"

               

                  $leaseRefreshAction = {

                      $lease.HttpNfcLeaseProgress([int](($bytesWritten + $readFromFile)/$totalBytes*100))

                  }

                  $headers = @{"Content-Type" = "application/x-vnd.vmware-streamVmdk"}

                  $bufferSize = 10KB

                  $Timeout = 300

               

                  $bytesWritten = 0

                  $totalBytes = $iSpec.FileItem | Measure-Object -Property Size -Sum | Select -ExpandProperty Sum

               

                  $leaseRefreshTimer = New-Object Timers.Timer

                  $leaseRefreshTimer.Interval = 1000  # Refresh every 1 second

                  Register-ObjectEvent -InputObject $leaseRefreshTimer -EventName Elapsed –SourceIdentifier LeaseTimer -Action $leaseRefreshAction > $null

                  $leaseRefreshTimer.Start()

               

                  foreach($device in $lease.Info.DeviceUrl){

                      $file = $iSpec.FileItem | where{$_.DeviceId -eq $device.ImportKey}

                      Write-Host "Transferring $($file.Path)"

                      $ovfFile = "$($ovfPath)\$($file.Path)"

                      $url = $device.Url

               

                      [System.Net.HttpWebRequest] $webRequest = [System.Net.WebRequest]::Create($url)

                      $webRequest.Timeout = $Timeout

                      $webRequest.Method = "POST"

                      $webRequest.ContentType = "application/x-vnd.vmware-streamVmdk"

                      $webRequest.AllowWriteStreamBuffering = $false

                      $webRequest.SendChunked = $true

               

                      $requestStream = $webRequest.GetRequestStream()

                      $fileStream = [System.IO.File]::OpenRead($ovfFile)

                      $chunk = New-Object byte[] $bufferSize

                      $readFromFile = 0

                      while($bytesRead = $fileStream.Read($chunk,0,$bufferSize)){

                          $readFromFile += $bytesRead

                          $requestStream.Write($chunk, 0, $bytesRead)

                          $requestStream.Flush()

                      }

               

                      $responceStream = $webRequest.GetResponse()

               

                      $FileStream.Close()

                      $requestStream.Close()

               

                      $responceStream.Close()

               

                      $bytesWritten += $file.Size

                      $lease.HttpNfcLeaseProgress([int]($bytesWritten/$totalBytes*100))

                      Write-Host "Bytes written $($bytesWritten)"

                  }

                  $leaseRefreshTimer.Stop()

                  Unregister-Event -SourceIdentifier LeaseTimer

               

                  $lease.HttpNfcLeaseComplete()

              }

              else{

                  Write-Host "Import failed"

                  Write-Host $lease.Error[0].LocalizedMessage

              }

              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz