VMware Cloud Community
marco2601
Contributor
Contributor
Jump to solution

Clone a VM with a script

Hello friends

You could help me with example script to clone a VM on a certain date and that it runs automatically on an ESXI host

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The cloning process is not the big deal (see for example Re: Guide for the script need to take clone on every day per schedule time ), but you would first have to decide how you want to do the scheduling.
This has some impact on how the cloning and removal can be done.

When you use the vCenter scheduler, the cloning can be builtin.
when you use the Windows Task scheduler, you can run the PowerCLI script from a Windows box.


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

View solution in original post

12 Replies
LucD
Leadership
Leadership
Jump to solution

Do you have anymore specifics?
Schedule? How, where?
Clone? From an existing VM?

What do you already have?

And where did you get stuck?


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

Reply
0 Kudos
marco2601
Contributor
Contributor
Jump to solution

Well I have a cluster in vCenter and I want to clone an existing Linux VM.

Cloning, for example, a Master VM every 3 days but I do not know how to clone on certain dates and the previous ones are eliminated

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

But where do you intend to run the schedule?

In the vCenter Scheduler or on a Windows box with the Windows Task Scheduler or on a Linux box with a crontab?


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

Reply
0 Kudos
ryanrpatel
Enthusiast
Enthusiast
Jump to solution

Are you looking for something like this?

New-VM -Name $NewVMName -VM $SourceVM -VMHost $TargetESXiHost -Datastore $TargetDatastore -RunAsync

Reply
0 Kudos
marco2601
Contributor
Contributor
Jump to solution

I have the following initial script:

Where you connect to vCenter, clone the machine, disconnect the network adapter from the source VM, and then start the cloned VM.

Now what stuck me is that I'm not sure how to do so that cloning is generated every week but does not eliminate the already cloned machine that is generated as a new clone

##Clonar VM

##Variables

$vcenter_server ="172.16.6.10"

$vcenter_user ="administrator@taurus.local"

$vcenter_pwd ="T4urus.2018.v1Center"

$Hostesxi = "172.16.1.51"

$VMsource = "Ubuntu"

$VMclone = "Ubuntu-clone"

##Connect to vCenter

connect-viserver -server $vcenter_server -User $vcenter_user -Password $vcenter_pwd

##Clone VM, disco virtual tipo thick y carpeta de almacenaiento de la VM

New-VM -VM $VMsource -Name $VMclone -VMHost $Hostesxi -DiskStorageFormat Thick -Location "Laboratorios" -Notes "Clone creado $dateofclone by Marco Lopez"

##Desconect Network Adapter source VM

Get-VM $VMsource | Get-NetworkAdapter | Set-NetworkAdapter -connected:$false -startconnected:$false

##Iniciar VM clonada

GET-VM -Name $VMclone| Start-VM -Confirm:$False

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The cloning process is not the big deal (see for example Re: Guide for the script need to take clone on every day per schedule time ), but you would first have to decide how you want to do the scheduling.
This has some impact on how the cloning and removal can be done.

When you use the vCenter scheduler, the cloning can be builtin.
when you use the Windows Task scheduler, you can run the PowerCLI script from a Windows box.


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

ryanrpatel
Enthusiast
Enthusiast
Jump to solution

If you can confirm the script to work manually, I'd setup a scheduled task to run that file based on your Timeline.

Reply
0 Kudos
marco2601
Contributor
Contributor
Jump to solution

I helped a lot that refedence practically the part of the cloning I have solved.

Now if you can guide me in the following:

The cloning was done from a source VM, what I need to do is how do I do that when the source VM does not have a connection, basically I do not fear ping for example to 172.16.6.90 that the ip in my source VM disconnect the network adapter from that VM and then turn on the last clone of said VM

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you want to delete the VM in $sourceVM, you can just do a Remove-VM.

Get-VM -Name $sourceVM | Remove-VM -DeletePermanently -Confirm:$false

Or do you mean something else?


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

Reply
0 Kudos
marco2601
Contributor
Contributor
Jump to solution

I express myself badly apparently

I understand that it would be another Script, where I do a connection check to a VM if I do not have a connection to that VM that turns on another VM

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'm afraid you lost me.


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

Reply
0 Kudos
AMWells
Contributor
Contributor
Jump to solution

Hola / hi Marco,

1.     Edit your post and remove all the password, IP and domain specific information.

2.     Don't use your domain admin account to log into vcenter set up a different account specifically for vcenter

3.     Don't use IP addresses in the script use DNS names - IP address change DNS name not so must.

4.     Don't store your password in the script. Store it encrypted on the pc you are running the script from.

-----------------

#STORING CREDENTIAL in a SECURE STRING and then using later

read-host -assecurestring | convertfrom-securestring | out-file C:\securestring.txt` ###this has to be run as the user the schedule task will be ran as#####
#This will prompt you for the paasword and store it in securestring.txt

#this is not part of your main script this is done only once to store the password

----------------

Then use this in the script
$vcenter_pwd = cat C:\SecureString.txt | ConvertTo-SecureString

5 to answer this:-

"Now what stuck me is that I'm not sure how to do so that cloning is generated every week but does not eliminate the already cloned machine that is generated as a new clone"

simples Smiley Happy : delete the old clone first before recreating the new clone,

So finally your script looks like this

------------------------------------------------

##Clonar VM

##Variables

$vcenter_server ="I would use dns name"

$vcenter_user ="administrator@domainl"

$vcenter_pwd = cat C:\SecureString.txt | ConvertTo-SecureString

$Hostesxi = "i would use dns name"

$VMsource = "Ubuntu"

$VMclone = "Ubuntu-clone"

##Connect to vCenter

connect-viserver -server $vcenter_server -User $vcenter_user -Password $vcenter_pwd

#Eliminar clonada antiguo

Get-VM -Name $VMclone | Remove-VM -DeletePermanently -Confirm:$false

##Clone VM, disco virtual tipo thick y carpeta de almacenaiento de la VM

New-VM -VM $VMsource -Name $VMclone -VMHost $Hostesxi -DiskStorageFormat Thick -Location "Laboratorios" -Notes "Clone creado $dateofclone by Marco Lopez"

##Desconect Network Adapter source VM

Get-VM $VMsource | Get-NetworkAdapter | Set-NetworkAdapter -connected:$false -startconnected:$false

##Iniciar VM clonada

GET-VM -Name $VMclone| Start-VM -Confirm:$False

----------------------------------------------------------------------

6.      use Windows task scheduler to run this task weekly/monthly to RUNAS account that stored the password in step 4, select "run whether user logged on or not", and check "run with highest privileges"   - it doesn't have to be the same account that is logging onto vcenter. The powercli modules etc will have to be installed on that pc that your are running the schedule on.

I hope this helps

Alex