PageRenderTime 64ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 1ms

/scripts/Lib_CommonGPMCFunctions.js

https://bitbucket.org/sramana/vidyalaya-active-directory
JavaScript | 498 lines | 338 code | 78 blank | 82 comment | 29 complexity | 30a134bd4c891e19703c697944393ed0 MD5 | raw file
  1. /////////////////////////////////////////////////////////////////
  2. // Copyright (c) Microsoft Corporation. All rights reserved
  3. //
  4. // Title: Lib_CommonGPMCFunctions.js
  5. // Author: mtreit@microsoft.com
  6. // Created: 7/16/2002
  7. // Purpose: Provides a library of common helper functions
  8. // for use when scripting the GPMC interfaces.
  9. //
  10. // This library must be included with the sample
  11. // WSH scripts that ship with the GPMC
  12. /////////////////////////////////////////////////////////////////
  13. ///////////////////////////////////////
  14. // Initialization
  15. ///////////////////////////////////////
  16. // Create global objects for use by all of the functions
  17. var GPM = new ActiveXObject("GPMgmt.GPM");
  18. var Constants = GPM.GetConstants();
  19. ///////////////////////////////////////
  20. // Common Function Library
  21. ///////////////////////////////////////
  22. //
  23. // Note: The functions in this section are shared by
  24. // many of the GPMC sample scripts. This section may be
  25. // pasted directly in each individual script to ensure they
  26. // will work stand-alone, or may be collected in a library
  27. // file and accessed using the 'include' functionality
  28. // provided by the WSF script format.
  29. //
  30. // Takes a GPO name or GUID and returns the GPO
  31. function GetGPO(szGPOName, GPMDomain)
  32. {
  33. var GPO;
  34. // Get the GPO object for the specified GPO
  35. try
  36. {
  37. GPO = GPMDomain.GetGPO(szGPOName);
  38. }
  39. catch (err)
  40. {
  41. // The attempt to get the GPO failed. The user may have
  42. // passed in the name instead of GUID, so fetch by name.
  43. try
  44. {
  45. GPO = GetGPOByName(szGPOName, GPMDomain);
  46. }
  47. catch (err)
  48. {
  49. WScript.Echo("Could not find GPO " + szGPOName);
  50. return false;
  51. }
  52. }
  53. return GPO;
  54. }
  55. // Given a GPO name or ID (GUID), returns that GPO from the directory.
  56. // If no GPO is found, returns null
  57. // If multiple GPOs exist by that name, returns the resulting collection
  58. //
  59. function GetGPOByName(szGPOName, GPMDomain)
  60. {
  61. // Create a search criteria object for the name
  62. var GPMSearchCriteria = GPM.CreateSearchCriteria();
  63. GPMSearchCriteria.Add(Constants.SearchPropertyGPODisplayName, Constants.SearchOpEquals, szGPOName);
  64. // Search for the specified GPO
  65. var GPOList = GPMDomain.SearchGPOs(GPMSearchCriteria);
  66. if (GPOList.Count == 0)
  67. {
  68. return false; // No GPO found
  69. }
  70. // The following could return a collection of multiple GPOs if more than one GPO
  71. // with the same name exists in the domain
  72. //
  73. if (GPOList.Count == 1)
  74. {
  75. return GPOList.Item(1);
  76. }
  77. else
  78. {
  79. return GPOList;
  80. }
  81. }
  82. // Retrieves the WMI filter with the specified name
  83. function GetWMIFilter(szWMIFilterName, GPMDomain)
  84. {
  85. var GPMSearchCriteria = GPM.CreateSearchCriteria();
  86. var FilterList = GPMDomain.SearchWMIFilters();
  87. var e = new Enumerator(FilterList);
  88. var WMIFilter;
  89. for (; !e.atEnd(); e.moveNext())
  90. {
  91. WMIFilter = e.item();
  92. if (WMIFilter.Name.toLowerCase() == szWMIFilterName.toLowerCase())
  93. {
  94. return WMIFilter;
  95. }
  96. }
  97. return false;
  98. }
  99. // Attempts to retrieve a SOM by name or path from the directory. Will return a single GPMSOM object, or
  100. // an array of such objects if more than one with the given name is found.
  101. //
  102. function GetSOM(szSOMName, GPMDomain)
  103. {
  104. // Check if this is the domain level - if so, get the SOM for the domain and return it
  105. if (szSOMName.toLowerCase() == GPMDomain.Domain.toLowerCase())
  106. {
  107. return GPMDomain.GetSOM(""); // Returns the SOM representing the domain
  108. }
  109. // First try to get the SOM, in case a valid LDAP-style path was passed in
  110. try
  111. {
  112. var GPMSOM = GPMDomain.GetSOM(szSOMName);
  113. }
  114. catch (err)
  115. {
  116. try
  117. {
  118. // Might be a site instead of a domain or oU
  119. GPMSOM = GPMSitesContainer.GetSite(szSOMName);
  120. }
  121. catch (err)
  122. {
  123. GPMSOM = false;
  124. }
  125. }
  126. if (GPMSOM)
  127. {
  128. return GPMSOM;
  129. }
  130. // Search for the SOM by name, using ADSI
  131. // Create an array to hold the results, as we may find more than one SOM with the specified name
  132. var aResult = new Array();
  133. // Define ADS related values - see IADS.h
  134. var ADS_SCOPE_BASE = 0;
  135. var ADS_SCOPE_ONELEVEL = 1;
  136. var ADS_SCOPE_SUBTREE = 2;
  137. var ADSIPROP_CHASE_REFERRALS = 0x9;
  138. var ADS_CHASE_REFERRALS_NEVER = 0;
  139. var ADS_CHASE_REFERRALS_SUBORDINATE = 0x20;
  140. var ADS_CHASE_REFERRALS_EXTERNAL = 0x40;
  141. var ADS_CHASE_REFERRALS_ALWAYS = ADS_CHASE_REFERRALS_SUBORDINATE | ADS_CHASE_REFERRALS_EXTERNAL;
  142. var szLDAPSuffix = GPMDomain.GetSOM("").Path;
  143. // Create the ADO objects and open the connection
  144. var ADOConnection = new ActiveXObject("ADODB.Connection");
  145. var ADOCommand = new ActiveXObject("ADODB.Command");
  146. ADOConnection.Provider = "ADsDSOObject";
  147. ADOConnection.Open("Active Directory Provider");
  148. ADOCommand.ActiveConnection = ADOConnection;
  149. // First look for OUs
  150. var szDomainLDAPPath = "LDAP://" + szLDAPSuffix;
  151. var szSQL = "select AdsPath from '" + EscapeString(szDomainLDAPPath) + "'";
  152. szSQL += " where Name='" + szSOMName + "'";
  153. // Execute the search
  154. ADOCommand.CommandText = szSQL;
  155. ADOCommand.Properties("Page Size") = 1000;
  156. ADOCommand.Properties("Timeout") = 500;
  157. ADOCommand.Properties("SearchScope") = ADS_SCOPE_SUBTREE;
  158. ADOCommand.Properties("Cache Results") = false;
  159. ADOCommand.Properties("Chase Referrals") = ADS_CHASE_REFERRALS_EXTERNAL; // Needed when querying a different domain
  160. try
  161. {
  162. var rs = ADOCommand.Execute();
  163. }
  164. catch (err)
  165. {
  166. WScript.Echo("There was an error executing the DS query " + szSQL);
  167. WScript.Echo("The error was:");
  168. WScript.Echo(ErrCode(err.number) + " - " + err.description);
  169. return false;
  170. }
  171. var SOM;
  172. while ( ! rs.eof )
  173. {
  174. SOM = GetObject(rs.Fields(0));
  175. // Ignore objects that are not OUs or the domain level
  176. if (SOM.Class == 'organizationalUnit' || SOM.Class == 'fTDfs')
  177. {
  178. GPMSOM = GPMDomain.GetSOM(SOM.ADsPath)
  179. aResult = aResult.concat(GPMSOM);
  180. }
  181. rs.MoveNext();
  182. }
  183. // Get the LDAP suffix from the forest name
  184. ForestDomain = GPM.GetDomain(szForestName, "", Constants.UseAnyDC);
  185. szLDAPSuffix = ForestDomain.GetSOM("").Path;
  186. var szSitesLDAPPath = "LDAP://CN=Sites,CN=Configuration," + szLDAPSuffix;
  187. var szSQL = "select AdsPath from '" + EscapeString(szSitesLDAPPath) + "'";
  188. szSQL += " where Name='" + szSOMName + "'";
  189. // Execute the search
  190. ADOCommand.CommandText = szSQL;
  191. try
  192. {
  193. var rs = ADOCommand.Execute();
  194. }
  195. catch (err)
  196. {
  197. WScript.Echo("There was an error executing the DS query " + szSQL);
  198. WScript.Echo("The error was:");
  199. WScript.Echo(ErrCode(err.number) + " - " + err.description);
  200. return false;
  201. }
  202. while ( ! rs.eof )
  203. {
  204. SOM = GetObject(rs.Fields(0));
  205. if (SOM.Class == 'site')
  206. {
  207. GPMSOM = GPMSitesContainer.GetSite(SOM.Name)
  208. aResult = aResult.concat(GPMSOM);
  209. }
  210. rs.MoveNext();
  211. }
  212. // Cleanup
  213. ADOConnection.Close();
  214. // Return the result
  215. if (aResult.length == 1)
  216. {
  217. return aResult[0];
  218. }
  219. if (aResult.length == 0)
  220. {
  221. return false;
  222. }
  223. return aResult;
  224. }
  225. // Retrieves a specific backup from the specified location
  226. function GetBackup(szBackupLocation, szBackupID)
  227. {
  228. var GPMBackup;
  229. var GPMBackupDir;
  230. // Get the backup directory specified
  231. try
  232. {
  233. GPMBackupDir = GPM.GetBackupDir(szBackupLocation);
  234. }
  235. catch (err)
  236. {
  237. WScript.Echo("The specified backup folder '" + szBackupLocation + "' could not be accessed.");
  238. return false;
  239. }
  240. // See if we were passed a valid backup ID
  241. try
  242. {
  243. GPMBackup = GPMBackupDir.GetBackup(szBackupID);
  244. }
  245. catch (err)
  246. {
  247. GPMBackup = false;
  248. }
  249. if (!GPMBackup)
  250. {
  251. // Not a valid backup ID, so fetch backup by GPO name
  252. var GPMSearchCriteria = GPM.CreateSearchCriteria();
  253. GPMSearchCriteria.Add(Constants.SearchPropertyBackupMostRecent, Constants.SearchOpEquals, true);
  254. GPMSearchCriteria.Add(Constants.SearchPropertyGPODisplayName, Constants.SearchOpEquals, szBackupID);
  255. var BackupList = GPMBackupDir.SearchBackups(GPMSearchCriteria);
  256. if (BackupList.Count == 0)
  257. {
  258. WScript.Echo("The specified backup '" + szBackupID + "' was not found in folder '" + szBackupLocation);
  259. return false;
  260. }
  261. else
  262. {
  263. GPMBackup = BackupList.Item(1);
  264. }
  265. }
  266. return GPMBackup;
  267. }
  268. // Prints any status messages for a GPO operation, such as backup or import
  269. function PrintStatusMessages(GPMResult)
  270. {
  271. var GPMStatus = GPMResult.Status;
  272. if (GPMStatus.Count == 0)
  273. {
  274. // No messages, so just return
  275. return;
  276. }
  277. WScript.Echo("");
  278. var e = new Enumerator(GPMStatus);
  279. for (; !e.atEnd(); e.moveNext())
  280. {
  281. WScript.Echo(e.item().Message);
  282. }
  283. }
  284. // Returns the DNS domain name for the current user, using ADSI
  285. function GetDNSDomainForCurrentUser()
  286. {
  287. var ADS_NAME_INITTYPE_DOMAIN = 1;
  288. var ADS_NAME_INITTYPE_SERVER = 2;
  289. var ADS_NAME_INITTYPE_GC = 3;
  290. var ADS_NAME_TYPE_1779 = 1; // "CN=Jane Doe,CN=users, DC=Microsoft, DC=com"
  291. var ADS_NAME_TYPE_CANONICAL = 2; // "Microsoft.com/Users/Jane Doe".
  292. var ADS_NAME_TYPE_NT4 = 3; // "Microsoft\JaneDoe"
  293. var ADS_NAME_TYPE_DISPLAY = 4; // "Jane Doe"
  294. var ADS_NAME_TYPE_DOMAIN_SIMPLE = 5; // "JaneDoe@Microsoft.com"
  295. var ADS_NAME_TYPE_ENTERPRISE_SIMPLE = 6; // "JaneDoe@Microsoft.com"
  296. var ADS_NAME_TYPE_GUID = 7; // {95ee9fff-3436-11d1-b2b0-d15ae3ac8436}
  297. var ADS_NAME_TYPE_UNKNOWN = 8; // The system will try to make the best guess
  298. var ADS_NAME_TYPE_USER_PRINCIPAL_NAME = 9; // "JaneDoe@Fabrikam.com"
  299. var ADS_NAME_TYPE_CANONICAL_EX = 10; // "Microsoft.com/Users Jane Doe"
  300. var ADS_NAME_TYPE_SERVICE_PRINCIPAL_NAME = 11; // "www/www.microsoft.com@microsoft.com"
  301. var ADS_NAME_TYPE_SID_OR_SID_HISTORY_NAME = 12; // "O:AOG:DAD:(A;;RPWPCCDCLCSWRCWDWOGA;;;S-1-0-0)"
  302. var objWshNetwork = new ActiveXObject("Wscript.Network");
  303. var objectNameTranslate = new ActiveXObject("NameTranslate");
  304. var arrNamePart;
  305. var strNTPath = "";
  306. var strTranslatedName = "";
  307. var strResult = "";
  308. strUser = objWshNetwork.UserName;
  309. strDomain = objWshNetwork.UserDomain;
  310. strNTPath = strDomain + "\\" + strUser;
  311. objectNameTranslate.Init(ADS_NAME_INITTYPE_DOMAIN, strDomain);
  312. objectNameTranslate.Set(ADS_NAME_TYPE_NT4, strNTPath);
  313. strTranslatedName = objectNameTranslate.Get(ADS_NAME_TYPE_CANONICAL);
  314. arrNamePart = strTranslatedName.split("/");
  315. strResult = arrNamePart[0];
  316. return strResult;
  317. }
  318. // Use ADSI to get the LDAP-style forest name of a given domain
  319. function GetForestLDAPPath(szDomainName)
  320. {
  321. // Get the RootDSE naming context for the specified domain
  322. var RootDSE = GetObject("LDAP://" + szDomainName + "/RootDSE");
  323. // Initialize the property cache
  324. RootDSE.GetInfo();
  325. // Now get the forest name
  326. var szForestName = RootDSE.rootDomainNamingContext;
  327. return szForestName;
  328. }
  329. // Use ADSI to get the forest name of a given domain
  330. function GetForestDNSName(szDomainName)
  331. {
  332. var ADS_NAME_INITTYPE_DOMAIN = 1;
  333. var ADS_NAME_INITTYPE_SERVER = 2;
  334. var ADS_NAME_INITTYPE_GC = 3;
  335. var ADS_NAME_TYPE_1779 = 1; // "CN=Jane Doe,CN=users, DC=Microsoft, DC=com"
  336. var ADS_NAME_TYPE_CANONICAL = 2; // "Microsoft.com/Users/Jane Doe".
  337. var ADS_NAME_TYPE_NT4 = 3; // "Microsoft\JaneDoe"
  338. var ADS_NAME_TYPE_DISPLAY = 4; // "Jane Doe"
  339. var ADS_NAME_TYPE_DOMAIN_SIMPLE = 5; // "JaneDoe@Microsoft.com"
  340. var ADS_NAME_TYPE_ENTERPRISE_SIMPLE = 6; // "JaneDoe@Microsoft.com"
  341. var ADS_NAME_TYPE_GUID = 7; // {95ee9fff-3436-11d1-b2b0-d15ae3ac8436}
  342. var ADS_NAME_TYPE_UNKNOWN = 8; // The system will try to make the best guess
  343. var ADS_NAME_TYPE_USER_PRINCIPAL_NAME = 9; // "JaneDoe@Fabrikam.com"
  344. var ADS_NAME_TYPE_CANONICAL_EX = 10; // "Microsoft.com/Users Jane Doe"
  345. var ADS_NAME_TYPE_SERVICE_PRINCIPAL_NAME = 11; // "www/www.microsoft.com@microsoft.com"
  346. var ADS_NAME_TYPE_SID_OR_SID_HISTORY_NAME = 12; // "O:AOG:DAD:(A;;RPWPCCDCLCSWRCWDWOGA;;;S-1-0-0)"
  347. // Get the RootDSE naming context for the specified domain
  348. var RootDSE = GetObject("LDAP://" + szDomainName + "/RootDSE");
  349. // Initialize the property cache
  350. RootDSE.GetInfo();
  351. // Now get the forest name
  352. var szForestName = RootDSE.rootDomainNamingContext;
  353. // Translate it to DNS style
  354. var objectNameTranslate = new ActiveXObject("NameTranslate");
  355. objectNameTranslate.Init(ADS_NAME_INITTYPE_DOMAIN, szDomainName);
  356. objectNameTranslate.Set(ADS_NAME_TYPE_1779, szForestName);
  357. var szTranslatedName = objectNameTranslate.Get(ADS_NAME_TYPE_CANONICAL);
  358. return szTranslatedName.slice(0,-1);
  359. }
  360. // Escapes certain characters in a string so they will work with SQL statements
  361. function EscapeString(str)
  362. {
  363. var result;
  364. // Handle single quotes
  365. var re = new RegExp(/'/g);
  366. result = str.replace(re, "''");
  367. return result;
  368. }
  369. // Replaces invalid characters in a file name
  370. function GetValidFileName(str)
  371. {
  372. var result = str;
  373. result = result.replace(/\*/g, "");
  374. result = result.replace(/\\/g, "");
  375. result = result.replace(/\//g, "");
  376. result = result.replace(/\|/g, "");
  377. result = result.replace(/>/g, "");
  378. result = result.replace(/</g, "");
  379. result = result.replace(/:/g, "");
  380. result = result.replace(/\"/g, "");
  381. result = result.replace(/\?/g, "");
  382. return result;
  383. }
  384. // Checks if the specified file system path is valid.
  385. // Returns true if the path is found, false otherwise.
  386. //
  387. function ValidatePath(szPath)
  388. {
  389. var fso = new ActiveXObject("Scripting.FileSystemObject");
  390. try
  391. {
  392. var Path = fso.GetFolder(szPath);
  393. }
  394. catch (err)
  395. {
  396. return false;
  397. }
  398. return true;
  399. }
  400. // Returns the hexadecimal string for a number, converting negative decimal
  401. // values to the appropriate winerror style hex values
  402. //
  403. function ErrCode(i)
  404. {
  405. var result;
  406. if (i < 0)
  407. {
  408. // Get the winerror-style representation of the hex value
  409. result = 0xFFFFFFFF + i + 1;
  410. }
  411. else
  412. {
  413. result = i;
  414. }
  415. return "0x" + result.toString(16); // base 16
  416. }