1 #VM Deployment script by David Chung 8/12/2011
   2 #This script is multithreaded VM deployment script using spreadsheet across multiple vsphere servers.
   3 #
   4 #It will log the result in C:\scripts\log\ folder
   5 #Copy autobuildv2.xls to C:\scripts folder.
   6 
   7 # --- Note that our windows standard template has two hard drives and two network (lan and backup) ---
   8 # --- Depending on customer requirement, CPU, RAM, Disk size, and Network VLAN connection changes. ---
   9 
  10 # Spreadsheet file should be:
  11 # VM name | Host name | CPU | RAM (GB) | DISK 2 (GB) | DISK 3 (GB) | NIC1 Connection | Template name | DataStore | Notes
  12 
  13 #Use following command to launch the script
  14 #./autobuild [spreadsheetname]
  15 
  16 
  17 param( [string] $file)
  18 
  19 #Update User ID and Password
  20 $user = 'username'
  21 $password = 'password'
  22 
  23 if ($file -eq ""){
  24         Write-Host
  25         Write-Host "Please specify spreadsheet file name eg...."
  26         Write-Host "./autobuildv2.ps1 spreadsheetname.xls" -ForegroundColor yellow
  27         Write-Host ""
  28         Write-Host ""
  29         exit
  30 }
  31 # Replace with your virtual center name
  32 $v1 = 'labvirutalcenter'
  33 $v2 = 'testvirtualcenter'
  34 $v3 = 'productionvirtualcenter'
  35 $v4 = 'drvirtualcenter'
  36 
  37 $dt = Get-Date -Format d
  38 
  39 #Connect to VI server using saved credentials
  40 
  41 #$credlb = Get-VICredentialStoreItem -Host $v1 -File C:\labcredential.xml
  42 #Connect-VIServer $credlb.Host -User $credlb.User -Password $credlb.Password
  43 
  44 #$credpd = Get-VICredentialStoreItem -Host $v3 -File C:\pdcredential.xml
  45 #Connect-VIServer $credpd.Host -User $credpd.User -Password $credpd.Password
  46 
  47 #$credts = Get-VICredentialStoreItem -Host $v2 -File C:\tscredential.xml
  48 #Connect-VIServer $credts.Host -User $credts.User -Password $credts.Password
  49 
  50 #$creddn = Get-VICredentialStoreItem -Host $v4 -File C:\dncredential.xml
  51 #Connect-VIServer $creddn.Host -User $creddn.User -Password $creddn.Password
  52 
  53 #open excel and read values
  54 $xls = new-object -com Excel.Application
  55 $path = "C:\scripts\" + $file
  56 $xls.Workbooks.Open($path) | Out-Null
  57 
  58 # Removes any existing jobs
  59 Remove-Job *
  60 
  61 # Starts from Row 6 on the spreadsheet
  62 $Row = 6
  63 
  64 # Loop starts
  65 for ($name -ne $null)
  66 {
  67         $name = $xls.Cells.Item($Row,1).Value()
  68         $vhost = $xls.Cells.Item($Row,2).Value()
  69         $cpu = $xls.Cells.Item($Row,3).Value()
  70         $memgb = $xls.Cells.Item($Row,4).Value()
  71         $dgb = $xls.Cells.Item($Row,5).Value()
  72         $dgb2 = $xls.Cells.Item($Row,6).Value()
  73         $net = $xls.Cells.Item($Row,7).Value()
  74         $temp = $xls.Cells.Item($Row,8).Value()
  75         $nfs = $xls.Cells.Item($Row,9).Value()
  76         $desc = $xls.Cells.Item($Row,10).Value()
  77         $vmdisk = $dgb * 1048576
  78         $vmdisk2 = $dgb2 * 1048576
  79         $memmb = $memgb * 1024
  80         $cp = $Row - 6
  81 
  82 # End of the loop when there is no data in the row.
  83         if ($name -eq $null) 
  84         { 
  85                 Write-Host ""
  86                 Write-Host ""
  87                 Write-Host "(" $cp ") VM Build in progress.  Please check virtual center for detail." -ForegroundColor Magenta
  88                 Write-Host "The script will end when ALL VMs are completed." -ForegroundColor Magenta
  89                 
  90                 # Waits until all jobs are finished
  91                 while ((Get-Job | where {$_.State -eq "Running"}).getType -ne $null)     
  92                 {     
  93                 Sleep -Seconds 10     
  94                 } 
  95                 
  96                 # Stops Excel process
  97                 Stop-Process -Name "Excel"
  98                 Write-Host ""
  99                 
 100                 # Writes Jobs in to log file
 101                 $Date = Get-Date
 102                 $logfile = "C:\scripts\log\autobuild" + "_" + $Date.Day + "-" + $Date.Month + "-" + $Date.Year + ".txt"
 103                 if (-not (test-path c:\scripts\log\))
 104                         {
 105                         MD c:\scripts\log | Out-Null
 106                         }                  
 107                 Receive-Job * | Out-File -Encoding ASCII -FilePath $logfile -Append
 108                 Remove-Job *
 109                 
 110                 Write-Host "Automated VM build is completed." -ForegroundColor Yellow
 111                 Write-Host ""
 112                 Invoke-Item $logfile
 113                 exit
 114         }
 115         
 116                         
 117         # Select the correct customization script
 118         if ($temp -eq "Win2K3-32")
 119         {
 120         # Customization script name
 121                 $cust = "Win2003_32bit"
 122         }
 123         elseif ($temp -eq "Win2K3-64")
 124         {
 125         # Customization script name
 126                 $cust = "Win2003_64bit"
 127         }
 128         
 129         elseif ($temp -eq "Win2K8R2")
 130         {
 131         # Customization script name
 132                 $cust = "Win2008"
 133         }
 134                         
 135         #if no customization script is selected, break out of the script
 136         else
 137         {
 138                 write "Your Guest Customizations are wrong"
 139                 break
 140         }
 141         
 142 #Select Vsphere server name based on ESX host name provided
 143         if ($vhost -like "ESXLAB*")
 144         {
 145                 $v = $v1
 146         }
 147         
 148         elseif ($vhost -like "ESXTST*")
 149         {
 150                 $v = $v2
 151         }
 152         
 153         elseif ($vhost -like "ESXPRD*")
 154         {
 155                 $v = $v3
 156         }
 157         
 158         elseif ($vhost -like "ESXDR*")
 159         {
 160                 $v = $v4
 161         }
 162         
 163         #if incorrect host names are selected
 164         else
 165         {
 166                 write "Please input correct host name"
 167                 break
 168         }
 169         
 170         # Launch Multi-threaded job (VM build and configure)
 171         $job = 
 172         {
 173         $in = $input.'<>4__this'.read(); 
 174         
 175         Add-PSSnapin 'vmware.vimautomation.core'
 176         
 177         $vmdisk = $in[5] * 1048576
 178         $vmdisk2 = $in[6] * 1048576
 179         $memmb = $in[4]* 1024
 180         
 181         #VM note (description, deployed by: username, and build date)
 182         $onwer = Get-Acl
 183         $deployed = $onwer.owner
 184         $note = $in[10] + '  |  Deployed by:' + $deployed + '  |  Created:' + $in[13]
 185         
 186         #Connect to VI server
 187         Connect-VIServer $in[11] -User $in[14] -Password $in[15]
 188         
 189         #Build VM and configure
 190         New-VM -Server $in[11] -vmhost $in[2] -Name $in[1] -Template $in[8] -Datastore $in[9] -DiskStorageFormat thin -OSCustomizationSpec $in[12] -Location "Discovered virtual machine" -Description $note
 191         Set-VM -Server $in[11] -vm $in[1] -Numcpu $in[3] -MemoryMB $memmb -RunAsync -Confirm:$false
 192         $disk = Get-VM $in[1] | Get-HardDisk | ? {$_.Name -eq "Hard disk 2"}
 193         Set-HardDisk -harddisk $disk -CapacityKB $vmdisk -Confirm:$false
 194         if ($in[6] -gt 0)
 195                 {
 196                 New-HardDisk -Server $in[11] -VM $in[1] -CapacityKB $vmdisk2 -Confirm:$false
 197                 }
 198         $vmnet = Get-VM $in[1] | Get-NetworkAdapter | where { $_.Name -eq "Network Adapter 1" } 
 199         $vmnet | Set-NetworkAdapter -NetworkName $in[7] -StartConnected:$true -Confirm:$false
 200         }
 201         
 202         # pass variables in to jobs
 203         $jobspec=@()
 204         $jobSpec += $job
 205         $jobspec += $name
 206         $jobspec += $vhost 
 207         $jobspec += $cpu 
 208         $jobspec += $memgb
 209         $jobspec += $dgb 
 210         $jobspec += $dgb2
 211         $jobspec += $net 
 212         $jobspec += $temp
 213         $jobspec += $nfs
 214         $jobspec += $desc
 215         $jobspec += $v
 216         $jobspec += $cust
 217         $jobspec += $dt
 218         $jobspec += $user
 219         $jobspec += $password
 220         
 221         #start the job  
 222         Start-Job -InputObject $jobspec -ScriptBlock $jobspec[0]
 223         
 224         Write-Host ""
 225         Write-Host $name " VM is being deployed on " $v -BackgroundColor Green -ForegroundColor Black
 226         Write-host ""
 227         
 228         
 229         $Row++
 230 }

désert/VMware/PowerCLI/Autobuildv2-safe.ps1 (last edited 2020-05-14 03:42:00 by localhost)