Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#
# Copyright 2021, Alexis La Goutte <alexis dot lagoutte at gmail dot com>
#
# Small script for deploy Witness using (Import-)vAPP (need PowerCLI and already connected to vcenter)
$ovf_path = "C:\Users\Administrator\Downloads\VMware-VirtualSAN-Witness-7.0U1-16850804.ova"
$vm_host = "vsan01.example.com"
$datastore = "vsanDatastore"
$cluster = "VSAN"
$inventory = "WTS"
$name_vm = "WTS"
# cpu = "2"
# MemoryGB = "4"
$StartVM = $true
$passwd = "MyPassword"
$vsannetwork = "Secondary"
$ipaddress0 = "192.0.2.101"
$netmask0 = "255.255.255.0"
$gateway0 = "192.0.2.254"
$dnsDomain = "example.com"
$hostname = $name_vm
$dns = "192.0.2.1,198.51.100.1"
$ntp = "192.0.2.1,198.51.100.1"
#$ntp = "ntp.example.com" <= don't use FQDN for NTP don't work !!
$ipaddress1 = "172.22.3.2"
$netmask1 = "255.255.255.0"
$Management_Network = "DVS-VLAN-MGMT"
$Secondary_Network = "DVS-VLAN-VSAN"
#Don't change after...
$vapp_config = @{
"source" = $ovf_path
"name" = $name_vm
}
if (-not (Get-Cluster -Name $cluster -ErrorAction "silentlycontinue")) {
Throw "Cluster not found : $cluster"
}
else {
$vapp_config.add("Location", $cluster)
}
if (-not (Get-VMHost -Name $vm_host -ErrorAction "silentlycontinue")) {
Throw "Vm_Host not found : $vm_host"
}
else {
$vapp_config.add("vmhost", $vm_host)
}
if (-not (Get-Datastore -Name $datastore -ErrorAction "silentlycontinue")) {
Throw "Datastore not found : $datastore"
}
else {
$vapp_config.add("datastore", $datastore)
}
if ( $PsBoundParameters.ContainsKey('inventory') ) {
if (-not (Get-Inventory -Name $inventory -ErrorAction "silentlycontinue")) {
Throw "Inventory not found : $inventory"
}
else {
$vapp_config.add("inventory", $inventory)
}
}
$ovfConfig = Get-OvfConfiguration -Ovf $ovf_path
if ($passwd) {
$ovfConfig.Common.guestinfo.passwd.Value = $passwd
}
if ($vsannetwork) {
$ovfConfig.Common.guestinfo.vsannetwork.Value = $vsannetwork
}
if ($ipaddress0) {
$ovfConfig.Common.guestinfo.ipaddress0.Value = $ipaddress0
}
if ($netmask0) {
$ovfConfig.Common.guestinfo.netmask0.Value = $netmask0
}
if ($gateway0) {
$ovfConfig.Common.guestinfo.gateway0.Value = $gateway0
}
if ($dnsDomain) {
$ovfConfig.Common.guestinfo.dnsDomain.Value = $dnsDomain
}
if ($hostname) {
$ovfConfig.Common.guestinfo.hostname.Value = $hostname
}
if ($dns) {
$ovfConfig.Common.guestinfo.dns.Value = $dns
}
if ($ntp) {
$ovfConfig.Common.guestinfo.ntp.Value = $ntp
}
if ($ipaddress1) {
$ovfConfig.Common.guestinfo.ipaddress1.Value = $ipaddress1
}
if ($netmask1) {
$ovfConfig.Common.guestinfo.netmask1.Value = $netmask1
}
if ($gateway1) {
$ovfConfig.Common.guestinfo.gateway1.Value = $gateway1
}
if ( $Management_Network ) {
$ovfConfig.NetworkMapping.Management_Network.Value = $Management_Network
}
if ( $Secondary_Network ) {
$ovfConfig.NetworkMapping.Secondary_Network.Value = $Secondary_Network
}
Import-VApp @vapp_config -OvfConfiguration $ovfConfig | Out-Null
if ( $PsBoundParameters.ContainsKey('MemoryGB') ) {
Get-VM $name_vm | Set-VM -MemoryGB $MemoryGB -confirm:$false | Out-Null
}
if ( $PsBoundParameters.ContainsKey('CPU') ) {
Get-VM $name_vm | Set-VM -NumCPU $cpu -confirm:$false | Out-Null
}
if ( $PsBoundParameters.ContainsKey('CapacityGB') ) {
(Get-VM $name_vm | Get-HardDisk)[1] | Set-Harddisk -CapacityGB $CapacityGB -confirm:$false | Out-Null
}
if ( $StartVM ) {
Get-VM $name_vm | Start-VM | Out-Null
Write-Output "$name_vm is started and ready to use"
}
else {
Write-Output "$name_vm is ready to use (need to Start VM !)"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment