Category Archives: Image Builder

More Auto Deploy PowerCLI shortcuts

Following on from a post I created a while back which gave you a couple of new functions to use with Auto Deploy I wanted to add a few extra pieces of code which I use on a regular basis that may save people time when they are building out their image profiles or working with auto deploy:

Adding the VMware hosted software depots to your PowerCLI Session:

Add-EsxSoftwareDepot https://hostupdate.vmware.com/software/VUM/PRODUCTION/main/vmw-depot-index.xml

Listing the 5 most recent image profiles from the online depot (Thanks to Andreas Peetz):

Get-EsxImageProfile | Sort-Object -Descending -Property @{Expression={$_.Name.Substring(0,10)}},@{Expression={$_.CreationTime.Date}},Name | Select -first 5 | FT -AutoSize

image

List the latest image profile which includes VMTools:

Continue reading

Easily Creating PowerShell Quick References

I needed to provide some examples of PowerShell code recently for a quick reference poster and thought this might be useful for others looking for quick code examples.

All the examples you need can be found by using Get-Help with the –Examples parameter.  Did you know that as with everything else in PowerShell these items are returned as nice objects, we can easily pick the items we need to create a some text which can be copied into a quick reference document.  Of course I am assuming the PowerShell module has fully supported and correctly added help code Winking smile

Just replace the module name below and you are away !

The Code

Get-Command -Module VMware.ImageBuilder | Sort Name | Foreach {
	$Examples = Get-Help $_ -Examples
	Write-Host -ForegroundColor Blue $examples.Name

	$examples.examples.Example | Foreach {
		$Remarks = $_.Remarks | Select -ExpandProperty Text
		$Code = $_ | Select -ExpandProperty Code
		Write-Host -ForegroundColor DarkGreen "# $($Remarks)"
		Write-Host $Code
	}
	Write-Host
}

Example output

Add-EsxSoftwareDepot
# Connect to a depot.
Add-EsxSoftwareDepot https://hostupdate.vmware.com/software/VUM/PRODUCTION/main/vmw-depot-index.xml
# Connect to a depot, saving it to a variable.
$depot = Add-EsxSoftwareDepot https://hostupdate.vmware.com/software/VUM/PRODUCTION/main/vmw-depot-index.xml

Add-EsxSoftwarePackage
# Add a package by name to an image profile:
Add-EsxSoftwarePackage -ImageProfile “My custom profile” -SoftwarePackage net-bnx2
# Add a package of a specific name and version:
Add-EsxSoftwarePackage -ImageProfile “My custom profile” -SoftwarePackage “net-bnx2 1.6.7-0.1OEM1”
# Clone an image profile, then add a package by name, in one line using pipelining:
New-EsxImageProfile -CloneProfile “ESX-5.0-234567-standard” -Name “My custom profile” | \
Add-EsxSoftwarePackage net-bnx2

Compare-EsxImageProfile
# Compares Profile 1 with Profile 2.
Compare-EsxImageProfile “Profile 1” “Profile 2”

Export-EsxImageProfile
# Export an ISO image
Export-EsxImageProfile -ImageProfile “Evan’s Profile” -ExportToIso -FilePath c:\isos\evans-iso.iso
# Clone an image profile, add a software package, then export to offline bundle.
New-EsxImageProfile -CloneProfile “ESXi-5.0.0-234567-standard” -Name “Evan’s Profile”
Add-EsxSoftwarePackage -ImageProfile “Evan’s Profile” -SoftwarePackage cisco-vem-v140
Export-EsxImageProfile -ImageProfile “Evan’s Profile” -ExportToBundle -FilePath c:\isos\base-plus-vem.zip

Get-EsxImageProfile
# Display all image profiles from depots and all image profiles the user created during this PowerCLI session:
Get-EsxImageProfile
# Display all ESX 5.0 profiles:
Get-EsxImageProfile -Name “ESX-5.0*”
# Display all image profiles from vendors other than VMware:
Get-EsxImageProfile | ? {$_.Vendor -ne “VMware”}
# List all the VIB packages from a particular image profile:
(Get-EsxImageProfile -Name “Profile A”).VibList

Get-EsxSoftwareChannel
#

Get-EsxSoftwarePackage
# List all the VIBs from all depots in table form:
Get-EsxSoftwarePackage
# List all the VIBs, sorted by date:
Get-EsxSoftwarePackage | Sort-Object ReleaseDate | Format-Table -Property Name,Version,Vendor
# List all the VIBs from VMware and Cisco released after Jan 1, 2010:
Get-EsxSoftwarePackage -Vendor “VMware”,”Cisco” -ReleasedAfter 1/1/2010
# List all the VIBs from vendors other than VMware
Get-EsxSoftwarePackage | ? {$_.Vendor -ne “VMware”}
# List all the base VIBs for the 5.0.0 release:
Get-EsxSoftwarePackage -Name “esx-base” -Version “5.0.0-*”
# Save the results of a VIB query for later:
$vibs = Get-EsxSoftwarePackage -Name “esx-base” -Version “5.0.0-*”

New-EsxImageProfile
# Clone an image profile, give it a new name, and change the acceptance level. (NOTE: The ‘\’ is used to continue the second line of input; either press ENTER after \ or enter everything on one line without the ‘\’).
New-EsxImageProfile -CloneProfile “ESX-5.0-234567-standard” \
-Name “My custom profile” -AcceptanceLevel CommunitySupported
# Create an image profile from scratch, assigning the result to a variable.  Software packages are specified by name.
$ip = New-EsxImageProfile -NewProfile -Name “Built from scratch!” -Vendor “NotVmware” \
-SoftwarePackage esx-base,esx-tboot,misc-drivers
# Create an image profile from scratch, passing in software packages via pipeline
Get-EsxSoftwarePackage -Name esx-base,esx-tboot,misc-drivers |  \
New-EsxImageProfile -NewProfile -Name “Built from scratch!” -Vendor “NotVmware”

Remove-EsxSoftwareDepot
# Connect to a depot, then disconnect from it by URL.
Add-EsxSoftwareDepot https://hostupdate.vmware.com/software/VUM/PRODUCTION/main/vmw-depot-index.xml
[… do something …]
Remove-EsxSoftwareDepot https://hostupdate.vmware.com/software/VUM/PRODUCTION/main/vmw-depot-index.xml
# Connect to a depot, saving it to a variable, then disconnect from it later.  Also an example of pipeline input.
$depot = Add-EsxSoftwareDepot https://hostupdate.vmware.com/software/VUM/PRODUCTION/main/vmw-depot-index.xml
[… do something …]
$depot | Remove-EsxSoftwareDepot
# Disconnect from all software depots
Remove-EsxSoftwareDepot $DefaultSoftwareDepots

Remove-EsxSoftwarePackage
# Remove package foo from my custom profile:
Remove-EsxSoftwarePackage -ImageProfile “My custom profile” -SoftwarePackage foo

Set-EsxImageProfile
# Modify the VIB list of an existing image profile
Set-EsxImageProfile -ImageProfile “Profile of a Fool” -SoftwarePackage esx-base,scsi-ips,esx-tboot
# Change the acceptance level (maybe so that some VIB with a lower acceptance level can be added) of the third image profile from a list (index starts at 0):
$myprofiles = Get-EsxImageProfile
Set-EsxImageProfile -ImageProfile $myprofiles[2] -AcceptanceLevel PartnerSupported

Updating your Image Profile

If you have been using the image builder and auto deploy cmdlets which came with vSphere 5.0 then you will already know how easy it is to completely rebuild all hosts just by replacing a simple auto deploy rule with your new image profile and then rebooting your hosts.

But how do you create your new image profile ?

An Image profile normally consists of a base ESXi image downloaded from VMware and then some custom software packages added in to ensure you have all the third party addons, drivers and utilities needed for the host to work with your infrastructure.

An example of this is the EMC PowerPath Software Package or the HP ESXi Proliant offline bundle, others may include the software package needed for the vCD Agent.

Once these software packages have been added to your image profile you are then able to export it back as an offline zip file, ISO file or use it in you Auto Deploy rule-set.

But when it comes to updating you need to remember which software packages you added to which build of ESX and repeat this procedure, this can be time consuming.

Introducing Update-ESXImageProfile

Did you know that VMware has an online repository of up to date versions of ESXi Software Depots ?  You can easily add this to your Image Builder session with the following line of code:

Add-EsxSoftwareDepot https://hostupdate.vmware.com/software/VUM/PRODUCTION/main/vmw-depot-index.xml

image

Using this online depot I was able to create a script which would download any newer software packages from the base ESXi Software Depot online and compare these against your current image profile, once this has been done it will add these latest versions to your image profile ensuring you have an up-to-date Image Profile.

Of course you will need to make sure you export this as a zip again so you can use it in the future and also add it to your Auto Deploy rule-set and reboot your hosts.

See it in action

The Script

Function Update-ESXImageProfile ($ImageProfile, $tools){
	if ($ImageProfile.Readonly) {
		Write-Host "Your ImageProfile is read-only and therefore cannot be updated, please use a custom ImageProfile"
	} Else {
		Write "Loading online Software Depot"
		$SD = Add-EsxSoftwareDepot -DepotUrl https://hostupdate.vmware.com/software/VUM/PRODUCTION/main/vmw-depot-index.xml
		If ($tools) {
			$NEWIP = Get-EsxImageProfile -Vendor "VMware, Inc." | Sort ModifiedTime -Descending | Where { $_.Name -notlike "*tools" } | Select -First 1
		} Else {
			$NEWIP = Get-EsxImageProfile -Vendor "VMware, Inc." | Sort ModifiedTime -Descending | Where { $_.Name -like "*tools" } | Select -First 1
		}
		Write-Host "New Image Profile found called $($NEWIP.Name)..."
		Write-Host "Checking for updated packages which do not exist in $ImageProfile"		
		$Compare = Compare-EsxImageProfile -ReferenceProfile $ImageProfile -ComparisonProfile $NEWIP
		$Updates = ($Compare | Select -ExpandProperty UpgradeFromRef)
		If ($Updates) {
			Foreach ($SP in $Updates) {
				$UpdatedPackage = Get-EsxSoftwarePackage | Where {$_.Guid -eq $SP}
				Write-Host "Adding $($UpdatedPackage.Name) to $ImageProfile"
				$Add = Add-EsxSoftwarePackage -ImageProfile $ImageProfile -SoftwarePackage $UpdatedPackage
			}
		}
		$OnlyInComp = ($Compare | Select -ExpandProperty OnlyInComp)
		If ($OnlyInComp) {
			Foreach ($SP in $OnlyInComp) {
				$UpdatedPackage = Get-EsxSoftwarePackage | Where {$_.Guid -eq $SP}
				Write-Host "Adding $($UpdatedPackage.Name) to $ImageProfile"
				$Add = Add-EsxSoftwarePackage -ImageProfile $ImageProfile -SoftwarePackage $UpdatedPackage
			}
		} 
		If ((-not $OnlyInComp) -and (-not $Updates)) {
			Write-Host "No Updates found for $ImageProfile"
		}
	}
}

Update-ESXImageProfile -ImageProfile "CustomImageProfile" -Tools $true

Auto Deploy and Image Builder profile functions

One of the best and one of my favorite ideas from the PowerCLI team (there are so many to choose from) was to include an initialization script when you launch PowerCLI, this gives us the nice black background, custom prompt and also other things like the Get-VICommand function.

Get-VICommand lists all the VMware related cmdlets added to your session, this is useful for quickly listing the VMware cmdlets you need, most people think this is actually a cmdlet which is part of PowerCLI but if you have ever imported the VMware snapin into a PowerShell session and tried to use Get-VICommand you will quickly realize this is not part of the cmdlet set.

Continue reading