VMware Cloud Community
xavy1303
Contributor
Contributor
Jump to solution

Clone single or multiple VMs with template from vSphere Client

Below script is working for me but i need to implement some thing new like:

1. Need to import a csv file where all new ip exist populated by me and all vm would be created with IP names like : Import-csv deploy.csv

2. Need to use template while running this cmdlet: New-VM -Name $VM_Name -VM $clone -VMHost $ESXi -Datastore $ds -Location $Folder -OSCustomizationSpec $customspecification -RunAsync

3. Can we ignore customspecification ? & what is it ?

# Specify vCenter Server, vCenter Server username and vCenter Server user password

$vCenter=" *************"

$vCenterUser="***************"

$vCenterUserPassword="******"

#

# Specify number of VMs you want to create

$vm_count = "1"

#

# Specify the VM you want to clone

$clone = "xxx.xxx.xxx.xxx"

#

# Specify the Customization Specification to use

$customspecification="XXXXXXX "

#

# Specify the datastore or datastore cluster placement

$ds = "XXXXXXX"

#

# Specify vCenter Server Virtual Machine & Templates folder

$Folder = "********************"   # An example

#

# Specify the vSphere Cluster

$Cluster = "XXXXXXX"

#

# Specify the VM name to the left of the - sign

$VM_prefix = "XXXXX-"

#

# End of user input parameters

#_______________________________________________________

#

write-host "Connecting to vCenter Server $vCenter" -foreground green

Connect-viserver $vCenter -user $vCenterUser -password $vCenterUserPassword -WarningAction 0

1..$vm_count | foreach {

$y="{0:D2}" -f $_

$VM_name= $VM_prefix + $y

$ESXi=Get-Cluster $Cluster | Get-VMHost -state connected | Get-Random

write-host "Creation of VM $VM_name initiated" -foreground green

New-VM -Name $VM_Name -VM $clone -VMHost $ESXi -Datastore $ds -Location $Folder -OSCustomizationSpec $customspecification -RunAsync

}

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Without the OSCustomizationSpec all the VMs generated from that Template will have the same properties (IP, hostname...).

Unless you change these via other methods once the VM has been created.


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

View solution in original post

Reply
0 Kudos
13 Replies
LucD
Leadership
Leadership
Jump to solution

The OSCustomizationSpec allows you to customize settings in the guest OS after deploying a VM.
See Customizing Guest Operating Systems.

There are some articles on the subject, including how to set the IP address in the guest OS.

See here and here.


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

Reply
0 Kudos
xavy1303
Contributor
Contributor
Jump to solution

I tried this script: PowerCLI - Clone VMs from a template and assign different Names and Static IPs

but finally i am getting this error:

Can you please help to resolve this error or you can connect me via call or chat.

Get-OSCustomizationSpec : 4/6/2017 12:11:54 PM    Get-OSCustomizationSpec

  Could not find Customization Specification with name 'VM_Custom'.

At C:\Users\Test3.ps1:89 char:12

+             $spec = Get-OSCustomizationSpec $strCustomSpec |

Get-OSCustomizationNicMappin ...

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

    + CategoryInfo          : ObjectNotFound: (VM_Custom:String) [Get-OSCustom

   izationSpec], VimException

    + FullyQualifiedErrorId : Common_CommonUtil_FilterCollection_ObjectNotFoun

   d,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetOSCustomizationSpec

Get-Cluster : 4/6/2017 12:11:54 PM    Get-Cluster        Cluster with name

'<IP>' was not found using the specified filter(s).

At C:\Users\Test3.ps1:92 char:14

+             $vmhost = Get-Cluster $Cluster | Get-VMHost | Get-Random |

Where{$_ -ne $null ...

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

    + CategoryInfo          : ObjectNotFound: (:) [Get-Cluster], VimException

    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimA

   utomation.ViCore.Cmdlets.Commands.GetCluster

I am using PowerShell 3.0 and PowerShell CLI 6.5.0

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You have to create the OSCustomizationSpec first.

That can be done with the New-OSCustomizationSpec and New-OSCustomizationNicMapping cmdlets.

The 2nd link I gave earlier shows how to do this.


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

Reply
0 Kudos
xavy1303
Contributor
Contributor
Jump to solution

Steps

Created a Template called My_Template from Host Machine

Open PowerCLI and fired this cmdlet as usual

While running this cmdlet for creating a New-VM through this CMD:

Facing issue in CLI:

PowerCLI C:\> New-VM -Name Test -Template My_Template -Datastore x -

VMHost <IP>

New-VM : 4/7/2017 3:49:51 PM    New-VM        The operation for the entity

"VM_Template" failed with the following message: "Von Datei /vmfs/volumes/58863

f8a-64767aff-a080-2c768a566bc8/<IP>/<IP>-000008.vmdk

verursachter Fehler."

At line:1 char:1

+ New-VM -Name Test -Template My_Template -Datastore x -VMHost

10.xxx. ...

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

~~~

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

    + FullyQualifiedErrorId : Client20_TaskServiceImpl_CheckServerSideTaskUpda

   tes_OperationFailed,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM

PowerCLI C:\>

Even I am getting an issue, vsphere Client see the screenshot.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Looks like ther emight be something wrong with that template.
Can you clone a VM from that template in the Web Client?


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

Reply
0 Kudos
xavy1303
Contributor
Contributor
Jump to solution

It's working now Smiley Happy but it working without configuring the below command, will there be any issue in future ?

New-OSCustomizationSpec –Name “BasicWindowsSpec” –FullName “administrator” –OrgName “Organization” –Domain “vmware.com” –DomainUsername “user” –DomainPassword “pass” –OSType Windows

Get-OSCustomizationSpec $args[0]

` | Get-OSCustomizationNicMapping

` | Set-OSCustomizationNicMapping

` -IpMode:UseStaticIP

` -IpAddress (Read-Host "IP Address: ")

` -SubnetMask (Read-Host "Subnet Mask: ")

` -Dns (Read-Host "DNS Server IP: ")

` -DefaultGateway (Read-Host "Default Gateway IP: ")

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Without the OSCustomizationSpec all the VMs generated from that Template will have the same properties (IP, hostname...).

Unless you change these via other methods once the VM has been created.


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

Reply
0 Kudos
xavy1303
Contributor
Contributor
Jump to solution

Yes, you are correct my new vm has same IP as Host, but when i am running this command (OSCustomizationSpec) i am getting an error, what could i do ?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Which error are you getting?


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

Reply
0 Kudos
xavy1303
Contributor
Contributor
Jump to solution

I am stuck at here:

PowerCLI C:\> New-OSCustomizationSpec -Name "BasicWindowsSpec" -FullName "admini

strator" -OrgName "vmware.com" -Domain "xxx.com" -DomainUserna

me "*******" -DomainPassword "******" -OSType Windows

New-OSCustomizationSpec : 4/10/2017 12:24:15 PM    New-OSCustomizationSpec

   Permission to perform this operation was denied. Required privilege

'VirtualMachine.Provisioning.ModifyCustSpecs' on managed object with id

'Folder-group-d1'.

At line:1 char:1

+ New-OSCustomizationSpec -Name "BasicWindowsSpec" -FullName "administrator"

-OrgN ...

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

~~~

    + CategoryInfo          : NotSpecified: (:) [New-OSCustomizationSpec], NoP

   ermission

    + FullyQualifiedErrorId : Client20_VmGuestServiceImpl_SavePersistentOSSpec

   _ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewCustomizationSpec

Post that i need to ru this command correct ?

Get-OSCustomizationSpec -name $OSCustSpec | Get-OSCustomizationNICMapping | Set-OSCustomizationNICMapping -IPMode UseStaticIP -IPAddress $VMIP -SubNetMask $SubNetMask -DefaultGateway $DefaultGateway -dns $DNS

Please reply me asap.

Reply
0 Kudos
xavy1303
Contributor
Contributor
Jump to solution

please update ...

Reply
0 Kudos
xavy1303
Contributor
Contributor
Jump to solution

Hi LucD,

Please update your suggestion.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You're obviously missing a Privilege to update the OSCustomizationSpec.

Try perhaps by copying an existing OSCustomizationSpec into as Temporary one (New-OSCustomizationSpec with the Type parameter and the value NonPersistent)

If that also gives a permissions error, you will have to get an account that has a Role assigned with the correct Privileges.


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

Reply
0 Kudos