Post by Chris128 on Nov 5, 2014 1:05:07 GMT
The DirectoryServices.ActiveDirectory.Domain class includes a function that lets you raise the domain functional level, but it does not seem to include any properties or functions that let you check what level the domain is currently at. So I wrote this little extension method to get this information:
To use it to check the functional level of the logged on user's domain, you would just do this:
Oh and for anyone that is not a fan of extension methods, here's another version of it in a standard shared/static function that accepts a domain name and username/password:
Imports System.Runtime.CompilerServices
Imports System.DirectoryServices
Public Module ExtensionMethods
Public Enum DomainFunctionalLevel As Integer
Windows2000 = 0
Windows2003Mixed = 1
Windows2003 = 2
Windows2008 = 3
Windows2008R2 = 4
Windows2012 = 5
Windows2012R2 = 6
AboveWindows2012R2 = 7
End Enum
<Extension()>
Public Function GetFunctionalLevel(Domain As ActiveDirectory.Domain) As DomainFunctionalLevel
Using DomainRoot As DirectoryEntry = Domain.GetDirectoryEntry
Dim FunctionalLevel As DomainFunctionalLevel = DomainFunctionalLevel.Windows2000
If DomainRoot.Properties.Contains("msDS-Behavior-Version") Then
Dim IntValue As Integer = CInt(DomainRoot.Properties("msDS-Behavior-Version").Value)
If IntValue > DomainFunctionalLevel.Windows2012R2 Then
FunctionalLevel = DomainFunctionalLevel.AboveWindows2012R2
Else
FunctionalLevel = DirectCast(IntValue, DomainFunctionalLevel)
End If
End If
Return FunctionalLevel
End Using
End Function
End Module
To use it to check the functional level of the logged on user's domain, you would just do this:
Using Domain As ActiveDirectory.Domain = ActiveDirectory.Domain.GetCurrentDomain
MessageBox.Show(Domain.GetFunctionalLevel.ToString)
End Using
Oh and for anyone that is not a fan of extension methods, here's another version of it in a standard shared/static function that accepts a domain name and username/password:
Imports System.DirectoryServices
Imports System.DirectoryServices.ActiveDirectory
...
Public Shared Function GetDomainFunctionalLevel(DomainName As String, Username As String, Password As String) As DomainFunctionalLevel
Using DomainRoot = Domain.GetDomain(New DirectoryContext(DirectoryContextType.Domain, DomainName, ConnectionUsername, ConnectionPassword))
Using DomainRootEntry As DirectoryEntry = DomainRoot.GetDirectoryEntry
Dim Level As DomainFunctionalLevel = DomainFunctionalLevel.Windows2000
If DomainRootEntry.Properties.Contains("msDS-Behavior-Version") Then
Dim IntValue As Integer = CInt(DomainRootEntry.Properties("msDS-Behavior-Version").Value)
If IntValue > DomainFunctionalLevel.Windows2012R2 Then
Level = DomainFunctionalLevel.AboveWindows2012R2
Else
Level = DirectCast(IntValue, DomainFunctionalLevel)
End If
End If
Return Level
End Using
End Using
End Function
'Same enum that is already shown in the extension method but copied here for completeness
Public Enum DomainFunctionalLevel As Integer
Windows2000 = 0
Windows2003Mixed = 1
Windows2003 = 2
Windows2008 = 3
Windows2008R2 = 4
Windows2012 = 5
Windows2012R2 = 6
AboveWindows2012R2 = 7
End Enum