// Attempt to do things the 'right' way
try
{
var objWMIService = ConnectToWMI(strDNSComputerName);
var objComputer = GetWMIComputerSystem(objWMIService, strDNSComputerName);
try
{
// Get the domain data. If computer is in a workgroup, it will catch exception.
strDomainDN = DNDomainFromDNS(strDomainDnsName);
}
catch (e)
{
WScript.Echo("Domain Data Exception caught for " + strDomainDnsName);
}
if (strDomainDN != null)
{
strNetBIOSDomain = NetBIOSDomainFromDN(strDomainDN);
}
strIPAddresses = GetIPAddresses(objWMIService);
WScript.Echo("Creating ADSystemInfo object on " + strNetBIOSHostName);
// Attempt to remotely create the System Info object
var adSys = new ActiveXObject("ADSystemInfo", strNetBIOSHostName);
// Unable to contact the machine, (mis)use the DNS name
catch (e)
{
WScript.Echo("Exception retrieving properties '" + e.number + ": " + e.description + "', using failsafe method"); // Do nothing
}
if (IsNullOrEmpty(strNetBIOSComputerName) || IsNullOrEmpty(strNetBIOSDomain))
{
// Try to parse the DNS name of the system
var arrNameSplit = strDNSComputerName.split(".");
if (IsNullOrEmpty(strNetBIOSComputerName) && arrNameSplit.length > 0)
{
strNetBIOSComputerName = arrNameSplit[0];
}
if (IsNullOrEmpty(strNetBIOSDomain) && 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) && !IsNullOrEmpty(strDomainDnsName))
{
strNetBIOSDomain = strDomainDnsName;
}
}
if (IsNullOrEmpty(strDomainDnsName))
{
for (var i = 1; i < arrNameSplit.length; i++)
{
if (!IsNullOrEmpty(strDomainDnsName))
{
strDomainDnsName = strDomainDnsName + ".";
strDomainDnsName = strDomainDnsName + arrNameSplit[i];
}
else
{
strDomainDnsName = arrNameSplit[i];
}
}
}
// Get the ComputerOU if we have the NetBIOS and Domain DN
if (IsNullOrEmpty(strComputerOU) &&
!IsNullOrEmpty(strNetBIOSComputerName) &&
!IsNullOrEmpty(strDomainDN))
{
try
{
strComputerOU = GetComputerOUFromDN("cn="+strNetBIOSComputerName+","+strDomainDN);
}
catch (e)
{
; // Nothing to be done
}
}
// Get the forest if we have the Domain DNS name
if (IsNullOrEmpty(strForestDnsName) &&
!IsNullOrEmpty(strDomainDnsName))
{
strForestDnsName = ForestFromDomainDNS(strDomainDnsName);
}
// Get the site name
if (IsNullOrEmpty(strSite))
{
strSite = GetSiteFromComputerDNS(strDNSComputerName);
}
// Get the virtual machine information
GetVirtualInfoFromReg(strDNSComputerName);
//--------------------------------------------------------------
// Tests for NULL or Empty
//--------------------------------------------------------------
function IsNullOrEmpty(str)
{
return null == str || 0 == str.length;
}
//--------------------------------------------------------------
// Connects to WMI on the specified computer
//--------------------------------------------------------------
function ConnectToWMI(strDNSComputerName)
{
return GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\" + strDNSComputerName + "\\root\\cimv2");
}
//--------------------------------------------------------------
// Gets the WMI Computer object for the given DNS computer name
//--------------------------------------------------------------
function GetWMIComputerSystem(objWMIService, strDNSComputerName)
{
var E_CLUSTER_RESOURCE_NOT_FOUND = -2146823281;
// Get the computer from the system
var astrSplit, colSettings, objComputer;
astrSplit = strDNSComputerName.split(".");
strNetBIOSComputerName = astrSplit[0];
//-----------------------------------------------------------
// Returns the DN domain name from DNS
//-----------------------------------------------------------
function DNDomainFromDNS(strDNSDomain)
{
// Determine DN domain name from RootDSE object.
var objRootDSE = GetObject("LDAP://" + strDNSDomain + "/RootDSE");
return objRootDSE.Get("defaultNamingContext");
}
//-----------------------------------------------------------
// Returns the forest for the domain
//-----------------------------------------------------------
function ForestFromDomainDNS(strDNSDomain)
{
var strForestDNS = null;
try
{
var objRootDSE = GetObject("LDAP://" + strDNSDomain + "/RootDSE");
var strForestDN = objRootDSE.Get("rootdomainNamingContext");
// We got the DN (DC=corp,DC=microsoft,DC=com), translate to DNS (corp.microsoft.com)
var arrParseDN = strForestDN.split(",");
for (var i = 0; i < arrParseDN.length; i++)
{
var arrParseDC = arrParseDN[i].split("=");
if (null == strForestDNS)
{
strForestDNS = arrParseDC[1];
}
else
{
strForestDNS = strForestDNS + "." + arrParseDC[1];
}
}
}
catch (e)
{
; // Nothing to be done
}
return strForestDNS;
}
//-----------------------------------------------------------
// Returns the NetBIOS domain name from the DNS domain name
//-----------------------------------------------------------
function NetBIOSDomainFromDN(strDNDomain)
{
// Constants for the NameTranslate object.
var ADS_NAME_INITTYPE_GC = 3;
var ADS_NAME_TYPE_NT4 = 3;
var ADS_NAME_TYPE_1779 = 1;
var objTrans, strNetBIOSDomain;
// Use the NameTranslate object to find the NetBIOS domain name from the
// DNS domain name.
objTrans = new ActiveXObject("NameTranslate");
objTrans.Init(ADS_NAME_INITTYPE_GC, "");
objTrans.Set(ADS_NAME_TYPE_1779, strDNDomain);
strNetBIOSDomain = objTrans.Get(ADS_NAME_TYPE_NT4);
//-------------------------------------------------------------
// Gets the IP addresses for the given computer name
//-------------------------------------------------------------
function GetIPAddresses(objWMIService)
{
var wbemFlagForwardOnly = 32;
var wbemFlagReturnImmediately = 16;
var strIPs;
var oItem, propValue;
var arrItems;
arrItems = new Enumerator(objWMIService.ExecQuery(
"Select IPAddress from Win32_NetworkAdapterConfiguration",
"WQL",
wbemFlagForwardOnly+wbemFlagReturnImmediately));
for (;!arrItems.atEnd(); arrItems.moveNext())
{
var IPValue = arrItems.item().IPAddress;
if (null != IPValue)
{
var arrIPs = IPValue.toArray();
for (var i =0; i < arrIPs.length; i++)
{
var strIP = arrIPs[i];
if (null != strIP)
{
if (null == strIPs)
{
strIPs = strIP;
}
else
{
var matchIP = strIPs.search(strIP);
if(matchIP == -1)
{
strIPs = strIPs + ", " + strIP;
}
}
}
}
}
}
return strIPs;
}
//-----------------------------------------------------------
// Returns the Computer OU from the given Computer DN
//-----------------------------------------------------------
function GetComputerOUFromDN(strComputerDN)
{
var ADS_SETTYPE_DN = 4;
var ADS_FORMAT_X500_PARENT = 8;
var oPathName = new ActiveXObject("PathName");
oPathName.Set(strComputerDN, ADS_SETTYPE_DN);
return oPathName.Retrieve(ADS_FORMAT_X500_PARENT);
}
//-----------------------------------------------------------
// Get the site name from the Computer DNS
//-----------------------------------------------------------
function GetSiteFromComputerDNS(strComputerDNS)
{
var HKEY_LOCAL_MACHINE = 0x80000002;
var strSiteName = null;
try
{
var objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\" + strComputerDNS + "\\root\\default:StdRegProv");
// Get the WMI method and setup the params
var objMethod = objReg.Methods_.Item("GetStringValue");
var objInParam = objMethod.InParameters.SpawnInstance_();
objInParam.hDefKey = HKEY_LOCAL_MACHINE;
objInParam.sSubKeyName = "System\\CurrentControlSet\\Services\\Netlogon\\Parameters";
objInParam.sValueName = "SiteName";
// Execute the method and retrieve the value
var objOutParam = objReg.ExecMethod_(objMethod.Name, objInParam);
strSiteName = objOutParam.sValue;
//If SiteName override is null or empty, try DynamicSiteName
if(strSiteName == null || strSiteName == "")
{
objInParam.sValueName = "DynamicSiteName";
// Execute the method and retrieve the value
objOutParam = objReg.ExecMethod_(objMethod.Name, objInParam);
strSiteName = objOutParam.sValue;
}
}
catch(e)
{
; // Nothing to be done
}
return strSiteName;
}
//-----------------------------------------------------------
// Get the virtual machine information from the registry
//-----------------------------------------------------------
function GetVirtualInfoFromReg(strComputerDNS)
{
var HKEY_LOCAL_MACHINE = 0x80000002;
try
{
var objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\" + strComputerDNS + "\\root\\default:StdRegProv");
// Get the WMI method and setup the params
var objMethod = objReg.Methods_.Item("GetStringValue");
var objInParam = objMethod.InParameters.SpawnInstance_();
objInParam.hDefKey = HKEY_LOCAL_MACHINE;
objInParam.sSubKeyName = "SOFTWARE\\Microsoft\\Virtual Machine\\Guest\\Parameters";
objInParam.sValueName = "HostName";
// Execute the method and retrieve the value
var objOutParam = objReg.ExecMethod_(objMethod.Name, objInParam);
strHostServerName = objOutParam.sValue;
objInParam.sValueName = "VirtualMachineName";
// Execute the method and retrieve the value
var objOutParam = objReg.ExecMethod_(objMethod.Name, objInParam);
strVirtualMachineName = objOutParam.sValue;
}
catch(e)
{
; // Nothing to be done
}
}
//-----------------------------------------------------------
// Adds the property to the instance if the value is non-null
//-----------------------------------------------------------
function AddClassProperty(oInstance, strProperty, strValue)
{
if (null != strValue) oInstance.AddProperty(strProperty, strValue);
}