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

3 thoughts on “Easily Creating PowerShell Quick References”

  1. Richard,

    This example doesnt show it being sent to a word doc (v2 maybe), although this is possible, I just copy and paste the output for now.

  2. hey I know this might sound like a silly question, but I have not been able to work it out.. Can this be sent to a word document to be able to keep as a reference??
    Thanks

Leave a Reply