VMware Cloud Community
drwoodberry
Contributor
Contributor

Import Multiple OVF's

I am trying to figure out if there is a way to deploy multiple ovf's at one time with the powercli. Currently I have a script that deploys one at a time. It looks in a folder for the .ovf file and starts the import. When it is done, it moves to the next. Any way to have it start, let's say 4 imports at a time, and as one finished it starts another until all items are imported?

Reply
0 Kudos
6 Replies
RvdNieuwendijk
Leadership
Leadership

You can use the Import-VApp -RunAsync parameter to return immediately without waiting for the task to complete. You can use the Get-Task cmdlet to retrieve the tasks running on your vCenter server.

Message was edited by: Robert van den Nieuwendijk

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
drwoodberry
Contributor
Contributor

I did not even see that command. The only issue I can see with that though, in one of my deployments there is a maximum of 12 imports that need to occur. I am worried about hammering the system with doing all these at once so I was looking to do them 4 at a time and kick off the next one as one finished. Looking to try something with Start-Job to see if that will work.

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership

To run only 4 deploy ovf's tasks at a time you can do something like:

$maxtasks = 4

foreach ($OVF in (Get-ChildItem -Path *.ovf)){

Import-VApp -RunAsync -Source $OVF.PSPath

while((Get-Task Status "Running" |

        Where-Object {$_.Name -eq "Deploy OVF template"} |

        Measure-Object | Select -ExpandProperty Count) -ge $maxtasks) {

   Start-Sleep -Seconds 5

}

}

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
drwoodberry
Contributor
Contributor

Thanks again for your help. Using the -Runasync switch was able to accomplish what I needed "almost." After I deploy the OVF template, I perform three operations on the object. First I set the network adapters network. Second I set the VM startpolicy on the object, and then I take a snapshot of the machine before powering it on. I have functions for each template I am deploying and depending on some selections made by the user given a special gui, a mix and match of the templates are deployed, hence I take into account some scenarios and perform a switch statement on the scenario they choose and deploy those templates. With a -Runsync command, because the object is not fully created those other tasks are not performed and generate errors. Is there a way to still deploy more than 1 OVF at a time but also make sure those other steps are performed? Sample switch statement and function for the deployment of the OVF it below.

function Baseline

{

  $build = "\Baseline.ovf"

    $path =  $ovf_location + $build

    Write-Host "Creating Baseline Server"

  if ($mycluster -match "Cluster1")

  {

  $datastore = vmlocation1

  $vm = Import-VApp -Source $path -VMHost $vmhost -Datastore $datastore -Name "Baseline" -Runasync

        Get-NetworkAdapter -VM $vm | Set-NetworkAdapter -NetworkName "Servers" -StartConnected:$true -Confirm:$false

        New-Snapshot -VM $vm -Name InitialDeployment

        Start-VM -VM $vm -Confirm:$false -RunAsync

  }

  else

  {

  $datastore = vmlocation2

  $vm = Import-VApp -Source $path -VMHost $vmhost -Datastore $datastore -Name "Baseline" -Runasync

        Get-NetworkAdapter -VM $vm | Set-NetworkAdapter -NetworkName "Servers" -StartConnected:$true -Confirm:$false

        New-Snapshot -VM $vm -Name InitialDeployment

        Start-VM -VM $vm -Confirm:$false -RunAsync

  }

    Write-Host "Created Baseline Server"

}

switch ($Virtual_Machine)

  {

  "All Cluster 1 Servers"

  {

  $null = dc

  $null = master

  $null = databridge

  $null = backup

  $null = services

  $null = jadocs

  $null = sql

  $null = dds

  $null = share

  $null = wave

  $null = media

  $null = cucm

   }

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership

If you use the Import-VApp -Runasync parameter the output of the cmdlet is a vSphere task object and not a virtual machine object. You can use the Wait-Task cmdlet to wait for the task to complete before continuing your script. E.g.

$Task = Import-VApp -Source $path -VMHost $vmhost -Datastore $datastore -Name "Baseline" -Runasync

# do something else

Wait-Task -Task $Task

# do things that only can be done after the vApp is deployed

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
drwoodberry
Contributor
Contributor

You are right about it being a task object. Once it is deployed however, I select it with Get-Vm to be able to set the other attributes I need. Your assistance has given me some ideas that I will attempt. I really appreciate all your help in this matter. I will assign I unique task to each import. Run the imports at the same time, then perform the other tasks upon completion of the import. I will most likely be checking back in.

Reply
0 Kudos