· 7 years ago · Jan 21, 2019, 12:08 PM
1# -------------------------------------------------------------------------- #
2# Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs #
3# #
4# Licensed under the Apache License, Version 2.0 (the "License"); you may #
5# not use this file except in compliance with the License. You may obtain #
6# a copy of the License at #
7# #
8# http://www.apache.org/licenses/LICENSE-2.0 #
9# #
10# Unless required by applicable law or agreed to in writing, software #
11# distributed under the License is distributed on an "AS IS" BASIS, #
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
13# See the License for the specific language governing permissions and #
14# limitations under the License. #
15#--------------------------------------------------------------------------- #
16
17# Original work by:
18
19#################################################################
20##### Windows Powershell Script to configure OpenNebula VMs #####
21##### Created by andremonteiro@ua.pt and tsbatista@ua.pt #####
22##### DETI/IEETA Universidade de Aveiro 2011 #####
23#################################################################
24
25Start-Transcript -Append -Path "$env:SystemDrive\.opennebula-context.out" | Out-Null
26
27Write-Output "Running Script: $($MyInvocation.InvocationName)"
28Get-Date
29Write-Output ""
30
31Set-ExecutionPolicy unrestricted -force # not needed if already done once on the VM
32[string]$computerName = "$env:computername"
33[string]$ConnectionString = "WinNT://$computerName"
34
35function getContext($file) {
36 Write-Host "Loading Context File"
37 $context = @{}
38 switch -regex -file $file {
39 "^([^=]+)='(.+?)'$" {
40 $name, $value = $matches[1..2]
41 $context[$name] = $value
42 }
43 }
44 return $context
45}
46
47function envContext($context) {
48 ForEach ($h in $context.GetEnumerator()) {
49 $name = "Env:"+$h.Name
50 Set-Item $name $h.Value
51 }
52}
53
54function addLocalUser($context) {
55 # Create new user
56 $username = $context["USERNAME"]
57 $password = $context["PASSWORD"]
58
59 if ($username -Or $password) {
60
61 if ($username -eq $null) {
62 # ATTENTION - Language/Regional settings have influence on the naming
63 # of this user. Use the User SID instead (S-1-5-21domain-500)
64 $username = (Get-WmiObject -Class "Win32_UserAccount" |
65 where { $_.SID -like "S-1-5-21[0-9-]*-500" } |
66 select -ExpandProperty Name)
67 }
68
69 Write-Output "Creating Account for $username"
70
71 $ADSI = [adsi]$ConnectionString
72
73 if(!([ADSI]::Exists("WinNT://$computerName/$username"))) {
74 # User does not exist, Create the User
75 Write-Output "- Creating account"
76 $user = $ADSI.Create("user",$username)
77 $user.setPassword($password)
78 $user.SetInfo()
79 } else {
80 # User exists, Set Password
81 Write-Output "- Setting Password"
82 $admin = [ADSI]"WinNT://$env:computername/$username"
83 $admin.psbase.invoke("SetPassword", $password)
84 }
85
86 # Set Password to Never Expire
87 Write-Output "- Setting password to never expire"
88 $admin = [ADSI]"WinNT://$env:computername/$username"
89 $admin.UserFlags.value = $admin.UserFlags.value -bor 0x10000
90 $admin.CommitChanges()
91
92 # Add user to local Administrators
93 # ATTENTION - Language/Regional settings have influence on the naming
94 # of this group. Use the Group SID instead (S-1-5-32-544)
95 $groups = (Get-WmiObject -Class "Win32_Group" |
96 where { $_.SID -like "S-1-5-32-544" } |
97 select -ExpandProperty Name)
98
99 ForEach ($grp in $groups) {
100
101 # Make sure the Group exists
102 If([ADSI]::Exists("WinNT://$computerName/$grp,group")) {
103
104 # Check if the user is a Member of the Group
105 $group = [ADSI] "WinNT://$computerName/$grp,group"
106 $members = @($group.psbase.Invoke("Members"))
107
108 $memberNames = @()
109 $members | ForEach-Object {
110 # https://p0w3rsh3ll.wordpress.com/2016/06/14/any-documented-adsi-changes-in-powershell-5-0/
111 $memberNames += ([ADSI]$_).psbase.InvokeGet('Name')
112 }
113
114 If (-Not ($memberNames -Contains $username)) {
115
116 # Make sure the user exists, again
117 if([ADSI]::Exists("WinNT://$computerName/$username")) {
118
119 # Add the user
120 Write-Output "- Adding to $grp"
121 $group.Add("WinNT://$computerName/$username")
122 }
123 }
124 }
125 }
126 }
127 Write-Output ""
128}
129
130function configureNetwork($context) {
131
132 # Get the NIC in the Context
133 $nicIds = ($context.Keys | Where {$_ -match '^ETH\d+_IP6?$'} | ForEach-Object {$_ -replace '(^ETH|_IP$|_IP6$)',''} | Sort-Object -Unique)
134
135 $nicId = 0;
136
137 foreach ($nicId in $nicIds) {
138 # Retrieve data from Context
139 $nicIpKey = "ETH" + $nicId + "_IP"
140 $nicIp6Key = "ETH" + $nicId + "_IP6"
141 $nicPrefix = "ETH" + $nicId + "_"
142
143 $ipKey = $nicPrefix + "IP"
144 $netmaskKey = $nicPrefix + "MASK"
145 $macKey = $nicPrefix + "MAC"
146 $dnsKey = $nicPrefix + "DNS"
147 $dnsSuffixKey = $nicPrefix + "SEARCH_DOMAIN"
148 $gatewayKey = $nicPrefix + "GATEWAY"
149 $networkKey = $nicPrefix + "NETWORK"
150
151 $ip6Key = $nicPrefix + "IP6"
152 $ip6ULAKey = $nicPrefix + "IP6_ULA"
153 $ip6PrefixKey = $nicPrefix + "IP6_PREFIX_LENGTH"
154 $gw6Key = $nicPrefix + "GATEWAY6"
155 $mtuKey = $nicPrefix + "MTU"
156
157 $ip = $context[$ipKey]
158 $netmask = $context[$netmaskKey]
159 $mac = $context[$macKey]
160 $dns = (($context[$dnsKey] -split " " | Where {$_ -match '^(([0-9]*).?){4}$'}) -join ' ')
161 $dns6 = (($context[$dnsKey] -split " " | Where {$_ -match '^(([0-9A-F]*):?)*$'}) -join ' ')
162 $dnsSuffix = $context[$dnsSuffixKey]
163 $gateway = $context[$gatewayKey]
164 $network = $context[$networkKey]
165 $mtu = $context[$mtuKey]
166
167 $ip6 = $context[$ip6Key]
168 $ip6ULA = $context[$ip6ULAKey]
169 $ip6Prefix = $context[$ip6PrefixKey]
170 $gw6 = $context[$gw6Key]
171
172 $mac = $mac.ToUpper()
173 if (!$netmask) {
174 $netmask = "255.255.255.0"
175 }
176 if (!$ip6Prefix) {
177 $ip6Prefix = "64"
178 }
179 if (!$network) {
180 $network = $ip -replace "\.[^.]+$", ".0"
181 }
182 if ($nicId -eq 0 -and !$gateway) {
183 $gateway = $ip -replace "\.[^.]+$", ".1"
184 }
185
186 # Load the NIC Configuration Object
187 $nic = $false
188 $retry = 30
189 do {
190 $retry--
191 Start-Sleep -s 1
192 $nic = Get-WMIObject Win32_NetworkAdapterConfiguration | `
193 where {$_.IPEnabled -eq "TRUE" -and $_.MACAddress -eq $mac}
194 } while (!$nic -and $retry)
195
196 If (!$nic) {
197 Write-Output ("Configuring Network Settings: " + $mac)
198 Write-Output (" ... Failed: Interface with MAC not found")
199 Continue
200 }
201
202 Write-Output ("Configuring Network Settings: " + $nic.Description.ToString())
203
204 # Release the DHCP lease, will fail if adapter not DHCP Configured
205 Write-Output "- Release DHCP Lease"
206 $ret = $nic.ReleaseDHCPLease()
207 If ($ret.ReturnValue) {
208 Write-Output (" ... Failed: " + $ret.ReturnValue.ToString())
209 } Else {
210 Write-Output " ... Success"
211 }
212
213 if ($ip) {
214 # set static IP address and retry for few times if there was a problem
215 # with acquiring write lock (2147786788) for network configuration
216 # https://msdn.microsoft.com/en-us/library/aa390383(v=vs.85).aspx
217 Write-Output "- Set Static IP"
218 $retry = 10
219 do {
220 $retry--
221 Start-Sleep -s 1
222 $ret = $nic.EnableStatic($ip , $netmask)
223 } while ($ret.ReturnValue -eq 2147786788 -and $retry);
224 If ($ret.ReturnValue) {
225 Write-Output (" ... Failed: " + $ret.ReturnValue.ToString())
226 } Else {
227 Write-Output " ... Success"
228 }
229
230 # Set IPv4 MTU
231 if ($mtu) {
232 Write-Output "- Set MTU: ${mtu}"
233 netsh interface ipv4 set interface $nic.InterfaceIndex mtu=$mtu
234
235 If ($?) {
236 Write-Output " ... Success"
237 } Else {
238 Write-Output " ... Failed"
239 }
240 }
241
242 if ($gateway) {
243
244 # Set the Gateway
245 Write-Output "- Set Gateway"
246 $ret = $nic.SetGateways($gateway)
247 If ($ret.ReturnValue) {
248 Write-Output (" ... Failed: " + $ret.ReturnValue.ToString())
249 } Else {
250 Write-Output " ... Success"
251 }
252
253 If ($dns) {
254
255 # DNS Servers
256 $dnsServers = $dns -split " "
257
258 # DNS Server Search Order
259 Write-Output "- Set DNS Server Search Order"
260 $ret = $nic.SetDNSServerSearchOrder($dnsServers)
261 If ($ret.ReturnValue) {
262 Write-Output (" ... Failed: " + $ret.ReturnValue.ToString())
263 } Else {
264 Write-Output " ... Success"
265 }
266
267 # Set Dynamic DNS Registration
268 Write-Output "- Set Dynamic DNS Registration"
269 $ret = $nic.SetDynamicDNSRegistration("TRUE")
270 If ($ret.ReturnValue) {
271 Write-Output (" ... Failed: " + $ret.ReturnValue.ToString())
272 } Else {
273 Write-Output " ... Success"
274 }
275
276 # WINS Addresses
277 # $nic.SetWINSServer($DNSServers[0], $DNSServers[1])
278 }
279
280 if ($dnsSuffix) {
281
282 # DNS Suffixes
283 $dnsSuffixes = $dnsSuffix -split " "
284
285 # Set DNS Suffix Search Order
286 Write-Output "- Set DNS Suffix Search Order"
287 $ret = ([WMIClass]"Win32_NetworkAdapterConfiguration").SetDNSSuffixSearchOrder(($dnsSuffixes))
288 If ($ret.ReturnValue) {
289 Write-Output (" ... Failed: " + $ret.ReturnValue.ToString())
290 } Else {
291 Write-Output " ... Success"
292 }
293
294 # Set Primary DNS Domain
295 Write-Output "- Set Primary DNS Domain"
296 $ret = $nic.SetDNSDomain($dnsSuffixes[0])
297 If ($ret.ReturnValue) {
298 Write-Output (" ... Failed: " + $ret.ReturnValue.ToString())
299 } Else {
300 Write-Output " ... Success"
301 }
302 }
303 }
304 }
305
306 if ($ip6) {
307 # We need the connection ID (i.e. "Local Area Connection",
308 # which can be discovered from the NetworkAdapter object
309 $na = Get-WMIObject Win32_NetworkAdapter | `
310 where {$_.deviceId -eq $nic.index}
311
312
313 # Disable router discovery
314 Write-Output "- Disable IPv6 router discovery"
315 netsh interface ipv6 set interface $na.NetConnectionId `
316 advertise=disabled routerdiscover=disabled | Out-Null
317
318 If ($?) {
319 Write-Output " ... Success"
320 } Else {
321 Write-Output " ... Failed"
322 }
323
324 # Remove old IPv6 addresses
325 Write-Output "- Removing old IPv6 addresses"
326 if (Get-Command Remove-NetIPAddress -errorAction SilentlyContinue) {
327 # Windows 8.1 and Server 2012 R2 and up
328 # we want to remove everything except the link-local address
329 Remove-NetIPAddress -InterfaceAlias $na.NetConnectionId `
330 -AddressFamily IPv6 -Confirm:$false `
331 -PrefixOrigin Other,Manual,Dhcp,RouterAdvertisement `
332 -errorAction SilentlyContinue
333
334 If ($?) {
335 Write-Output " ... Success"
336 } Else {
337 Write-Output " ... Nothing to do"
338 }
339 } Else {
340 Write-Output " ... Not implemented"
341 }
342
343 # Set IPv6 Address
344 Write-Output "- Set IPv6 Address"
345 netsh interface ipv6 add address $na.NetConnectionId $ip6/$ip6Prefix
346
347 If ($? -And $ip6ULA) {
348 netsh interface ipv6 add address $na.NetConnectionId $ip6ULA/64
349 }
350
351 If ($?) {
352 Write-Output " ... Success"
353 } Else {
354 Write-Output " ... Failed"
355 }
356
357 # Set IPv6 Gateway
358 if ($gw6) {
359 Write-Output "- Set IPv6 Gateway"
360 netsh interface ipv6 add route ::/0 $na.NetConnectionId $gw6
361
362 If ($?) {
363 Write-Output " ... Success"
364 } Else {
365 Write-Output " ... Failed"
366 }
367 }
368
369 # Set IPv6 MTU
370 if ($mtu) {
371 Write-Output "- Set IPv6 MTU: ${mtu}"
372 netsh interface ipv6 set interface $nic.InterfaceIndex mtu=$mtu
373
374 If ($?) {
375 Write-Output " ... Success"
376 } Else {
377 Write-Output " ... Failed"
378 }
379 }
380
381 # Remove old IPv6 DNS Servers
382 Write-Output "- Removing old IPv6 DNS Servers"
383 netsh interface ipv6 set dnsservers $na.NetConnectionId source=static address=
384
385 If ($dns6) {
386 # Set IPv6 DNS Servers
387 Write-Output "- Set IPv6 DNS Servers"
388 $dns6Servers = $dns6 -split " "
389 foreach ($dns6Server in $dns6Servers) {
390 netsh interface ipv6 add dnsserver $na.NetConnectionId address=$dns6Server
391 }
392 }
393
394 doPing($ip6)
395 }
396
397 # Get the NIC in the Context
398 $aliasIds = ($context.Keys | Where {$_ -match "^ETH${nicId}_ALIAS\d+_IP6?$"} | ForEach-Object {$_ -replace '(^ETH\d+_ALIAS|_IP$|_IP6$)',''} | Sort-Object -Unique)
399
400 foreach ($aliasId in $aliasIds) {
401 $aliasPrefix = "ETH${nicId}_ALIAS${aliasId}"
402 $aliasIp = $context[$aliasPrefix + '_IP']
403 $aliasNetmask = $context[$aliasPrefix + '_MASK']
404 $aliasIp6 = $context[$aliasPrefix + '_IP6']
405 $aliasIp6ULA = $context[$aliasPrefix + '_IP6_ULA']
406 $aliasIp6Prefix = $context[$aliasPrefix + '_IP6_PREFIX_LENGTH']
407
408 if (!$aliasNetmask) {
409 $aliasNetmask = "255.255.255.0"
410 }
411
412 if (!$aliasIp6Prefix) {
413 $aliasIp6Prefix = "64"
414 }
415
416 if ($aliasIp) {
417 Write-Output "- Set Additional Static IP (${aliasPrefix})"
418 netsh interface ipv4 add address $nic.InterfaceIndex $aliasIp $aliasNetmask
419
420 If ($?) {
421 Write-Output " ... Success"
422 } Else {
423 Write-Output " ... Failed"
424 }
425 }
426
427 if ($aliasIp6) {
428 Write-Output "- Set Additional IPv6 Address (${aliasPrefix})"
429 netsh interface ipv6 add address $nic.InterfaceIndex $aliasIp6/$aliasIp6Prefix
430
431 If ($? -And $aliasIp6ULA) {
432 netsh interface ipv6 add address $nic.InterfaceIndex $aliasIp6ULA/64
433 }
434
435 If ($?) {
436 Write-Output " ... Success"
437 } Else {
438 Write-Output " ... Failed"
439 }
440 }
441 }
442
443 If ($ip) {
444 doPing($ip)
445 }
446 }
447
448 Write-Output ""
449}
450
451function renameComputer($context) {
452
453 # Initialize Variables
454 $current_hostname = hostname
455 $context_hostname = $context["SET_HOSTNAME"]
456 $logged_hostname = "Unknown"
457
458 if (! $context_hostname) {
459 return
460 }
461
462 # Check for the .opennebula-renamed file
463 If (Test-Path "$env:SystemDrive\.opennebula-renamed") {
464
465 # Grab the JSON content
466 $json = Get-Content -Path "$env:SystemDrive\.opennebula-renamed" `
467 | Out-String
468
469 # Convert to a Hash Table and set the Logged Hostname
470 try {
471 $status = $json | ConvertFrom-Json
472 $logged_hostname = $status.ComputerName
473 }
474 # Invalid JSON
475 catch [System.ArgumentException] {
476 Write-Output "Invalid JSON:"
477 Write-Output $json.ToString()
478 }
479 }
480
481 If ((!(Test-Path "$env:SystemDrive\.opennebula-renamed")) -or `
482 ($context_hostname.ToLower() -ne $logged_hostname.ToLower())) {
483
484 # .opennebula-renamed not found or the logged_name does not match the
485 # context_name, rename the computer
486
487 Write-Output "Changing Hostname to $context_hostname"
488 # Load the ComputerSystem Object
489 $ComputerInfo = Get-WmiObject -Class Win32_ComputerSystem
490
491 # Rename the computer
492 $ret = $ComputerInfo.rename($context_hostname)
493
494 $contents = @{}
495 $contents["ComputerName"] = $context_hostname
496 ConvertTo-Json $contents | Out-File "$env:SystemDrive\.opennebula-renamed"
497
498 # Check success
499 If ($ret.ReturnValue) {
500
501 # Returned Non Zero, Failed, No restart
502 Write-Output (" ... Failed: " + $ret.ReturnValue.ToString())
503 Write-Output " Check the computername."
504 Write-Output "Possible Issues: The name cannot include control" `
505 "characters, leading or trailing spaces, or any of" `
506 "the following characters: `" / \ [ ] : | < > + = ; , ?"
507
508 } Else {
509
510 # Returned Zero, Success
511 Write-Output "... Success"
512
513 # Restart the Computer
514 Write-Output "... Rebooting"
515 Restart-Computer -Force
516
517 # Exit here so the script doesn't continue to run
518 Exit 0
519 }
520 } else {
521 If ($current_hostname -eq $context_hostname) {
522 Write-Output "Computer Name already set: $context_hostname"
523 }
524 ElseIf (($current_hostname -ne $context_hostname) -and `
525 ($context_hostname -eq $logged_hostname)) {
526 Write-Output "Computer Rename Attempted but failed:"
527 Write-Output "- Current: $current_hostname"
528 Write-Output "- Context: $context_hostname"
529 }
530 }
531 Write-Output ""
532}
533
534function enableRemoteDesktop()
535{
536 Write-Output "Enabling Remote Desktop"
537 # Windows 7 only - add firewall exception for RDP
538 Write-Output "- Enable Remote Desktop Rule Group"
539 netsh advfirewall Firewall set rule group="Remote Desktop" new enable=yes
540
541 # Enable RDP
542 Write-Output "- Enable Allow Terminal Services Connections"
543 $ret = (Get-WmiObject -Class "Win32_TerminalServiceSetting" -Namespace root\cimv2\terminalservices).SetAllowTsConnections(1)
544 If ($ret.ReturnValue) {
545 Write-Output (" ... Failed: " + $ret.ReturnValue.ToString())
546 } Else {
547 Write-Output " ... Success"
548 }
549 Write-Output ""
550}
551
552function enablePing()
553{
554 Write-Output "Enabling Ping"
555 #Create firewall manager object
556 $fwm=new-object -com hnetcfg.fwmgr
557
558 # Get current profile
559 $pro=$fwm.LocalPolicy.CurrentProfile
560
561 Write-Output "- Enable Allow Inbound Echo Requests"
562 $ret = $pro.IcmpSettings.AllowInboundEchoRequest=$true
563 If ($ret) {
564 Write-Output " ... Success"
565 } Else {
566 Write-Output " ... Failed"
567 }
568
569 Write-Output ""
570}
571
572function doPing($ip, $retries=20)
573{
574 Write-Output "- Ping Interface IP $ip"
575
576 $ping = $false
577 $retry = 0
578 do {
579 $retry++
580 Start-Sleep -s 1
581 $ping = Test-Connection -ComputerName $ip -Count 1 -Quiet -ErrorAction SilentlyContinue
582 } while (!$ping -and ($retry -lt $retries))
583
584 If ($ping) {
585 Write-Output " ... Success ($retry tries)"
586 } Else {
587 Write-Output " ... Failed ($retry tries)"
588 }
589}
590
591function runScripts($context, $contextLetter)
592{
593 Write-Output "Running Scripts"
594
595 # Get list of scripts to run, " " delimited
596 $initscripts = $context["INIT_SCRIPTS"]
597
598 if ($initscripts) {
599
600 # Parse each script and run it
601 ForEach ($script in $initscripts.split(" ")) {
602
603 $script = $contextLetter + $script
604 If (Test-Path $script) {
605 Write-Output "- $script"
606 envContext($context)
607 & $script
608 }
609
610 }
611 }
612
613 # Execute START_SCRIPT or START_SCRIPT_64
614 $startScript = $context["START_SCRIPT"]
615 $startScript64 = $context["START_SCRIPT_BASE64"]
616
617 If ($startScript64) {
618 $startScript = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($startScript64))
619 }
620
621 If ($startScript) {
622
623 # Save the script as .opennebula-startscript.ps1
624 $startScriptPS = "$env:SystemDrive\.opennebula-startscript.ps1"
625 $startScript | Out-File $startScriptPS "UTF8"
626
627 # Launch the Script
628 Write-Output "- $startScriptPS"
629 envContext($context)
630 & $startScriptPS
631
632 }
633 Write-Output ""
634}
635
636function extendPartition($disk, $part)
637{
638 "select disk $disk","select partition $part","extend" | diskpart | Out-Null
639}
640
641function extendPartitions()
642{
643 Write-Output "- Extend partitions"
644
645 #$diskIds = ((wmic diskdrive get Index | Select-String "[0-9]+") -replace '\D','')
646 $diskId = 0
647
648 $partIds = ((wmic partition where DiskIndex=$diskId get Index | Select-String "[0-9]+") -replace '\D','' | %{[int]$_ + 1})
649
650 ForEach ($partId in $partIds) {
651 extendPartition $diskId $partId
652 }
653}
654
655function reportReady()
656{
657 $reportReady = $context["REPORT_READY"]
658
659 if ($reportReady) {
660 Write-Output "Report Ready to onegate"
661
662 try {
663 $body = "READY = YES"
664 $token= Get-Content "${contextLetter}token.txt"
665 $target= $context.ONEGATE_ENDPOINT+"/vm"
666 [System.Net.HttpWebRequest] $webRequest = [System.Net.WebRequest]::Create($target)
667 $webRequest.Timeout = 10000
668 $webRequest.Method = "PUT"
669 $webRequest.Headers.Add("X-ONEGATE-TOKEN", $token)
670 $webRequest.Headers.Add("X-ONEGATE-VMID", $context.VMID)
671 $buffer = [System.Text.Encoding]::UTF8.GetBytes($body)
672 $webRequest.ContentLength = $buffer.Length
673 $requestStream = $webRequest.GetRequestStream()
674 $requestStream.Write($buffer, 0, $buffer.Length)
675 $requestStream.Flush()
676 $requestStream.Close()
677 $response = $webRequest.getResponse()
678
679 if ($response.StatusCode -eq "OK") {
680 Write-Output " ... Success"
681 } else {
682 Write-Output " ... Failed"
683 Write-Output $response.StatusCode
684 }
685 }
686 catch {
687 $errorMessage = $_.Exception.Message
688
689 Write-Output " ... Failed"
690 Write-Output $errorMessage
691 }
692 }
693}
694################################################################################
695# Main
696################################################################################
697
698# Check the working WMI
699if (-Not (Get-WMIObject -ErrorAction SilentlyContinue Win32_Volume)) {
700 Write-Output "WMI not ready, exiting"
701 Stop-Transcript | Out-Null
702 exit 1
703}
704
705Write-Output "Detecting contextualization data"
706Write-Output "- Looking for CONTEXT ISO"
707
708# Get all drives and select only the one that has "CONTEXT" as a label
709$contextDrive = Get-WMIObject Win32_Volume | ? { $_.Label -eq "CONTEXT" }
710
711if ($contextDrive) {
712 Write-Output " ... Found"
713
714 # At this point we can obtain the letter of the contextDrive
715 $contextLetter = $contextDrive.Name
716 $contextLetter = "C:\"
717 $contextScriptPath = $contextLetter + "context.sh"
718} else {
719 Write-Output " ... Not found"
720 Write-Output "- Looking for VMware tools"
721
722 # Try the VMware API
723 foreach ($pf in ${env:ProgramFiles}, ${env:ProgramFiles(x86)}, ${env:ProgramW6432}) {
724 $vmtoolsd = "${pf}\VMware\VMware Tools\vmtoolsd.exe"
725 if (Test-Path $vmtoolsd) {
726 Write-Output " ... Found in ${vmtoolsd}"
727 break
728 } else {
729 Write-Output " ... Not found in ${vmtoolsd}"
730 }
731 }
732
733 $vmwareContext = ""
734 if (Test-Path $vmtoolsd) {
735 $vmwareContext = & $vmtoolsd --cmd "info-get guestinfo.opennebula.context" | Out-String
736 }
737
738 if ("$vmwareContext" -eq "") {
739 Write-Host "No contextualization data found"
740 Stop-Transcript | Out-Null
741 exit 1
742 }
743
744 [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($vmwareContext)) | Out-File "$env:SystemDrive\context.sh" "UTF8"
745 $contextScriptPath = "$env:SystemDrive\context.sh"
746}
747
748# Execute script
749if(Test-Path $contextScriptPath) {
750 $context = getContext $contextScriptPath
751 extendPartitions
752 renameComputer $context
753 addLocalUser $context
754 enableRemoteDesktop
755 enablePing
756 configureNetwork $context
757 runScripts $context $contextLetter
758 reportReady
759}
760
761Stop-Transcript | Out-Null