Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

scheduled Tasks / Script is currently running / VMware PowerCLI

$
0
0

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)
  • delete a VM with a given pattern in his Name (realized)
  • create a clone of a VM (realized)
  • move that created clone (from above) and move it to a named Folder (open)

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

Gerold


Viewing all articles
Browse latest Browse all 6937

Trending Articles