VMware Cloud Community
GregVV
Contributor
Contributor

How to script deploying from template or making Clones

I have the VM named Server001. I would like to deploy (or clone) 500 VM's.

I have csv file including:

- VM Name

- Hostname - the same as VM Name;

- IP adddress, mask, gateway;

- IP of DNS server;

- Password for local administrator;

What is the best way to deploy (or create the clones or the linked clones)?

Thanks in advance,

Greg

10 Replies
LucD
Leadership
Leadership

Have a look at  3.  Re: How to Script making Linked Clones?

You can use that script and put it inside a ForEach loop that is fed by an Import-Csv cmdlet.

Something along these lines

Import-Csv <your-CSV> -UseCulture | %{

   # clone a VM

}

Does that help you on your way ?


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

0 Kudos
GregVV
Contributor
Contributor

Could you please wite down the whole script with those modification?

Thank you.

0 Kudos
LucD
Leadership
Leadership

Sure, try something like this

$masterName = "Server001"
$snapName = "Master"
$esxName = "MyEsx"
$oscustName = "Your OSCustomizationSpec"

# Create a non-persistent copy
$oscust = Get-OSCustomizationSpec -Name $oscustName | New-OSCustomizationSpec -Type NonPersistent
$vm = Get-VM -Name $masterName

# Get the snapshot
if($vm.PowerState -eq "PoweredON"){
 
"Master VM $vmName needs to be powered off"
 
return
}
Try {
 
$snap = Get-Snapshot -Name $snapName -VM $vm -ErrorAction Stop
}
Catch {
 
$snap = New-Snapshot -Name $snapName -VM $vm
}

# Create the linked clones
Import-Csv C:\lclones.csv -UseCulture | %{
 
# Configure NIC settings
  $nicParams = @{
   
IPMode = "Static"
   
IPAddress = $_.IP
   
SubnetMask = $_.Mask
   
DefaultGateway= $_.Gateway
   
Dns = @($_.Dns)
  }
 
Get-OSCustomizationNicMapping -OSCustomizationSpec $oscust |
 
Set-OSCustomizationNicMapping $nicParams

 
# Set admin password
  $osParams = @{
   
AdminPassword = $_.Password
   
NamingScheme = "Fixed"
   
NamingPrefix = $_.Name
  }
 
Get-OSCustomizationNicMapping -OSCustomizationSpec $oscust |
 
Set-OSCustomizationSpec @osParams

 
# Create linked clone
  $cloneParams = @{
   
Name = $_.Name
   
VM = $vm
   
ReferenceSnapshot = $snap
   
LinkedClone = $true
   
VMHost = $esxName
   
OSCustomizationSpec = $osSpec
  }
 
New-VM @cloneParams
}

Your CSV file should look like this

Name,IP,Mask,Gateway,Dns,Password

VM1,192.168.1.1,255.255.255.0,193.168.1.254,DNS1,MyPswd


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

GregVV
Contributor
Contributor

Can I use Cluster instead of ESXi here $esxName = "MyEsx"?

Can i specify datastore where to place VMs?

I run script, VMs created but parameters not set.

I have following errors:

The term 'Static' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

At C:\scripts\ed.ps1:26 char:20

+     IPMode = Static <<<<

    + CategoryInfo          : ObjectNotFound: (Static:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

Get-OSCustomizationNicMapping : 8/6/2013 2:55:39 AM    Get-OSCustomizationNicMapping        Value cannot be found for the mandatory parameter OSCustomizationSpec

At C:\scripts\ed.ps1:32 char:32

+   Get-OSCustomizationNicMapping <<<<  -OSCustomizationSpec $oscust |

    + CategoryInfo          : NotSpecified: (:) [Get-OSCustomizationNicMapping

   ], VimException

    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetOSCustomizationNicMapping

The term 'Fixed' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

At C:\scripts\ed.ps1:38 char:25

+     NamingScheme = Fixed <<<<

    + CategoryInfo          : ObjectNotFound: (Fixed:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

Set-OSCustomizationSpec : Cannot validate argument on parameter 'OSCustomizationSpec'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.

At C:\scripts\ed.ps1:42 char:26

+   Set-OSCustomizationSpec <<<<  @osParams

    + CategoryInfo          : InvalidData: (:) [Set-OSCustomizationSpec], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetCustomizationSpec

0 Kudos
LucD
Leadership
Leadership

No, you can't use a cluster afaik, but you can pick a random node from a cluster.

Datastore can be specified. In the below script I assume it is picked up from the CSV file.

The Fixed and Static values had to be between quotes.

Do you have a OSCustomizationSPec defined ? Change the name to reflect your spec !

Try again with this updated version

$masterName = "Server001"
$snapName = "Master"
$clusterName = "MyCluster"
$esx = Get-Cluster -Name $clusterName | Get-VMHost | Get-Random
$oscustName = "Your OSCustomizationSpec"

# Create a non-persistent copy
$oscust = Get-OSCustomizationSpec -Name $oscustName | New-OSCustomizationSpec -Type NonPersistent
$vm = Get-VM -Name $masterName

# Get the snapshot
if($vm.PowerState -eq "PoweredON"){
 
"Master VM $vmName needs to be powered off"
 
return
}
Try {
 
$snap = Get-Snapshot -Name $snapName -VM $vm -ErrorAction Stop
}
Catch {
 
$snap = New-Snapshot -Name $snapName -VM $vm
}

# Create the linked clones
Import-Csv C:\lclones.csv -UseCulture | %{
 
# Configure NIC settings
  $nicParams = @{
   
IPMode = "Static"
   
IPAddress = $_.IP
   
SubnetMask = $_.Mask
   
DefaultGateway= $_.Gateway
   
Dns = @($_.Dns)
  }
 
Get-OSCustomizationNicMapping -OSCustomizationSpec $oscust |
 
Set-OSCustomizationNicMapping $nicParams

 
# Set admin password
  $osParams = @{
   
AdminPassword = $_.Password
   
NamingScheme = "Fixed"
   
NamingPrefix = $_.Name
  }
 
Get-OSCustomizationNicMapping -OSCustomizationSpec $oscust |
 
Set-OSCustomizationSpec @osParams

 
# Create linked clone
  $cloneParams = @{
   
Name = $_.Name
   
VM = $vm
   
ReferenceSnapshot = $snap
   
LinkedClone = $true
   
VMHost = $esx
   
Datastore = $_.Datastore
   
OSCustomizationSpec = $osSpec
  }

 
New-VM @cloneParams
}


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

GregVV
Contributor
Contributor

I run script, VMs created but parameters not set.

I have following errors:

New-OSCustomizationSpec : 8/6/2013 5:32:58 AM    New-OSCustomizationSpec

There is already a customization spec with the same name.

At C:\scripts\8.ps1:8 char:78

+ $oscust = Get-OSCustomizationSpec -Name $oscustName | New-OSCustomizationSpec

<<<<  -Type NonPersistent

    + CategoryInfo          : InvalidArgument: (:) [New-OSCustomizationSpec],

   InvalidArgument

    + FullyQualifiedErrorId : Client20_VmHostServiceImpl_NewCustomizationSpec_

   DuplicateCustomizationSpecClient,VMware.VimAutomation.ViCore.Cmdlets.Comma

  nds.NewCustomizationSpec

New-OSCustomizationSpec : 8/6/2013 5:32:58 AM    New-OSCustomizationSpec

There is already a customization spec with the same name.

At C:\scripts\8.ps1:8 char:78

+ $oscust = Get-OSCustomizationSpec -Name $oscustName | New-OSCustomizationSpec

<<<<  -Type NonPersistent

    + CategoryInfo          : InvalidArgument: (:) [New-OSCustomizationSpec],

   InvalidArgument

    + FullyQualifiedErrorId : Client20_VmHostServiceImpl_NewCustomizationSpec_

   DuplicateCustomizationSpecClient,VMware.VimAutomation.ViCore.Cmdlets.Comma

  nds.NewCustomizationSpec

Get-OSCustomizationNicMapping : 8/6/2013 5:32:59 AM    Get-OSCustomizationNicMa

pping        Value cannot be found for the mandatory parameter OSCustomizationS

pec

At C:\scripts\8.ps1:33 char:32

+   Get-OSCustomizationNicMapping <<<<  -OSCustomizationSpec $oscust |

    + CategoryInfo          : NotSpecified: (:) [Get-OSCustomizationNicMapping

   ], VimException

    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomatio

   n.ViCore.Cmdlets.Commands.GetOSCustomizationNicMapping

Get-OSCustomizationNicMapping : 8/6/2013 5:32:59 AM    Get-OSCustomizationNicMa

pping        Value cannot be found for the mandatory parameter OSCustomizationS

pec

At C:\scripts\8.ps1:42 char:32

+   Get-OSCustomizationNicMapping <<<<  -OSCustomizationSpec $oscust |

    + CategoryInfo          : NotSpecified: (:) [Get-OSCustomizationNicMapping

   ], VimException

    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomatio

   n.ViCore.Cmdlets.Commands.GetOSCustomizationNicMapping

0 Kudos
LucD
Leadership
Leadership

The OSCustomizationSpec, the script creates is nonpersistent, which means it will be kept as long as your PowerCLI session runs.

You can explicitely remove it with

Remove-OSCustomizationSpec -OSCustomizationSpec $oscust -Confirm:$false

Or add that line to the end of the script.

Another option is to stop/start your PowerCLI session


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

0 Kudos
GregVV
Contributor
Contributor

This script works without errors, but all parameters (IPs, DNS, passwords) remain untouched Smiley Sad

$masterName = "Server"

$snapName = "Master"

$dsName = "Stor01"

$rpName = "DPool"

$oscustName = "Easy"

# Create a non-persistent copy

$oscust = Get-OSCustomizationSpec -Name $oscustName

$vm = Get-VM -Name $masterName

# Get the snapshot

if($vm.PowerState -eq "PoweredON"){

  "Master VM $vmName needs to be powered off"

  return

}

Try {

  $snap = Get-Snapshot -Name $snapName -VM $vm -ErrorAction Stop

}

Catch {

  $snap = New-Snapshot -Name $snapName -VM $vm

}

# Create the linked clones

Import-Csv C:\scripts\ed.csv -UseCulture | %{

  # Configure NIC settings

  $nicParams = @{

    IpMode = "UseStaticIP"

    IPAddress = $_.IP

    SubnetMask = $_.Mask

    DefaultGateway= $_.Gateway

    Dns = $_.Dns

  }

  Get-OSCustomizationNicMapping -OSCustomizationSpec $oscust |

  Set-OSCustomizationNicMapping @nicParams

 

  # Set admin password

  $osParams = @{

    AdminPassword = $_.Password

    NamingScheme = "Fixed"

    NamingPrefix = $_.Name

  }

  Get-OSCustomizationSpec $oscust |

  Set-OSCustomizationSpec @osParams

  # Create linked clone

  $cloneParams = @{

    Name = $_.Name

    VM = $vm

    ReferenceSnapshot = $snap

    LinkedClone = $true

    ResourcePool = $rpName

    Datastore = $dsName

    OSCustomizationSpec = $osSpec

  }

  New-VM @cloneParams

  Start-VM $_.Name

}

0 Kudos
LucD
Leadership
Leadership

How many NICs do you have in the "master" VM ?


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

0 Kudos
GregVV
Contributor
Contributor

Only one

0 Kudos