VMware Cloud Community
pc-tech
Contributor
Contributor
Jump to solution

New-Folder and Wait-Task

Hello There,

I am new to VI Toolkit (and indeed to Powershell) but I have a portion of a script I am trying to build which I am totally stuck on the syntax for:

The script is as follows - it attempts to create a temporary virtual machine folder, performs a clone of a guest to the new folder (and more importantly, to a new datastore from the one it's on), then delete the guest (without "deletefromdisk" so the new datastore has a backup of the files) and then deletes the temporary virtual machine folder.

$sourceHost = "xxx.xxx.xxx.xxx" # this is a VC, not an ESX server.

$vm2c = "yyyyyyyyyy"

$dataCenter="zzzzzz"

$destcln = $vm2c + "-clone"

$destdatastr = "aaaaaaaaaaaaaaa"

$foldername = "~bbbbbbbb"

$sourceVIServer = Connect-VIServer -Server "$sourceHost"

Get-Folder "vm" | New-Folder -Name $foldername

$FolderID = Get-Folder -Name $foldername

$targetfolder = get-view ($FolderID).ID

$destdsview = get-view (get-datastore -name $destdatastr).id

$VMCloneSpec = New-Object VMware.Vim.VirtualMachineCloneSpec

$VMCloneSpec.Location = New-Object VMware.Vim.VirtualMachineRelocateSpec

$vmclonespec.location.datastore = $destdsview.moref

$VMCloneSpec.powerOn = $false

$vmclonespec.template = $false

$clntask = (Get-View (Get-VM -Name $vm2c).ID).CloneVM_Task($targetfolder.MoRef, $destcln, $VMCloneSpec)

Wait-Task $clntask.value

Remove-VM (Get-VM -Name $destcln)

Remove-Folder -Name $foldername

The first issue I have is with New-Folder. I have worked out to create a folder which can contain guests, you need to do this under the "vm" folder. However in a vCenter, I have three individual datacenters, and it creates the folder under ALL of the datacenters, then the script barfs as the lookup for the folder using get-folder fails (as there are 3). Is there any way I can specify a specific datacenter to add my folder to?

The second issue I have is Wait-Task. The piece of code I'm using is from these forums and it's performing a CloneVM_Task. I would my script to wait for this task to complete (as obviously I want to follow the wait with a remove-VM on the new "cloned" VM). I can't get it to wait using any combination of "wait-task -task $clntask.value", "wait-task -task $clntask", "wait-task $clntask" or "wait-task $clntask.value". Does anyone have the correct syntax for what I am trying to do - is it even possible?

Thanks in advance...

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You can use a Get-Datacenter or Get-Folder (or a sequence of these) to position yourself to the location where you want to create the new folder.

Suppose you have 2 datacenters, DC1 and DC2, with each a folder called Templates.

If you want to create a subfolder in DC2 under the Templates folder you could do it like this

Get-Datacenter DC2 | Get-Folder Templates | New-Folder NewFolder

The Wait-Task cmdlet works with a TaskImpl object.

The CloneVM_Task method returns a Task object.

See also

To wait for the completion of the task you could use a construct like this

$taskMoRef = (Get-View (Get-VM -Name $vm2c).ID).CloneVM_Task($targetfolder.MoRef, $destcln, $VMCloneSpec)

$task = Get-View $taskMoRef 
while ($task.Info.State -eq "running" -or $task.Info.State -eq "queued") {
  sleep 2
  $task = Get-View $taskMoRef
}


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

View solution in original post

Reply
0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

You can use a Get-Datacenter or Get-Folder (or a sequence of these) to position yourself to the location where you want to create the new folder.

Suppose you have 2 datacenters, DC1 and DC2, with each a folder called Templates.

If you want to create a subfolder in DC2 under the Templates folder you could do it like this

Get-Datacenter DC2 | Get-Folder Templates | New-Folder NewFolder

The Wait-Task cmdlet works with a TaskImpl object.

The CloneVM_Task method returns a Task object.

See also

To wait for the completion of the task you could use a construct like this

$taskMoRef = (Get-View (Get-VM -Name $vm2c).ID).CloneVM_Task($targetfolder.MoRef, $destcln, $VMCloneSpec)

$task = Get-View $taskMoRef 
while ($task.Info.State -eq "running" -or $task.Info.State -eq "queued") {
  sleep 2
  $task = Get-View $taskMoRef
}


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

Reply
0 Kudos
halr9000
Commander
Commander
Jump to solution

The Wait-Task cmdlet works with a TaskImpl object.

The CloneVM_Task method returns a Task object.

See also

Before anyone asks--the above could be considered a bug, or at least a relatively important enhancement request. Wait-Task should be flexible enough to accept either as input. I believe I have mentioned this to VMware already, but telling them again won't hurt. Smiley Happy






[PowerShell MVP|https://mvp.support.microsoft.com/profile=5547F213-A069-45F8-B5D1-17E5BD3F362F], VI Toolkit forum moderator

Author of the upcoming book: Managing VMware Infrastructure with PowerShell

Co-Host, PowerScripting Podcast (http://powerscripting.net)

Need general, non-VMware-related PowerShell Help? Try the forums at PowerShellCommunity.org

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
Reply
0 Kudos
admin
Immortal
Immortal
Jump to solution

You can use Get-VIObjectByVIView to switch from a Task to a TaskImpl, then pipe that into Wait-Task.

Reply
0 Kudos