Skip to content

Instantly share code, notes, and snippets.

Last active June 8, 2018 22:23
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
#Make sure you are connected to vCenter
$vcenter = 'rgavc2'
$serverlist = $global:DefaultVIServer
if($serverlist -eq $null) {
write-host "Connecting to $vcenter"
connect-viserver -Server $vcenter
#User Configurtable Parmaters. Include each VM and it's associated IP in the list below.
$vmlist = @{"" = "temp3"}
$spec = 'rga'
$template = 'server_2016_template'
$datastore = get-datastore -Name data-nmbl
$vmhost = 'rga-esx3.rga.local'
$mask = ''
$dns = '',''
#Create the VM
foreach ($vm in $vmlist.keys) {
$params = @{
IpAddress = $vm
SubnetMask = $mask
DefaultGateway = ''
dns = $dns
IpMode = 'UseStaticIp'
#clone vm spec
Get-OSCustomizationSpec -name $spec | New-OSCustomizationSpec -name temp -type nonpersistent | out-null
#set ip of clone
Get-OSCustomizationSpec -Name temp | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping @params
write-host "Creating $($vmlist.Item($vm)) on $vmhost" -b Yellow -ForegroundColor Black
New-VM -vmhost $vmhost -Name $vmlist.Item($vm) -OSCustomizationSpec temp -Template $template -Datastore $datastore -ErrorAction SilentlyContinue | out-null
while (Get-Task -Status Running | where {$_.Name -eq 'CloneVM_Task'}) {
write-host 'Waiting for clone to finish'
Start-Sleep -Seconds 5 }
write-host "Starting $($vmlist.Item($vm))" -b yellow -ForegroundColor Black
Start-VM $vmlist.item($vm) | out-null
#cleanup spec
Get-OSCustomizationSpec -name temp | Remove-OSCustomizationSpec -Confirm:$false
Disconnect-viServer -server * -force -Confirm:$false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment