Skip to content

Instantly share code, notes, and snippets.

@nshores
Last active June 8, 2018 22:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nshores/c1bc15204080e06ffe8bb09a035b61a3 to your computer and use it in GitHub Desktop.
Save nshores/c1bc15204080e06ffe8bb09a035b61a3 to your computer and use it in GitHub Desktop.
create_vm.ps1
#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 = @{"192.168.0.21" = "temp3"}
$spec = 'rga'
$template = 'server_2016_template'
$datastore = get-datastore -Name data-nmbl
$vmhost = 'rga-esx3.rga.local'
$mask = '255.255.255.0'
$dns = '192.168.0.9','192.168.0.12'
#Create the VM
foreach ($vm in $vmlist.keys) {
$params = @{
IpAddress = $vm
SubnetMask = $mask
DefaultGateway = '192.168.0.1'
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