VMware Cloud Community
elihuj
Enthusiast
Enthusiast
Jump to solution

Clone VM Script

I'm looking to create a PowerCLI script to clone a VM, and overwrite any existing clones that are present. Just doing some searching, here is what I found:

New-VM -Name VM2 -VM VM1 -Datastore datastorename -vmhost hostname

I have not tested the script yet, but it does look like it will do at least part of what I am looking to accomplish. Do I need to use the Datastore and VMhost parameters? And is there anything I can do to delete any existing clones before the script is run? Thank you.

40 Replies
MicroMarket
Contributor
Contributor
Jump to solution

Dear LucD

1* - when im run this command bellow im have an error when type the datastore name

New-VM -VM RJ7469SI010 -Datastore ST310_METALUN_04 -Name RJ7469SI010_new -VMHost rj7469sh023.riodejaneiro.caixa

New-VM : 05/05/2015 11:52:31New-VM    The specified parameter

'Datastore' expects a single value, but your name criteria 'ST310_METALUN_04'

corresponds to multiple values.

No linha:1 caractere:1

+ New-VM -VM RJ7469SI010 -Datastore ST310_METALUN_04 -Name RJ7469SI010_new

-VMHost ...

But the name of Datastore is really  ST310_METALUN_04

when im insert the command Get-Datastore the Datastore ST310_METALUN_04 appears many times

How im can specify the datacenter ???

2* - when im run this command whitou VcUser and VcPass im connect with sucess


Connect-VIServer
-Server $vc


im need these parameters ???

$vcUser = "USER" # Username for VCENTER

$vcPass = "PASSWORD"# Password for VCENTER

Thanks

Almir

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

It looks as if you might more datastores that start with ST310_METALUN_04.

Do a Get-Datastore -Name ST310_METALUN_04

Does that return more than 1 object ?


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

Reply
0 Kudos
MicroMarket
Contributor
Contributor
Jump to solution

LucD escreveu:

It looks as if you might more datastores that start with ST310_METALUN_04.

Do a Get-Datastore -Name ST310_METALUN_04

Does that return more than 1 object ?

Dear LucD

See bellow. the command show two objects with same name

but the datastore are in two diferent hosts

PS C:\Users\p614580> Get-Datastore -Name ST310_METALUN_04

Name                           FreeSpaceGB  CapacityGB
----                          ----------- ----------
ST310_METALUN_04                   34,102  1.023,750
ST310_METALUN_04                   34,102  1.023,750

how im cna clone to another datacenter ?

Thanks

Almir

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Are these 2 different datastores ?

Could it be that these are local datastores on different ESXi nodes ?

Or is this there a ghost entry for that datastore ?


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

Reply
0 Kudos
MicroMarket
Contributor
Contributor
Jump to solution

LucD escreveu:

Are these 2 different datastores ?

Could it be that these are local datastores on different ESXi nodes ?

Or is this there a ghost entry for that datastore ?


Dear LucD

Yes these Datastore appear 2 times with same name! But The datastore is the Same
Its not local !!! the Datastore is a SAN Datastore that are attached in two different ESX Host´s

it appears twice because it is in two different ESX Hosts

how im can clone across datacenter´s with this problem?


obs :
im have this ideia bellow . im set the host to show the datastore onetime
$mydatastore = getdatastore VMHost=rj7469sh023 -Name ST310_METALUN_04

New-VM -Name ST310_New -VM ST310 -Datastore $mydatastore -VMHost rj7469sh023


but continue to show an error

PS C:\Users\p614580> New-vm -Name SI010_New -VM RJ7469SI010 -VMHost rj7469sh023.riodejaneiro.caixa -Datastore $mydatastore

New-vm : 05/05/2015 18:03:18    New-VM        the operation is invalid and actual state of object No linha:1 caractere:1

+ New-vm -Name SI010_New -VM RJ7469SI010 -VMHost

rj7469sh023.riodejaneiro.caixa -D ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~

    + CategoryInfo          : NotSpecified: (:) [New-VM], VimException

    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomatio

   n.ViCore.Cmdlets.Commands.NewVM

Thanks

Almir

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The Get-Datastore cmdlet doesn't have a VMHost parameter, you have to use the Location parameter.

Something like this

$esx = Get-VMHost -Name rj7469sh023

$mydatastore = Get-Datastore -Location $esx -Name ST310_METALUN_04

New-VM -Name ST310_New -VM ST310 -Datastore $mydatastore -VMHost $esx


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

Reply
0 Kudos
MicroMarket
Contributor
Contributor
Jump to solution

LucD escreveu:

The Get-Datastore cmdlet doesn't have a VMHost parameter, you have to use the Location parameter.

Something like this

$esx = Get-VMHost -Name rj7469sh023

$mydatastore = Get-Datastore -Location $esx -Name ST310_METALUN_04

New-VM -Name ST310_New -VM ST310 -Datastore $mydatastore -VMHost $esx

Dear LucD

Im found the problem !!  The problem is the Snapin !!!!
See the complete script , im remove the line

# add snapin for VMware

add-pssnapin VMware.VimAutomation.Core

After this im use the command bellow (but im use vmhost, the location does ot work. Location is for folders. im not use folders)


$Vc
= "rj7469sr002.corp.caixa.gov.br" #Name of Vcenter Server
Connect-VIServer -Server $vc
$esx = Get-VMHost -Name rj7469sh023.riodejaneiro.caixa
$mydatastore = Get-Datastore -VMHost $esx -Name ST310_METALUN_04

New-vm -Name SX002_New -VM RJ7469SX002_CESTRUX -Datastore $mydatastore -VMHost $esx

See the Result
vmware_clone.jpg

Thanks for All

Almir

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, great that you have it working.

Out of curiosity, which PowerCLI version are you using, can you do a

Get-PowerCLIVersion


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

Reply
0 Kudos
MicroMarket
Contributor
Contributor
Jump to solution

Dear LucD

See :


PowerCLI Version

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

   VMware vSphere PowerCLI 6.0 Release 1 build 2548067

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

Component Versions

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

   VMWare AutoDeploy PowerCLI Component 6.0 build 2358282

   VMWare ImageBuilder PowerCLI Component 6.0 build 2358282

   VMware License PowerCLI Component 6.0 build 2315846

   VMware vSphere PowerCLI Component 6.0 build 2548068

   VMware Cloud Infrastructure Suite PowerCLI Component 6.0 build 2548068

   VMware VDS PowerCLI Component 6.0 build 2548068

   VMware vCloud Director PowerCLI Component 6.0 build 2512755

   VMware HA PowerCLI Component 6.0 build 2510422

   VMware vCloud Air PowerCLI Component 6.0 build 2512755

   VMware PowerCLI Component for Storage Management 6.0 build 2522368

Thanks

Almir

Reply
0 Kudos
MicroMarket
Contributor
Contributor
Jump to solution

Dear LucD


you know how im can verify if datastore have space before clone to datastore?


Thanks

Almir

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Check the value in the FreeSpaceMB or FreeSpaceGB property of the object returned by Get-Datastore.


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

Reply
0 Kudos
vmk2014
Expert
Expert
Jump to solution

Dear LucD,

I want to clone the VM through schedule tasks.

My requirement is

Automation through Schedule tasks :- The Script will run every day at schedule time and it will overwrite the old clone1 and create new clone2 in the data store. ( For only single vm Clone)

Please let me know how it will be done.

Thanks

Vmk

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Have a look at Alan's Running a PowerCLI Scheduled task


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

Reply
0 Kudos
vmk2014
Expert
Expert
Jump to solution

LucD,

Just wanted to confirm whether the below script is fine ?

$tgtEsxName = "mcpsx0040.inlm.local.inlp.local" #ESXI Host you want to clone to

$tgtDatastoreName = "VNX1_P0_T1_122" #Datadstore you want to clone to

$Vc = "10.100.125.109" #Name of Vcenter Server

$vcUser = "xxxx_kxxxx@inlv" # Username for VCENTER

$vcPass = "XXXX"# Password for VCENTER

# add snapin for VMware

add-pssnapin VMware.VimAutomation.Core

Connect-VIServer -Server $vc -User $vcUser -Password $vcPass # log into server

$newVMName = "mcp001_clone"

$sourceVMName = "mcp001"

$esxName = "mcpsx0040.inlm.local"

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

New-VM -Name $newVMName -VM $sourceVMName -VMHost $esxName

Thanks

vmk

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Looks ok, without actually running it.

I would not store credentials in clear text in the script.

Have a look at what is possible with the Credential Store items!


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

Reply
0 Kudos
vmk2014
Expert
Expert
Jump to solution

Thanks LucD.

But i don't see the path for credential storeItem

New-VICredentialStoreItem -Host 192.168.1.10 -User Admin -Password pass

Can you please guide me ? How it will be.

thanks

vmk

Reply
0 Kudos
vmk2014
Expert
Expert
Jump to solution

Hi LucD,

For Windows 2012 R2, Script doesn't work with below argument.

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe “& ‘C:\Temp\clone.ps1′”


thanks

vmk

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Have a look at Have you seen PowerCLI’s “Credential Store” feature? for an example of the Credential Store.

What exactly doesn't work?

Any error messages?


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

Reply
0 Kudos
vmk2014
Expert
Expert
Jump to solution

Thanks LucD for prompt. Now,I  ran the script through Batch file and its working.

Thanks

vmk

Reply
0 Kudos
butlpau02
Contributor
Contributor
Jump to solution

Hi Guys,

Anyway to clone a VM across linked mode vCenters via power cli?

Tried using the -Server switch but can't use it twice seemingly in the same command.

Cheers,

Paul.

Reply
0 Kudos