#Checks if the OS Version is more than 6.2 i.e. Server 2012
function CheckByOSCurrentVersion($strComputerDNS) #As Boolean
{
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $strComputerDNS)
$regKey = $reg.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion")
$strCurrentOSVer = $regKey.GetValue("CurrentVersion")
if($strCurrentOSVer -ge $WIN_OS_2012_Ver)
{
return $true;
}
return $false;
}
#--------------------------------------------------------------
# Tests for NULL or Empty
#--------------------------------------------------------------
function IsNullOrEmpty($str)
{
return (($null -eq $str) -or (0 -eq $str.length))
}
#-----------------------------------------------------------
# Returns the DN domain name from DNS
#-----------------------------------------------------------
function DNDomainFromDNS($strDomainDNsName)
{
# Determine DN domain name from RootDSE object.
$query = "LDAP://$strDomainDNsName/RootDSE"
$objRootDSE = [System.DirectoryServices.DirectoryEntry]([ADSI]$query)
return $objRootDSE.Get("defaultNamingContext");
}
#-----------------------------------------------------------
# Returns the forest for the domain
#-----------------------------------------------------------
function ForestFromDomainDNS($strDNSDomain)
{
$strForestDNS = $null;
try
{
$query = "LDAP://$strDNSDomain/RootDSE"
$objRootDSE = [System.DirectoryServices.DirectoryEntry]([ADSI]$query);
$strForestDN = $objRootDSE.Get("rootdomainNamingContext");
# We got the DN (DC=corp,DC=microsoft,DC=com), translate to DNS (corp.microsoft.com)
$arrParseDN = $strForestDN.split(",");
for ($i = 0; $i -lt $arrParseDN.length; $i++)
{
$arrParseDC = $arrParseDN[$i].split("=");
if ($null -eq $strForestDNS)
{
$strForestDNS = $arrParseDC[1];
}
else
{
$strForestDNS = $strForestDNS + "." + $arrParseDC[1];
}
}
}
catch
{
; # Nothing to be done
}
return $strForestDNS
}
Function Load-CimModules
{
$error.Clear()
$CimModule = Get-Module CimCmdlets
if ($null -eq $CimModule)
{
Import-Module CimCmdlets
$error.Clear()
}
}
#-----------------------------------------------------------
# Returns the NetBIOS domain name from the DNS domain name
#-----------------------------------------------------------
function NetBIOSDomainFromDN($strDNSComputerName)
{
if($Is_OS_More_Than_2012)
{
Load-CimModules
#-------------------------------------------------------------
# Gets the IP addresses for the given computer name
#-------------------------------------------------------------
function GetIPAddresses($strDNSComputerName)
{
$strIPs =""
if($Is_OS_More_Than_2012)
{
Load-CimModules
#-----------------------------------------------------------
# Get the site name from the Computer DNS
#-----------------------------------------------------------
function GetSiteFromComputerDNS($strComputerDNS)
{
$strSiteName = $null;
#If SiteName override is $null or empty, try DynamicSiteName
if(IsNullOrEmpty $strSiteName)
{
$strSiteName = $regKey.GetValue("DynamicSiteName")
}
}
catch
{
; # Nothing to be done
}
return $strSiteName;
}
#-----------------------------------------------------------
# Adds the property to the instance if the value is non-$null
#-----------------------------------------------------------
function AddClassProperty($oInstance, $strProperty, $strValue)
{
if ($null -ne $strValue) {
$oInstance.AddProperty($strProperty, $strValue);
}
}
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $strDNSComputerName)
$regKey = $reg.OpenSubKey("SOFTWARE\\Microsoft\\Virtual Machine\\Guest\\Parameters")
$strVirtualMachineName = $regKey.GetValue("VirtualMachineName")
}
catch
{
; # Nothing to be done
}
# Attempt to do things the 'right' way
try
{
$E_CLUSTER_RESOURCE_NOT_FOUND = -2146823281;
# Get the computer from the system
$astrSplit = ""
$objComputer = $null
$colSettings = $null
$astrSplit = $strDNSComputerName.split(".")
$strNetBIOSComputerName = $astrSplit[0]
try
{
$query = "Select Domain, Name, NumberOfLogicalProcessors, NumberOfProcessors from Win32_ComputerSystem WHERE Name = ""$strNetBIOSComputerName"""
if($Is_OS_More_Than_2012)
{
Load-CimModules
try
{
# Get the domain data. If computer is in a workgroup, it will catch exception.
$strDomainDN = DNDomainFromDNS $strDomainDNsName
}
catch
{
Write-Host "Domain Data Exception caught for " + $strDomainDNsName
}
if ($strDomainDN -ne $null)
{
$strNetBIOSDomain = NetBIOSDomainFromDN $strDNSComputerName
}
# Unable to contact the machine, (mis)use the DNS name
catch
{
$e = $_.Exception.Message
$message = "Exception retrieving properties '" + $e + "', using failsafe method" # Do nothing
Write-Host $message
}
if((IsNullOrEmpty $strNetBIOSComputerName) -or (IsNullOrEmpty $strNetBIOSDomain))
{
# Try to parse the DNS name of the system
$arrNameSplit = $strDNSComputerName.split(".");
if (IsNullOrEmpty $strNetBIOSComputerName -and ($arrNameSplit.length > 0))
{
$strNetBIOSComputerName = $arrNameSplit[0];
}
if ((IsNullOrEmpty $strNetBIOSDomain) -and ($arrNameSplit.length > 1))
{
$strNetBIOSDomain = $arrNameSplit[1];
}
# If there is no DNS name (no '.') then this is a workgroup, so use the domain name from WMI as NetBIOS Domain
if ((IsNullOrEmpty $strNetBIOSDomain) -and (-not (IsNullOrEmpty $strDomainDnsName)))
{
$strNetBIOSDomain = $strDomainDNsName;
}
}
# Get the forest if we have the Domain DNS name
if((IsNullOrEmpty $strForestDnsName) -and (-not (IsNullOrEmpty $strDomainDNsName)))
{
$strForestDnsName = ForestFromDomainDNS $strDomainDNsName
}
# Get the site name
if (IsNullOrEmpty $strSite)
{
$strSite = GetSiteFromComputerDNS $strDNSComputerName
}