<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Active Directory Archives - LucD notes</title>
	<atom:link href="https://www.lucd.info/tag/active-directory/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.lucd.info/tag/active-directory/</link>
	<description>My PowerShell ramblings</description>
	<lastBuildDate>Fri, 04 Feb 2011 00:04:44 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://www.lucd.info/wp-content/uploads/2018/12/cropped-120px-Tibetan_Dharmacakra-32x32.png</url>
	<title>Active Directory Archives - LucD notes</title>
	<link>https://www.lucd.info/tag/active-directory/</link>
	<width>32</width>
	<height>32</height>
</image> 
<atom:link rel="hub" href="https://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="https://pubsubhubbub.superfeedr.com"/><atom:link rel="hub" href="https://websubhub.com/hub"/>	<item>
		<title>Script vSphere 4.1 AD Authentication</title>
		<link>https://www.lucd.info/2010/07/25/script-vsphere-4-1-ad-authentication/</link>
					<comments>https://www.lucd.info/2010/07/25/script-vsphere-4-1-ad-authentication/#comments</comments>
		
		<dc:creator><![CDATA[LucD]]></dc:creator>
		<pubDate>Sun, 25 Jul 2010 15:20:54 +0000</pubDate>
				<category><![CDATA[Active Directory]]></category>
		<category><![CDATA[Authentication]]></category>
		<category><![CDATA[PowerCLI]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[vSphere]]></category>
		<category><![CDATA[SDK]]></category>
		<guid isPermaLink="false">http://www.lucd.info/?p=2499</guid>

					<description><![CDATA[One of the new features that came with vSphere 4.1 was the ability [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>One of the new features that came with vSphere 4.1 was the ability to use <strong>Active Directory Authentication</strong> on ESX(i) servers for permissions, console access and ssh access.This is a great feature that you will probably want to activate on all your ESX(i) servers.</p>
<p>Unfortunately this new feature is not available in <a href="https://downloads.vmware.com/downloads/download.do?downloadGroup=SDKWIN41" target="_blank">PowerCLI 4.1</a>. That means you can&#8217;t set this up in your configuration scripts through a PowerCLI cmdlet. In most such cases you can fall back on one of the <a href="javascript:void(location.href='left-pane.html');%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20void(parent.frames[1].location.href='right-pane.html');" target="_blank">SDK APIs</a> to bypass this lack of a cmdlet. But unfortunately the new &#8220;managers&#8221;, of which <a href="https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.host.ActiveDirectoryAuthentication.html" target="_blank">HostActiveDirectoryAuthentication</a> is one, are not available in the VMware.Vim assembly either.</p>
<p><strong>Rob</strong> raised this in a recent <a href="https://communities.vmware.com/community/vmtn/vsphere/automationtools/powercli" target="_blank">PowerCLI Community</a> <a href="https://communities.vmware.com/message/1576319#1576319" target="_blank">thread</a>. <strong>Yasen</strong>, one of the PowerCLI Dev Team members, provided a bypass. To make this bypass a bit more accessible, I decided to roll it up in a PowerShell function.</p>
<p><span id="more-2499"></span></p>
<h2>A (little) bit of background information</h2>
<p>The <strong>Active Directory Authentication</strong> feature has been discussed at length in several <strong>vSphere 4.1</strong> blog posts.</p>
<p>A great video, that explains what it is and how you can use it, was made by <a href="https://twitter.com/davidmdavis" target="_blank">David Davis</a>. See his <a href="https://www.vmwarevideos.com/video-new-vsphere-4-1-windows-active-directory-authentication" target="_blank">VIDEO: New vSphere 4.1 Windows Active Directory Authentication</a> post.</p>
<h2>The script</h2>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">#requires -version 2

function Get-VMHostAuthentication{
&lt;#
.SYNOPSIS
	The function retrieves the authentication services from an ESX(i) host
.DESCRIPTION
	This function retrieves the configured authentication services from one
	or more ESX(i) hosts.
.NOTES
	Author: Luc Dekens
.PARAMETER VMHost
	Specify the ESX(i) host
.EXAMPLE
	PS&gt; Get-VMHost | Get-VMHostAuthentication
.EXAMPLE
	PS&gt; Get-VMHostAuthentication -VMHost (Get-VMHost)
#&gt;
	param(
	[parameter(ValueFromPipeline = $true,Position=1,Mandatory = $true)]
	[VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl[]]$VMHost)

	process{
		if(!$VMHost){$VMHost = $_}
		foreach($esx in $VMHost){
			$filter = New-Object VMware.Vim.PropertyFilterSpec -Property @{
				ObjectSet = New-Object VMware.Vim.ObjectSpec -Property @{
					Obj = $esx.ExtensionData.ConfigManager.AuthenticationManager
				}
				PropSet = New-Object VMware.Vim.PropertySpec -Property @{
					Type = &quot;HostAuthenticationManager&quot;
					All = $true
				}
			}
			$collector = Get-View $esx.ExtensionData.Client.ServiceContent.PropertyCollector
			$content = $collector.RetrieveProperties($filter)
			$stores = $content | Select -First 1 | %{$_.PropSet} | where {$_.Name -eq &quot;info&quot;}
			foreach($authConfig in $stores.Val.AuthConfig){
				$row = New-Object PSObject
				$row | Add-Member -MemberType NoteProperty -Name Name -Value $null
				$row | Add-Member -MemberType NoteProperty -Name Type -Value $null
				$row | Add-Member -MemberType NoteProperty -Name Enabled -Value $null
				$row | Add-Member -MemberType NoteProperty -Name Domain -Value $null
				$row | Add-Member -MemberType NoteProperty -Name Membership -Value $null
				$row | Add-Member -MemberType NoteProperty -Name Trust -Value $null
				$row.Name = $esx.Name
				$row.Enabled = $authConfig.Enabled
				switch($authConfig.GetType().Name){
					'HostLocalAuthenticationInfo'{
						$row.Type = 'Local authentication'
					}
					'HostActiveDirectoryInfo'{
						$row.Type = 'Active Directory'
						$row.Domain = $authConfig.JoinedDomain
						$row.Membership = $authConfig.DomainMembershipStatus
						$row.Trust = $authConfig.TrustedDomain
					}
				}
				$row
			}
		}
	}
}

function Set-VMHostADDomain{
&lt;#
.SYNOPSIS
	The function adds or removes an ESX(i) 4.1 server to/from an Active Directory domain
.DESCRIPTION
	By adding an ESX(i) 4.1 host to an AD domain, you can use AD authentication for console
	access, SSH and permissions on the host's child objects
.NOTES
	Author: Luc Dekens
.PARAMETER VMHost
	Specify the ESX(i) host
.PARAMETER Domain
	The name of the Active Directory domain in FQDN notation
.PARAMETER User
	An Active Directory account that administrative authority to add hosts to AD
.PARAMETER Password
	The password for the AD account specified in -User
.PARAMETER Credential
	The credentials for an AD account with administrative authority to add hosts to AD
.PARAMETER ADJoin
	A switch indicating if the host shall be added ($true) or removed ($false) from AD
.PARAMETER RemovePermission
	Will remove ($true) all AD permissions that still exist on the ESX(i) host and
	it's children
.EXAMPLE
	PS&gt; Get-VMHost | Set-VMHostADDomain -ADJoin:$true -Domain $domain -User $user -Password $pswd
.EXAMPLE
	PS&gt; Set-VMHostADDomain -VMHost (Get-VMHost) -ADJoin:$true -Credential $cred
.EXAMPLE
	PS&gt; Set-VMHostADDomain -VMHost (Get-VMHost) -ADJoin:$false -RemovePermission:$true
#&gt;
	param(
	[parameter(ValueFromPipeline = $true,Position=1,Mandatory = $true)]
	[VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl[]]$VMHost,
	[string]$Domain,
	[string]$User,
	[string]$Password,
	[System.Management.Automation.PSCredential]$Credential,
	[switch]$ADJoin,
	[switch]$RemovePermission = $false
	)

	process{
		if(!$VMHost){$VMHost = $_}
		foreach($esx in $VMHost){
			$filter = New-Object VMware.Vim.PropertyFilterSpec -Property @{
				ObjectSet = New-Object VMware.Vim.ObjectSpec -Property @{
					Obj = $esx.ExtensionData.ConfigManager.AuthenticationManager
				}
				PropSet = New-Object VMware.Vim.PropertySpec -Property @{
					Type = &quot;HostAuthenticationManager&quot;
					All = $true
				}
			}
			$collector = Get-View $esx.ExtensionData.Client.ServiceContent.PropertyCollector
			$content = $collector.RetrieveProperties($filter)
			$stores = $content | Select -First 1 | %{$_.PropSet} | where {$_.Name -eq &quot;supportedStore&quot;}
			$result = $stores.Val | where {$_.Type -eq &quot;HostActiveDirectoryAuthentication&quot;}
			$hostADAuth = [VMware.Vim.VIConvert]::ToVim41($result)

			if($ADJoin){
				if($Credential){
					$User = $Credential.GetNetworkCredential().UserName
					$Password = $Credential.GetNetworkCredential().Password
				}
				$taskMoRef = $esx.ExtensionData.Client.VimService.JoinDomain_Task($hostADAuth,$Domain,$User,$Password)
			}
			else{
				$taskMoRef = $esx.ExtensionData.Client.VimService.LeaveCurrentDomain_Task($hostADAuth,$RemovePermission)
			}
			$esx.ExtensionData.WaitForTask([VMware.Vim.VIConvert]::ToVim($taskMoRef))
		}
	}
}</pre><p></p>
<h4>Annotations</h4>
<p><strong>Line 1</strong>: This function uses PowerShell v2 features and will not run with PowerShell v1</p>
<p><strong>Line 3-63</strong>: The<strong> Get-VMHostAuthentication</strong> function</p>
<p><strong>Line 38-60</strong>: Return information for each of the configured authentication methods.</p>
<p><strong>Line 65-138</strong>: The <strong>Set-VMHostADDomain</strong> function</p>
<p><strong>Line 24,108</strong>: Handles use of the function in a pipeline or with a -VMHost parameter</p>
<p><strong>Line 26-34,110-118</strong>: Creates the <a href="https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vmodl.query.PropertyCollector.FilterSpec.html" target="_blank">PropertyFilterSpec</a> object through the use of the hash table property list.</p>
<p><strong>Line 119-123</strong>: The &#8216;bypass&#8217; from Yasen. It uses the <a href="https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vmodl.query.PropertyCollector.html" target="_blank">PropertyCollector</a> to  ultimately get at the MoRef of the <a href="https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.host.ActiveDirectoryAuthentication.html" target="_blank">HostActiveDirectoryAuthentication</a> manager.</p>
<p><strong>Line 123</strong>: The MoRef is converted to a VIM 4.1 MoRef with the <strong>ToVim41</strong> method of the builtin VIConvert class.</p>
<p><strong>Line 125</strong>: Depending on the state of the <strong>ADJoin</strong> switch, the host is &#8216;joined&#8217; or &#8216;removed&#8217; from the AD domain.</p>
<p><strong>Line 126-129</strong>: If the AD account is passed through a <a href="https://msdn.microsoft.com/en-us/library/system.management.automation.pscredential%28VS.85%29.aspx" target="_blank">PSCredential</a> object, the clear text account and password are extracted.</p>
<p><strong>Line 135</strong>: The script waits for the completion of the called method.</p>
<h2>Examples</h2>
<p>To display the authentication services for an ESX(i) server that has no AD authentication configured.</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">Get-VMHost $esxName | Get-VMHostAuthetication</pre><p></p>
<p>This returns the following information.</p>
<p><a href="https://lucd.info/wp-content/uploads/2010/07/auth-local-only.png"><img decoding="async" class="alignnone size-full wp-image-2512" title="auth-local-only" src="https://lucd.info/wp-content/uploads/2010/07/auth-local-only.png" alt="" width="583" height="75" srcset="https://www.lucd.info/wp-content/uploads/2010/07/auth-local-only.png 583w, https://www.lucd.info/wp-content/uploads/2010/07/auth-local-only-300x38.png 300w" sizes="(max-width: 583px) 100vw, 583px" /></a></p>
<p>To add a specific ESX(i) server to an Active Directory domain you can do</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">Set-VMHostADDomain -VMhost (Get-VMHost $esxName) -ADJoin:$true -Domain 'test.local' -User 'Administrator' -Password 'MyPassword'</pre><p></p>
<p>or</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">Get-VMHost $esxName | Set-VMHostADDomain -ADJoin:$true -Domain 'test.local' -User 'Administrator' -Password 'MyPassword'</pre><p></p>
<p>The Get-VMHostAuthentication function now returns.</p>
<p><a href="https://lucd.info/wp-content/uploads/2010/07/auth-local-and-ad.png"><img decoding="async" class="alignnone size-full wp-image-2513" title="auth-local-and-ad" src="https://lucd.info/wp-content/uploads/2010/07/auth-local-and-ad.png" alt="" width="626" height="73" srcset="https://www.lucd.info/wp-content/uploads/2010/07/auth-local-and-ad.png 626w, https://www.lucd.info/wp-content/uploads/2010/07/auth-local-and-ad-300x34.png 300w" sizes="(max-width: 626px) 100vw, 626px" /></a></p>
<p>You can also enable AD authentication on a series of ESX(i) hosts.</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">Set-VMHostADDomain -VMhost (Get-VMHost) -ADJoin:$true -Domain 'test.local' -Credential (Get-Credential)</pre><p></p>
<p>or like this</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">Get-VMHost | Set-VMHostADDomain -ADJoin:$true -Domain 'test.local' -Credential (Get-Credential)</pre><p></p>
<p>To remove a host, you have to set the <strong>-ADJoin</strong> switch to $false.<br />
Like this</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">Get-VMHost | Set-VMHostADDomain -ADJoin:$false</pre><p></p>
<p>The previous call assumes that there were no permissions with AD principals left on the ESX(i) server or any of it&#8217;s children.<br />
If there were, you would get an error message.</p>
<p><a href="https://lucd.info/wp-content/uploads/2010/07/ADAuthError.png"><img decoding="async" class="alignnone size-full wp-image-2510" title="ADAuthError" src="https://lucd.info/wp-content/uploads/2010/07/ADAuthError.png" alt="" width="690" height="52" srcset="https://www.lucd.info/wp-content/uploads/2010/07/ADAuthError.png 862w, https://www.lucd.info/wp-content/uploads/2010/07/ADAuthError-300x22.png 300w" sizes="(max-width: 690px) 100vw, 690px" /></a></p>
<p>You can let the &#8216;unjoin&#8217; remove any remaining permissions for you.<br />
For that you use the <strong>-RemovePermission</strong> parameter.</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">Get-VMHost | Set-VMHostADDomain -ADJoin:$false -RemovePermission:$true</pre><p></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lucd.info/2010/07/25/script-vsphere-4-1-ad-authentication/feed/</wfw:commentRss>
			<slash:comments>15</slash:comments>
		
		
			</item>
	</channel>
</rss>
