Guest User

Untitled

a guest
Aug 27th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. #****************************************************
  2. #Script to Deploy Multiple VMs from Template
  3. #v1.
  4. #Downloaded from: Jason Langone
  5. #Many thanks to: Blog.Halfbyte.com and NTPRO.NL
  6. #*****************************************************
  7.  
  8. $vms = Import-Csv C:\cloneme.csv
  9. $password = Read-Host -AsSecureString -Prompt "Please supply your vCenter password"
  10. $tempCredential = New-Object System.Management.Automation.PsCredential "None",$password
  11. $password = $tempCredential.GetNetworkCredential().Password
  12. $username = $Env:USERNAME
  13. $mydomain = "exacttarget.com"
  14.  
  15. $me = $Env:USERDOMAIN + "\" + $Env:USERNAME
  16. $email = $Env:USERNAME + "@" + $mydomain
  17. $datetime = Get-Date
  18. $mmddyyyy = $datetime.ToShortDateString()
  19.  
  20. # Set this if you're going to lunch:
  21. $confirm = $false
  22. #$confirm = $true
  23.  
  24. foreach ($vm in $vms) {
  25.  
  26. $server = Connect-VIServer -Server $vm.vCenter -Protocol https -User $username -Password $password
  27. $seconddisk = $vm.Disksize + "GB"
  28. $totalmb = ($seconddisk / 1MB + 12GB / 1MB)
  29. $secondkb = ($seconddisk / 1KB)
  30.  
  31. $cluster = Get-Cluster -Name $vm.Cluster
  32. $datacenter = Get-Datacenter -Cluster $cluster
  33. $network = Get-VirtualPortGroup -Name $vm.Network
  34.  
  35. # Assign new VMs randomly to hosts in the cluster.
  36. $hosts = Get-VMHost -Location $cluster
  37. $hrand = Get-Random -Maximum $hosts.Count -Minimum 1
  38.  
  39. # Assign new VMs randomly to datacenters available to the cluster, where there is enough space to handle the whole VM.
  40. $datastores = Get-Datastore -VMHost $hosts[$hrand] | Where-Object {$_.FreeSpaceMB -gt $totalmb} | sort -Descending -Property FreeSpaceMB
  41. $drand = Get-Random -Maximum $datastores.Count -Minimum 1
  42.  
  43. # Create the VM from template.
  44. Write-Host $vm.Name will be cloned from $vm.Template on $hosts[$hrand], datastore $datastores[$drand] in network $vm.Network
  45. if ($template = Get-Template -Location $datacenter -Name $vm.Template) {
  46. $newvm = New-VM -Name $vm.Name -Template $template -DiskStorageFormat Thin -Host $hosts[$hrand] -Datastore $datastores[$drand] -Confirm:$confirm
  47.  
  48. # Move the VM to its network.
  49. if ($adapter = Get-NetworkAdapter -VM $vm.Name | where { $_.Name -eq "Network Adapter 1" }) {
  50. Set-NetworkAdapter -NetworkAdapter $adapter -NetworkName $vm.Network -StartConnected $true -Confirm:$confirm
  51. } else {
  52. $adapter = New-NetworkAdapter -VM $vm.Name -NetworkName $vm.Network -Type Vmxnet3 -StartConnected:$true -Confirm:$confirm
  53. }
  54.  
  55. # Resize the second hard disk to the specified size (40GB is default)
  56. if ($disk = Get-HardDisk -VM $vm.Name | where { $_.Name -eq "Hard disk 2" }) {
  57. if ($disk.CapacityKB -lt $secondkb) {
  58. Set-HardDisk -HardDisk $disk -Capacity $secondkb -Confirm:$confirm
  59. } else {
  60. Remove-HardDisk -HardDisk $disk -DeletePermanently:$true -Confirm:$confirm
  61. New-HardDisk -VM $vm.Name -CapacityKB $secondkb -DiskType Flat -StorageFormat Thin -Confirm:$confirm
  62. }
  63. }
  64.  
  65. # Move the VM to the right folder.
  66. $vfolder = Get-Folder -Location $datacenter -NoRecursion | Where-Object { $_.Name -eq "vm" }
  67. if ($folder = Get-Folder -Location $vfolder | Where-Object { $_.Name -eq $vm.Folder }) {
  68. Move-VM -Destination $folder -VM $vm.Name -Confirm:$confirm
  69. } else {
  70. $folder = New-Folder -Location $vfolder -Name $vm.Folder -Confirm:$confirm
  71. Move-VM -Destination $folder -VM $vm.Name -Confirm:$confirm
  72. }
  73.  
  74. # Move the VM to the right resource pool. You can leave the resource pool blank in the CSV file.
  75. if ($rp = Get-ResourcePool -Location $datacenter | Where-Object { $_ -eq $vm.ResourcePool }) {
  76. Move-VM -Destination $rp -VM $vm.Name -Confirm:$confirm
  77. } # Will refrain from adding new resource pools if they don't exist.
  78.  
  79. # Set Comments
  80. $description = "Cloned from " + $vm.Template + " at " + $mmddyyyy
  81. Set-CustomField -Entity $newvm -Name "Creator" -Value $me -Confirm:$confirm
  82. Set-CustomField -Entity $newvm -Name "Email" -Value $email -Confirm:$confirm
  83. Set-VM -VM $newvm -Description $description -Confirm:$confirm
  84.  
  85. # Power on the VM.
  86. Start-VM -VM $vm.Name -Confirm:$confirm
  87. }
  88. }
Add Comment
Please, Sign In to add comment