-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathAutomate vCloud Agent Deployment.ps1
62 lines (46 loc) · 3.2 KB
/
Automate vCloud Agent Deployment.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#Variable declaration
$vCenterIPorFQDN="192.168.243.172"
$vCenterUsername="Administrator@vsphere.local"
$vCenterPassword="vmware"
$pathToVcloudAgent="/opt/vmware/vcloud-director/agent" #Path of vCloud Agent in vCloud Director virtual machine
$FileVcloudAgent="vcloudagent-esx51-5.1.0-799577.zip" #vCloud Agent file that will be installed
$downloadDestination="C:\Users\Paolo\Desktop" #Where to download vCloud Agent on local machine
$vCdVmName="vCloud Director" #Name of vCloud Director virtual machine
$vCdVmUsername="root" #vCloud Director virtual machine username
$vCdVmPassword="vmware" #vCloud Director virtual machine password
$InstallHosts= @("192.168.243.144") #IP or FQDN of ESXi hosts on which vcloud agent will be installed
$HostUsername="root" #This assumes every host in both clusters have same user/password
$HostPassword="mypassword"
$puttyScpLocation='C:\Users\Paolo\Downloads\pscp.exe' #Location of pscp.exe (Putty Secure Copy) executable - Please preserve single quotes
Write-Host "Connecting to vCenter" -foregroundcolor "magenta"
Connect-VIServer -Server $vCenterIPorFQDN -Username $vCenterUsername -Password $vCenterPassword
#Get new vCloud agent downloading it from the vCloud Director VM
Write-Host "Downloading" $FileVcloudAgent "to your local machine in" $downloadDestination "directory" -foregroundcolor "magenta"
Copy-VMGuestFile -Source "$($pathToVcloudAgent)/$($FileVcloudAgent)" -Destination $downloadDestination -VM $vCdVmName -GuestToLocal -GuestUser $vCdVmUsername -GuestPassword $vCdVmPassword
foreach ($element in $InstallHosts) {
#Set Host into Maintenance Mode
Write-Host "Entering maintenance mode" -foregroundcolor "magenta"
Get-VMHost -Name $element | Set-VMHost -State Maintenance
#Start SSH service on host in order to allow the copy of vcloud agent using Secure Copy
Start-VMHostService -HostService (Get-VMHostService -VMHost $element | Where-Object Label -eq "SSH") -Confirm:$false
#Check whether vcloud agent is installed or not
$esx=Get-EsxCli -VMHost $element
$isAlreadeyInstalled = $esx.software.vib.list($true) | Where-Object Name -like "vcloud-agent" | Select-Object Version
if ($isAlreadeyInstalled.Version -eq $null) { #No previous agent on host.
Write-Host "No vCloud agent found, proceeding to installation" -foregroundcolor "magenta"
}else{ #Uninstall previous agent
Write-Host "vCloud agent found. Let me uninstall it before proceeding" -foregroundcolor "magenta"
$esx.software.vib.remove($false,$false,$true,$false,"vcloud-agent")
}
#Transfer vcloud agent over to ESXi host using Putty Secure Copy
Write-Host "Copying vCloud agent over to ESXi host" -foregroundcolor "magenta"
& $puttyScpLocation -pw $HostPassword "$($downloadDestination)\$($FileVcloudAgent)" "$($HostUsername)@$($element):/tmp"
#Install vcloud agent
Write-Host "Installing vCloud agent" -foregroundcolor "magenta"
$esx.software.vib.install("/tmp/$($FileVcloudAgent)", $false, $false, $true, $false, $false)
#Stop SSH service on host
Stop-VMHostService -HostService (Get-VMHostService -VMHost $element | Where-Object Label -eq "SSH") -Confirm:$false
#Exit Maintenance Mode
Write-Host "Exiting maintenance mode" -foregroundcolor "magenta"
Get-VMHost -Name $element | Set-VMHost -State Connected
}