VMware Cloud Community
GeroldKa
Contributor
Contributor
Jump to solution

Clone a VM / move to Folder / delete VM / as Powershell Script as scheduled Task ends with "currently running"

Hi all together,

I would kindly request your help with a script which should run as a scheduled Task. It is functional but terminates not proper and need some love at the end ...

I want to create a script which

  • find a VM with a pattern in his Name (realized and works)
  • delete a VM with a given pattern in his Name (realized and works)
  • create a clone of a VM (realized and works)
  • move that created clone (from above) and move it to a named Folder (open)
    or does this in the clone process

I realised in that way so far:

add-pssnapin VMware.VimAutomation.Core
# Import Backup CSV
$backupinfo = Import-Csv C:\scripts\vm_backup\test.csv
#Set VCenter servername
$vcenter_server = "Name-des-Servers"
#Connect to vCenter
Connect-VIServer $vcenter_Server

# BEGIN OLD BACKUP CLEANUP
#Select all old backups
$old_backups = Get-VM *-backups
if($old_backups) {
foreach($backup_vm in $old_backups) {
Get-VM $backup_vm | Remove-VM -DeleteFromDisk -Confirm:$false
}
}

# BEGIN QUEUING NEW CLONES
#Increment through CSV
foreach ($customer in $backupinfo) {
$target_host = Get-VMHost -Name $customer.TargetHost
If ($target_host) {
#Set Date format for clone names
$date = Get-Date -Format "yyyy-MM-dd"
#Set Date format for emails
$time = (Get-Date -f "HH:MM")
#Get SourceVM
$vm = Get-VM $customer.SourceVM
# Create new snapshot for clone
$cloneSnap = $vm | New-Snapshot -Name "Clone Snapshot"
# Get managed object view
$vmView = $vm | Get-View
# Get folder managed object reference
$cloneFolder = $vmView.parent
# Build clone specification
$cloneSpec = new-object Vmware.Vim.VirtualMachineCloneSpec
# Make linked disk specification?
$cloneSpec.Snapshot = $vmView.Snapshot.CurrentSnapshot
#Set VirtualMachineRelocateSpec
$cloneSpec.Location = new-object Vmware.Vim.VirtualMachineRelocateSpec
#Thin provisioning
$cloneSpec.Location.Transform = [Vmware.Vim.VirtualMachineRelocateTransformation]::sparse
#Target Datastore
$cloneSpec.Location.Datastore = (Get-Datastore -Name $customer.TargetDS | Get-View).MoRef
#Target Host
$cloneSpec.Location.Host = (Get-VMHost -Name $customer.TargetHost | Get-View).MoRef
#Target Resource Pool, based on first VM in TargetHost
$cloneSpec.Location.Pool = (Get-VMHost -Name $customer.TargetHost | Get-VM | Select-Object -First 1 | Get-View).ResourcePool
#Set Clone Name
$cloneName = "$vm-$date-$time-backup"
# Create clone
$clone_task = $vmView.CloneVM_Task( $cloneFolder, $cloneName, $cloneSpec )
# Remove Snapshot created for clone, will queue automatically
Get-Snapshot -VM (Get-VM -Name $customer.SourceVM) -Name $cloneSnap | Remove-Snapshot -confirm:$False
}
}

# Move VM in Clone Folder
move-VM -VM *-backups -Destination Clone

#Disconnect from vCentre
Disconnect-VIServer -Confirm:$false

The script itself works so far and makes what it should, but the scheduled Task is still "currently running". I now created a Workaround in that manner, that I end the Task after two hours of running; but there must be a better solution which works proper.
As well as the part with the moveing the VM.

Could YOU maybe help me or have a good idea how to solve that issue?

Thanks in advance

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try with

PowerShell -noninteractive -noprofile -executionpolicy bypass -file C:\scripts\scriptname.ps1


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

View solution in original post

Reply
0 Kudos
7 Replies
LucD
Leadership
Leadership
Jump to solution

I assume you run powershell.exe in the scheduled task definition ?

Did you try adding the -NonInteractive parameter on the powershell.exe ?


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

Reply
0 Kudos
GeroldKa
Contributor
Contributor
Jump to solution

I run that command:

PowerShell -noprofile -noexit -executionpolicy bypass -file C:\scripts\scriptname.ps1

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try with

PowerShell -noninteractive -noprofile -executionpolicy bypass -file C:\scripts\scriptname.ps1


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

Reply
0 Kudos
GeroldKa
Contributor
Contributor
Jump to solution

thanks, Luc

your former proposal saved my day 🙂

AustinArnold
Contributor
Contributor
Jump to solution

Hi

I just wanted to say thanks, as I have been struggling with a similar issue with a PS script not terminating the powershell exe after completing - the "-noninteractive" fixed it for me too.

Cheers

Reply
0 Kudos
GuruH
Contributor
Contributor
Jump to solution

Hi LucD, Could you please help me to create a clone of a Vm. Im planning to include below steps in the script. -> power off VM -> Clone the VM -> it should save in the specified datastore -> power on Vm -> delete the old clone. Could you please give me an idea or script to resolve this? Thank you in advance:)

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I don't know if there are any other special requirements, but the process could look like this (simplified)

1) Stop the VM with Shutdown-VMGuest (requires the VMware Tools to be installed, otherwise use Stop-VM).

2) Clone the VM with the New-VM cmdlet. Use the VM parameter to point to the source VM, and use the Datastore parameter to point to the destination datastore

3) Power on with Start-VM

4) Remove the old VM with Remove-VM


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

Reply
0 Kudos