Hi Guys,
I need to deploy about 500 vms on ESX host with different datastores and multiple network adapters like Customer, Admin etc. I have modified the script below as per my requirement, but I am not able to add include multiple network adapters and multiple vlans. Can u please provide your inputs. on this. I also need to check for datastore space availability before deploying to prevent generating alerts.
I got this this script from my training center and one from script center. I have fused 2 to make this script. Please advise.
Connect-VIServer 192.168.1.3
Workflow
{
$VirtualMachinesCSV = "C:\VMsFromTemplate.csv"
$strDescription = "Created from template and CSV by Andy Barnes"
$VirtualMachinesDetails = Import-CSV $VirtualMachinesCSV
$VirtualMachinesDetails | %{ New-VM -Name $_.Name -Template $(Get-Template $_.Template) -VMHost $(Get-VMHost $_.DestinationHost) -OSCustomizationSpec $(Get-OSCustomizationSpec $_.CustomSpec) }
$Datastores = Get-Datastore
$myCol = @()
ForEach ($Datastore in $Datastores)
{
$myObj = "" | Select-Object Datastore, CapacityGB, UsedGB, FreeGB, PercFree
$myObj.Datastore = $Datastore.Name
$myObj.CapacityGB = "{0:N2}" -f ($Datastore.CapacityMB/1KB)
$myObj.UsedGB = "{0:N2}" -f (($Datastore.CapacityMB - $Datastore.FreeSpaceMB)/1kb)
$myObj.FreeGB = "{0:N2}" -f ($Datastore.FreeSpaceMB/1kb)
$myObj.PercFree = "{0:N}" -f (100 * $Datastore.FreeSpaceMB/$Datastore.CapacityMB)
$myCol += $myObj
}
$VirtualMachinesDetails | %{ Set-VM -VM $_.Name -NumCpu $_.NumCpu -MemoryMB $_.MemoryMB -Description $strDescription -Confirm:$false }
$VirtualMachinesDetails | %{ Start-VM -VM $_.Name -Confirm:$false } }
# B - Setup the IP address
Get-OSCustomizationSpec $custspec | Get-OSCustomizationNicMapping |
Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $ipaddr -SubnetMask $subnet -DefaultGateway $gateway -Dns $pdns,$sdns
# C - Copy the Template and apply customizations
$item = New-VM -Template $templatevm -VMHost $vmhost -Name $vmname -Datastore $Datastore -DiskStorageFormat 'Thick' -Description $description -OSCustomizationSpec $custspec -RunAsync
"`n Step 1 is now being run.`n"
Get-Task | ? {$_.id -eq $Item.id} |
Foreach-Object {
$VMtask = @{
State = $_.State
PercentComplete = $_.PercentComplete
StartTime = $_.StartTime
Job = $_.Name
}
New-Object psobject -Property $VMtask
}
}
else
{
"`nVM aleady exists, checking for running tasks . .`n"
$VMID = (Get-Vm -Name $vmname).ID
$Task = @(Get-Task | ? {($_.Name -eq "CloneVM_Task") -and ($_.StartTime -gt $Global:StartTime)})
$Task[$global:i] | Foreach-Object {
$VMtask=@{
State = $_.State
PercentComplete = $_.PercentComplete
StartTime = $_.StartTime
Job = $_.Name
}
New-Object psobject -Property $VMtask
} | Select-Object State, Percentcomplete
if ($Task[$global:i].finishtime -le (get-date))
{
$Global:i++
if ($VMCSV_Builds.count -eq $i)
{
"Task is complete, go onto the next step (2).`n"
$Global:Step++
"Moving onto step $Step`n"
$Global:i = 0
}
}
elseif (!$Task[$i].finishtime)
{
Write-Host "Task is $($Task[$i].PercentComplete) % complete."
$i++
}
}
}
if ($Global:Step -eq "2")
{
# Step 2
# A - Get the VM and setup the VLAN - Set the Network Name (I often match PortGroup names with the VLAN name)
$Global:VMStatus = GET-VM $vmname | Get-NetworkAdapter
if (!$Global:VMStatus)
{
"Please wait for ALL VM's to build before running this step . ."
$Global:i--
}
if (!(($Global:VMStatus.NetworkName -eq $vlan) -and (($Global:VMStatus.ConnectionState).StartConnected)))
{
Get-VM -Name $vmname | Get-NetworkAdapter | Set-NetworkAdapter -StartConnected $true -NetworkName $vlan -Confirm:$false
}
else
{
$Global:i++
if ($VMCSV_Builds.count -eq $i)
{
"`nThe network is set correctly, go onto the next step (3).`n"
$Global:Step++
"Moving onto step $Step`n"
$Global:i = 0
}
}
}
if ($Global:Step -eq "3")
{
# Step 3
# A - Start the VM
$VMStatus = GET-VM $vmname
if ($VMStatus.PowerState -ne "PoweredOn")
{
GET-VM $vmname | Start-vm | select PowerState, Version, Description, Guest
$Global:i++
}
elseif ($VMCSV_Builds.count -eq $Global:i)
{
"`n$vmname is $($VMStatus.PowerState), you can move onto the next step (4)."
$Global:Step++
"Moving onto step $Step`n"
$Global:i = 0
}
else
{
$Global:i++
}
}
# Wait around 20 minutes before running this
if ($Global:Step -eq "4")
{
# Step 4
# A - Check the VM is part of the domain and then move it to the correct OU
"`n Step 4 is now being run, This will test of the Server has been added to the domain correctly`n"
if (Get-QADComputer $vmname )
{
"`nYou can now log onto the System it is part of the domain`n"
Get-QADComputer -Name $vmname | select ParentContainer, DnsName
"Moving the Server to the correct OU"
Get-QADComputer -Name $vmname | Move-QADObject -NewParentContainer 'OU=Servers,OU=Hospital,DC=nyumc,DC=org' | select ParentContainer, DnsName
$Global:i++
if ($VMCSV_Builds.count -eq $i)
{
$Global:Step++
"Moving onto step $Step`n"
$Global:i = 0
}
}
else
{
"`nVM is still building and is not yet part of the domain, try again soon`n"
}
}
# Add the build details to the VM
if ($Global:Step -eq "5")
{
if (-not (Get-CustomAttribute -Name CreatedBy))
{
New-CustomAttribute -Name "CreatedBy" -TargetType VirtualMachine
}
if (-not (Get-CustomAttribute -Name CreatedOn))
{
New-CustomAttribute -Name "CreatedOn" -TargetType VirtualMachine
}
if (! (Get-CustomAttribute -Name Owner))
{
New-CustomAttribute -Name "Owner" -TargetType VirtualMachine
}
if (! (Get-CustomAttribute -Name AppContact))
{
New-CustomAttribute -Name "AppContact" -TargetType VirtualMachine
}
$VM = Get-vm $vmname
If (-NOT ($VM).CustomFields['CreatedBy'])
{
Write-Host "Looking for creator of $($vmname.name)"
Try {
$event = $VM | Get-VIEvent -MaxSamples 500000 -Types Info | Where {
$_.GetType().Name -eq "VmBeingDeployedEvent" -OR $_.Gettype().Name -eq "VmCreatedEvent" -or $_.Gettype().Name -eq "VmRegisteredEvent"`
-or $_.Gettype().Name -eq "VmClonedEvent"
}#End Where
If (($event | Measure-Object).Count -eq 0) {
$username = "Unknown"
$created = "Unknown"
}#End If
Else {
If ([system.string]::IsNullOrEmpty($event.username)) {
$username = "Unknown"
}#End If
Else {
$username = $event.username
}#End Else
$created = $event.CreatedTime
}#End Else
}
Catch {
Write-Warning "$($Error[0])"
Return
}
}#End If
Write-Host "Updating $($vm.name) attributes"
if($username)
{
$vm | Set-Annotation -CustomAttribute "CreatedBy" -Value $username -ErrorAction 0 | Out-Null
}
if($created)
{
$vm | Set-Annotation -CustomAttribute "CreatedOn" -Value $created -ErrorAction 0 | Out-Null
}
if($description)
{
$vm | Set-Annotation -CustomAttribute "Owner" -Value $description -ErrorAction 0 | Out-Null
}
if($appowner)
{
$vm | Set-Annotation -CustomAttribute "AppContact" -Value $appowner -ErrorAction 0 | Out-Null
}
$Global:i++
if ($VMCSV_Builds.count -eq $i)
{
$Global:Step++
"Moving onto step $Step`n"
$Global:i = 0
}
}
Agroman09