1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 param( [string] $file)
18
19
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
32 $v1 = 'labvirutalcenter'
33 $v2 = 'testvirtualcenter'
34 $v3 = 'productionvirtualcenter'
35 $v4 = 'drvirtualcenter'
36
37 $dt = Get-Date -Format d
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 $xls = new-object -com Excel.Application
55 $path = "C:\scripts\" + $file
56 $xls.Workbooks.Open($path) | Out-Null
57
58
59 Remove-Job *
60
61
62 $Row = 6
63
64
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
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
91 while ((Get-Job | where {$_.State -eq "Running"}).getType -ne $null)
92 {
93 Sleep -Seconds 10
94 }
95
96
97 Stop-Process -Name "Excel"
98 Write-Host ""
99
100
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
118 if ($temp -eq "Win2K3-32")
119 {
120
121 $cust = "Win2003_32bit"
122 }
123 elseif ($temp -eq "Win2K3-64")
124 {
125
126 $cust = "Win2003_64bit"
127 }
128
129 elseif ($temp -eq "Win2K8R2")
130 {
131
132 $cust = "Win2008"
133 }
134
135
136 else
137 {
138 write "Your Guest Customizations are wrong"
139 break
140 }
141
142
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
164 else
165 {
166 write "Please input correct host name"
167 break
168 }
169
170
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
182 $onwer = Get-Acl
183 $deployed = $onwer.owner
184 $note = $in[10] + ' | Deployed by:' + $deployed + ' | Created:' + $in[13]
185
186
187 Connect-VIServer $in[11] -User $in[14] -Password $in[15]
188
189
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
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
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)