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
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
Try with
PowerShell -noninteractive -noprofile -executionpolicy bypass -file C:\scripts\scriptname.ps1
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
I run that command:
PowerShell -noprofile -noexit -executionpolicy bypass -file C:\scripts\scriptname.ps1
Try with
PowerShell -noninteractive -noprofile -executionpolicy bypass -file C:\scripts\scriptname.ps1
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
thanks, Luc
your former proposal saved my day 🙂
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
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:)
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