Skip to content

Instantly share code, notes, and snippets.

@lrivallain
Last active August 8, 2016 03:13
Show Gist options
  • Save lrivallain/63eb486fffadeb252953 to your computer and use it in GitHub Desktop.
Save lrivallain/63eb486fffadeb252953 to your computer and use it in GitHub Desktop.
This script inserts guestinfo to a VM configuration parameters. "guestinfo" settings can be read from Guest OS in order to customize network settings for example.
# load PowerCli Snapin
if ((Get-PSSnapin -Name VMware.Vimautomation.Core -ErrorAction SilentlyContinue) -eq $null ) {
Add-PsSnapin VMware.Vimautomation.Core
}
# vCenter server
$VC = "vcenter.domain"
$Username = "domain\user"
# connecting vCenter
$Credentials = get-credential -credential $Username
Connect-VIServer -server $VC -Credential $Credentials
# create a list of infos for the guest OS
$customization = @{
"hostname" = "happy-debian";
"ipaddress" = "192.168.109.18";
"netmask" = "255.255.255.0";
"gateway" = "192.168.109.1";
"dns1" = "192.168.109.2";
"dns2" = "192.168.109.3";
}
# create a reconfiguration task object
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
# add each information to the previous object
foreach ($info in $customization.GetEnumerator()) {
Write-Host "setting $($info.Name): $($info.Value)"
$extra = New-Object VMware.Vim.optionvalue
$extra.Key="guestinfo.$($info.Name)"
$extra.Value=$info.Value
$vmConfigSpec.extraconfig += $extra
}
# select the virtual machine and apply changes
$vm = Get-View -ViewType VirtualMachine | where { $_.name -eq "deb7GuestInfo" }
$vm.ReconfigVM($vmConfigSpec)
# clean leave
Disconnect-VIServer -Confirm:$false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment