home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / ADSDK.ZIP / Samples / Perl / modify.pl < prev   
Encoding:
Text File  |  1998-10-06  |  8.3 KB  |  371 lines

  1. # This sample demonstrates how to retrieve/modify properties
  2. # for objects in DS
  3.  
  4. use OLE;             # Include OLE and Win32 extensions
  5. use Win32;
  6.  
  7. # Create the Global Providers object
  8. $adsiNameSpaces = CreateObject OLE 'ADsNameSpaces'
  9.                   or warn "Couldn't create new instance of the ADSI Name Spaces Check your registry for ADsNameSpaces key under classes!!";
  10.  
  11. $errNum = Win32::OLELastError;
  12. if ($errNum != 0)
  13. {
  14.    print "OLE Error0: ", Win32::FormatMessage($errNum);
  15.    exit(1);
  16. }
  17. else
  18. {
  19.     print "Successfully created the NameSpaces object\n";
  20. }
  21.  
  22. # Now get the LDAP Provider object 
  23. $ldapNameSpace = $adsiNameSpaces->getobject("", "LDAP:");
  24.  
  25. $errNum = Win32::OLELastError;
  26. if ($errNum != 0)
  27. {
  28.    print "OLE Error1: ", Win32::FormatMessage($errNum);
  29.    exit(1);
  30. }
  31. else
  32. {
  33.     print "Successfully got the LDAP Provider object\n";
  34. }
  35.  
  36. # Let us bind to a DS
  37. $myDSObject = $ldapNameSpace->OpenDSObject("LDAP:/\/\NW01T1/\DC=NW01T1DOM,DC=NTDEV,DC=Microsoft,DC=Com", "cn=administrator,cn=users,dc=nw01t1dom,dc=ntdev,dc=microsoft,dc=com", "", 1);
  38.  
  39. $errNum = Win32::OLELastError;
  40. if ($errNum != 0)
  41. {
  42.    print "OLE Error2: ", Win32::FormatMessage($errNum);
  43.    exit(1);
  44. }
  45. else
  46. {
  47.     print "Successfully bound to an object\n";
  48. }
  49.  
  50. #Create an User object in the DS
  51. $newObj = $myDSObject->create("User", "cn=Anandha");
  52.  
  53. $errNum = Win32::OLELastError;
  54. if ($errNum != 0)
  55. {
  56.    print "OLE Error3: ", Win32::FormatMessage($errNum);
  57.    exit(1);
  58. }
  59. else
  60. {
  61.     print "Successfully called IADsContainer::Create to create an object\n";
  62. }
  63.  
  64. # We have to set the mandatory property samAccountName
  65. $newObj->put("samAccountName", "Anandha");
  66.  
  67. $errNum = Win32::OLELastError;
  68. if ($errNum != 0)
  69. {
  70.    print "OLE Error4: ", Win32::FormatMessage($errNum);
  71. }
  72. else
  73. {
  74.     print "Successfully set SamAccountName property on the created object\n";
  75. }
  76.  
  77. # SetInfo on the object
  78. $newObj->setinfo();
  79.  
  80. $errNum = Win32::OLELastError;
  81. if ($errNum != 0)
  82. {
  83.    print "OLE Error5: ", Win32::FormatMessage($errNum);
  84. }
  85. else
  86. {
  87.     print "Successfully created the object in DS (SetInfo passes)\n";
  88. }
  89.  
  90. ####################################################
  91. # Now let us demonstrate how to get/set properties #
  92. ####################################################
  93.  
  94. # Set a single valued property of syntax String
  95. $newObj->put("singleString", "Ganesan");
  96. $errNum = Win32::OLELastError;
  97. if ($errNum != 0)
  98. {
  99.    print "OLE Error6: ", Win32::FormatMessage($errNum);
  100. }
  101. else
  102. {
  103.     print "Successfully put singleString\n";
  104. }
  105.  
  106. # Get the singleString
  107. $strVal = $newObj->get("singleString");
  108. $errNum = Win32::OLELastError;
  109. if ($errNum != 0)
  110. {
  111.    print "OLE Error7: ", Win32::FormatMessage($errNum);
  112. }
  113. else
  114. {
  115.     print "String value is $strVal\n";
  116. }
  117.  
  118. # Set a single valued property of syntax Integer
  119. $newObj->put("singleInteger", 1357);
  120. $errNum = Win32::OLELastError;
  121. if ($errNum != 0)
  122. {
  123.    print "OLE Error8: ", Win32::FormatMessage($errNum);
  124. }
  125. else
  126. {
  127.     print "Successfully put singleInteger\n";
  128. }
  129.  
  130. # Get the singleInteger
  131. $intVal = $newObj->get("singleInteger");
  132. $errNum = Win32::OLELastError;
  133. if ($errNum != 0)
  134. {
  135.    print "OLE Error9: ", Win32::FormatMessage($errNum);
  136. }
  137. else
  138. {
  139.     print "Integer value is $intVal\n";
  140. }
  141.  
  142. # Set a single valued property of syntax Boolean
  143. $newObj->put("singleBoolean", 1);
  144. $errNum = Win32::OLELastError;
  145. if ($errNum != 0)
  146. {
  147.    print "OLE Error10: ", Win32::FormatMessage($errNum);
  148. }
  149. else
  150. {
  151.     print "Successfully put singleBoolean\n";
  152. }
  153.  
  154. # Get the singleBoolean
  155. $boolVal = $newObj->get("singleBoolean");
  156. $errNum = Win32::OLELastError;
  157. if ($errNum != 0)
  158. {
  159.    print "OLE Error11: ", Win32::FormatMessage($errNum);
  160. }
  161. else
  162. {
  163.     print "Boolean value is $boolVal\n";
  164. }
  165.  
  166. # Set a single valued property of syntax OctetString
  167. $binaryString = 'this is binary string';
  168. $newObj->put("singleOctetString", $binaryString);
  169. $errNum = Win32::OLELastError;
  170. if ($errNum != 0)
  171. {
  172.    print "OLE Error12: ", Win32::FormatMessage($errNum);
  173. }
  174. else
  175. {
  176.     print "Successfully put singleOctetString\n";
  177. }
  178.  
  179. # Get the singleOctetString
  180. $octStrVal = $newObj->get("singleOctetString");
  181. $errNum = Win32::OLELastError;
  182. if ($errNum != 0)
  183. {
  184.    print "OLE Error13: ", Win32::FormatMessage($errNum);
  185. }
  186. else
  187. {
  188.     print "OctetString value0 is $octStrVal \n";
  189. }
  190.  
  191. # Set a single valued property of syntax UTCTime
  192. $dateString = new OLE::Variant(OLE::VT_DATE, 'Feb 26, 1998');
  193. $newObj->put("singleUTCTime", $dateString);
  194. $errNum = Win32::OLELastError;
  195. if ($errNum != 0)
  196. {
  197.    print "OLE Error14: ", Win32::FormatMessage($errNum);
  198. }
  199. else
  200. {
  201.     print "Successfully put singleUTCTime\n";
  202. }
  203.  
  204. # Get the singleUTCTime
  205. $dateStr = $newObj->get("singleUTCTime");
  206. $errNum = Win32::OLELastError;
  207. if ($errNum != 0)
  208. {
  209.    print "OLE Error15: ", Win32::FormatMessage($errNum);
  210. }
  211. else
  212. {
  213.     print "UTCTime value is $dateStr \n";
  214. }
  215.  
  216. # Set a single valued property of syntax LargeInteger
  217. $largeInt = 32352489;
  218. $newObj->put("singleLargeInteger", $largeInt);
  219. $errNum = Win32::OLELastError;
  220. if ($errNum != 0)
  221. {
  222.    print "OLE Error16: ", Win32::FormatMessage($errNum);
  223. }
  224. else
  225. {
  226.     print "Successfully put singleLargeInteger\n";
  227. }
  228.  
  229. # Get the singleLargeInteger
  230. $largeInt1 = $newObj->get("singleLargeInteger");
  231. $errNum = Win32::OLELastError;
  232. if ($errNum != 0)
  233. {
  234.    print "OLE Error17: ", Win32::FormatMessage($errNum);
  235. }
  236. else
  237. {
  238.     print "LargeInteger value is $largeInt1 \n";
  239. }
  240.  
  241. # Set a multi valued property of syntax String
  242. @strArr1 = ("Anandha", "Ganesan", "Kumar");  #Create an array of strings
  243. $refStrArr1 = \@strArr1;                     # Get reference to array
  244.  
  245. $newObj->put("multiString", $refStrArr1);
  246. $errNum = Win32::OLELastError;
  247. if ($errNum != 0)
  248. {
  249.    print "OLE Error18: ", Win32::FormatMessage($errNum);
  250. }
  251. else
  252. {
  253.     print "Successfully put multiString\n";
  254. }
  255.  
  256. # Get the multiString
  257. $strArr2 = $newObj->get("multiString");
  258. $errNum = Win32::OLELastError;
  259. if ($errNum != 0)
  260. {
  261.    print "OLE Error19: ", Win32::FormatMessage($errNum);
  262. }
  263. else
  264. {
  265.     print "String value is : @{ $strArr2 }\n";
  266. }
  267.  
  268. # Set a multi valued property of syntax Integer
  269. @intArr1 = (2343, 834, 234, 32048);  #Create an array of integers
  270. $refIntArr1 = \@intArr1;             # Get reference to array
  271.  
  272. $newObj->put("multiInteger", $refIntArr1);
  273. $errNum = Win32::OLELastError;
  274. if ($errNum != 0)
  275. {
  276.    print "OLE Error20: ", Win32::FormatMessage($errNum);
  277. }
  278. else
  279. {
  280.     print "Successfully put multiInteger\n";
  281. }
  282.  
  283. # Get the multiInteger
  284. $intArr2 = $newObj->get("multiInteger");
  285. $errNum = Win32::OLELastError;
  286. if ($errNum != 0)
  287. {
  288.    print "OLE Error21: ", Win32::FormatMessage($errNum);
  289. }
  290. else
  291. {
  292.     print "Integer value is : @{ $intArr2 }\n";
  293. }
  294.  
  295. # Set a multi valued property of syntax Boolean
  296. @boolArr1 = (1, 0, 0);                #Create an array of integers
  297. $refBoolArr1 = \@boolArr1;             # Get reference to array
  298.  
  299. $newObj->put("multiBoolean", $refBoolArr1);
  300. $errNum = Win32::OLELastError;
  301. if ($errNum != 0)
  302. {
  303.    print "OLE Error22: ", Win32::FormatMessage($errNum);
  304. }
  305. else
  306. {
  307.     print "Successfully put multiBoolean\n";
  308. }
  309.  
  310. # Get the multiBoolean
  311. $boolArr2 = $newObj->get("multiBoolean");
  312. $errNum = Win32::OLELastError;
  313. if ($errNum != 0)
  314. {
  315.    print "OLE Error23: ", Win32::FormatMessage($errNum);
  316. }
  317. else
  318. {
  319.     print "Boolean value is : @{ $boolArr2 }\n";
  320. }
  321.  
  322. # Now delete the User object
  323. $myDSObject->delete("User", "cn=Anandha");
  324.  
  325. $errNum = Win32::OLELastError;
  326. if ($errNum != 0)
  327. {
  328.    print "OLE Error24: ", Win32::FormatMessage($errNum);
  329. }
  330. else
  331. {
  332.     print "Successfully deleted the object\n";
  333. }
  334.  
  335. # Close handles to the OLE objects
  336. DestroyObject OLE $myDSObject;
  337. $errNum = Win32::OLELastError;
  338. if ($errNum != 0)
  339. {
  340.    print "OLE Error25: ", Win32::FormatMessage($errNum);
  341.    exit(1);
  342. }
  343. else
  344. {
  345.     print "Successfully destroyed the object myDSObject\n";
  346. }
  347.  
  348. DestroyObject OLE $ldapNameSpace;
  349. $errNum = Win32::OLELastError;
  350. if ($errNum != 0)
  351. {
  352.    print "OLE Error26: ", Win32::FormatMessage($errNum);
  353.    exit(1);
  354. }
  355. else
  356. {
  357.     print "Successfully destroyed the object ldapNameSpace \n";
  358. }
  359.  
  360. DestroyObject OLE $adsiNameSpaces;
  361. $errNum = Win32::OLELastError;
  362. if ($errNum != 0)
  363. {
  364.    print "OLE Error27: ", Win32::FormatMessage($errNum);
  365.    exit(1);
  366. }
  367. else
  368. {
  369.     print "Successfully destroyed the object adsiNameSpaces \n";
  370. }
  371.