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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# Load Windows PowerShell cmdlets for managing vSphere Add-PsSnapin VMware.VimAutomation.Core -ea "SilentlyContinue" #provide option for optional path (TODO) $pathToOVF = "C:\ovf\vCCNode-2.7.1.0-2651441_OVF10.ovf" $vc = "vCenter address" echo "Connecting to $vc" $adUserAccount = "$([Environment]::UserDomainName)\$([Environment]::UserName)" # get the AD username running this script $creds = Get-Credential -UserName $adUserAccount -Message "Please enter your AD Password" $username = $creds.username $password = $creds.GetNetworkCredential().password # Get current domain using logged-on user's credentials $CurrentDomain = "LDAP://"+([ADSI]"").distinguishedName $domain = New-Object DirectoryServices.DirectoryEntry($CurrentDomain,$username,$password) if ($domain.name -eq $null) { write-host "Authentication failed - please verify your username and password." exit #terminate the script. } else { write-host -foregroundcolor Green "Successfully authenticated with LDAP" #> $vi = "vi://$username:$password@$vc" #Connect to vCenter write-host -foregroundcolor Green "Connecting to vCenter" Connect-VIServer -Server $vc -User $username -Password $password #Get Datastores write-host "" write-host "##############################################################################" write-host "" write-host -foregroundcolor Green "Retrieving Datastores" #get-datastore | Select Name, @{N="FreeSpaceGB";E={[System.Math]::Round($_.FreeSpaceMB / 1024, 2)}} , @{N="CapacityGB";E={$_.CapacityMB / 1024}}, @{N="UsedGB";E={[System.Math]::Round(($_.CapacityMB - $_.FreeSpaceMB) / 1024, 2)}} #Prompt datastore selection $datastores = get-datastore | select Name,FreeSpaceMB #Sets some static info $LargestFreeSpace = "0" $LargestDatastore = $null #Performs the calculation of which datastore has most free space foreach ($datastore in $datastores) { if ($Datastore.FreeSpaceMB -gt $LargestFreeSpace) { $LargestFreeSpace = $Datastore.FreeSpaceMB $LargestDatastore = $Datastore.name } } #Writes out the result to the PowerShell Console write-host "$LargestDatastore has been selected as the largest datastore with $LargestFreeSpace MB Free Space" write-host "" write-host "##############################################################################" write-host "" $env = "enter port group name could be partial dmz* etc" #Lookup port group write-host -foregroundcolor Green "Retrieving Port Groups" $pg = get-VirtualPortgroup -Name $env | select Name $pgt = $pg.Name.SubString(0) echo "Port group selected is $pgt" write-host "" write-host "##############################################################################" write-host "" # Calculate or assign IP's to variables echo "GW will be set to XXX" echo "DNS will be set to XXX" echo "Network IP will be set to XXX" echo "Subnet Mask will be set to XXX" write-host "" write-host "##############################################################################" write-host "" $name="xxx" & 'C:\Program Files\VMware\VMware OVF Tool\ovftool.exe' --allowExtraConfig --acceptAllEulas --powerOn --name=$name --diskMode=thick --datastore="$LargestDatastore" --network="$pgt" --prop:vami.DNS.VMware_vCloud_Connector_Server="XXX" --prop:vami.netmask0.VMware_vCloud_Connector_Server="255.255.255.192" --prop:vami.ip0.VMware_vCloud_Connector_Server="XXX" --prop:vami.gateway.VMware_vCloud_Connector_Server="XXX" "$pathToOVF" " $vi" |
EDIT – Struggling with VAMI property key does not exist, here’s the workaround. Let the VM boot without applying network settings and use PowerCLI – Invoke-VMScript
1 2 3 4 5 6 7 8 9 10 |
$output = Invoke-VMScript -VM $name -ScriptText "/opt/vmware/share/vami/vami_set_network eth0 STATICV4 10.X.X.X 255.255.255.0 10.X.X.X" -GuestUser root -GuestPassword vmware -WarningAction SilentlyContinue $output.ScriptOutput write-host -foregroundcolor green "vami_set_network completed" write-host "" $output = Invoke-VMScript -VM $name -ScriptText "/opt/vmware/share/vami/vami_set_dns 8.8.8.8" -GuestUser root -GuestPassword vmware -WarningAction SilentlyContinue $output.ScriptOutput write-host "" write-host -foregroundcolor green "vami_set_dns completed" $output = Invoke-VMScript -VM $name -ScriptText "service network restart eth0" -GuestUser root -GuestPassword vmware -WarningAction SilentlyContinue $output.ScriptOutput |
Enjoy.
Datastore calculations found on http://www.spug.co.uk/?p=185 @Thanks!
Also William Lam’s post http://www.virtuallyghetto.com/2011/11/unattended-deployment-of-vcloud.html – Thanks!