Infrastructure Adventures

09/24/2012

Update vSphere Host Profile from Reference Host via PowerCLI

Filed under: Virtualization — Tags: , , , — Joe Keegan @ 7:18 PM

I’m dealing with many clusters of stateless hosts deployed via vSphere AutoDeploy and updating host profiles from reference host is something I badly needed. I was surprised this cmdlet didn’t exist so I wrote a function to do the job. Hope folks find this useful.

Function Update-VMHostProfile {
	<# 		
	.SYNOPSIS 			
		Updates the host profile from its reference host.		
	
	.DESCRIPTION 			
		The Update-VMHostProfile updates a host profile from its reference host.
		
	.PARAMETER  Profile
		Specify the host profile in which you wish to update from reference host.
		
	.EXAMPLE 			
		C:\PS> Update-VMHostProfile -Profile $Profile

		Updates the host profile $Profile from its reference host.

	.EXAMPLE 			
		C:\PS> Get-VMHost -Name $Host | Get-VMHostProfile | Update-VMHostProfile

		Get's the host profile attached to the VMHost $Host and updates it from the host profile's reference host.
	
	.EXAMPLE 			
		C:\PS> Get-VMHostProfile | Update-VMHostProfile

		Updates all host profiles from reference host.

	.NOTES
			Function has only been tested with vSphere 5.1, should work with vSphere 5.0.

	.LINK

http://infrastructureadventures.com/

	#>

	[CmdletBinding()]
	Param (
		[Parameter(Mandatory=$True,ValueFromPipeline=$True)]
		$Profile
	)

	BEGIN {}

	PROCESS {
	
		if ( ($Profile.GetType()).name -eq "String" ) {
			$Profile = Get-VMHostProfile -Name $Profile
		}

		$HostProfileReferenceHostId = ("host-" + (($Profile.ReferenceHost.Id).split("-"))[2])
	
		$HostProfileHostBasedConfigSpec = New-Object VMware.Vim.HostProfileHostBasedConfigSpec
		$HostProfileHostBasedConfigSpec.host = New-Object VMware.Vim.ManagedObjectReference
		$HostProfileHostBasedConfigSpec.host.type = "HostSystem"
		$HostProfileHostBasedConfigSpec.host.Value = $HostProfileReferenceHostId
		$HostProfileHostBasedConfigSpec.useHostProfileEngine = $true

		$HostProfileView = Get-View -Id $Profile.Id
		$HostProfileView.UpdateHostProfile($HostProfileHostBasedConfigSpec)
		
		$Profile
	}

	END {}
}
About these ads

2 Comments »

  1. very awesome! I think lots of people need this.

    Comment by vmjfk — 09/25/2012 @ 8:26 AM

  2. This is an awesome blog, really like the clear examples.

    Comment by Magnus — 10/22/2012 @ 1:14 PM


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Silver is the New Black Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 35 other followers

%d bloggers like this: