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

  1. # This sample demonstrates how to create/delete objects in DS
  2.  
  3. use OLE;             # Include OLE and Win32 extensions
  4. use Win32;
  5.  
  6. # Create the Global Providers object
  7. $adsiNameSpaces = CreateObject OLE 'ADsNameSpaces'
  8.                   or warn "Couldn't create new instance of the ADSI Name Spaces Check your registry for ADsNameSpaces key under classes!!";
  9.  
  10. $errNum = Win32::OLELastError;
  11. if ($errNum != 0)
  12. {
  13.    print "OLE Error0: ", Win32::FormatMessage($errNum);
  14.    exit(1);
  15. }
  16. else
  17. {
  18.     print "Successfully created the NameSpaces object\n";
  19. }
  20.  
  21. # Now get the LDAP Provider object 
  22. $ldapNameSpace = $adsiNameSpaces->getobject("", "LDAP:");
  23.  
  24. $errNum = Win32::OLELastError;
  25. if ($errNum != 0)
  26. {
  27.    print "OLE Error1: ", Win32::FormatMessage($errNum);
  28.    exit(1);
  29. }
  30. else
  31. {
  32.     print "Successfully got the LDAP Provider object\n";
  33. }
  34.  
  35. # Let us bind to a DS
  36. $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);
  37.  
  38. $errNum = Win32::OLELastError;
  39. if ($errNum != 0)
  40. {
  41.    print "OLE Error2: ", Win32::FormatMessage($errNum);
  42.    exit(1);
  43. }
  44. else
  45. {
  46.     print "Successfully bound to an object\n";
  47. }
  48. ###################################
  49. # Create an User object in the DS #
  50. ###################################
  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. # Now delete the User object
  91. $myDSObject->delete("User", "cn=Anandha");
  92.  
  93. $errNum = Win32::OLELastError;
  94. if ($errNum != 0)
  95. {
  96.    print "OLE Error6: ", Win32::FormatMessage($errNum);
  97. }
  98. else
  99. {
  100.     print "Successfully deleted the object\n";
  101. }
  102.  
  103. # Close handles to the OLE objects
  104. DestroyObject OLE $myDSObject;
  105. $errNum = Win32::OLELastError;
  106. if ($errNum != 0)
  107. {
  108.    print "OLE Error7: ", Win32::FormatMessage($errNum);
  109.    exit(1);
  110. }
  111. else
  112. {
  113.     print "Successfully destroyed the object myDSObject\n";
  114. }
  115.  
  116. DestroyObject OLE $ldapNameSpace;
  117. $errNum = Win32::OLELastError;
  118. if ($errNum != 0)
  119. {
  120.    print "OLE Error8: ", Win32::FormatMessage($errNum);
  121.    exit(1);
  122. }
  123. else
  124. {
  125.     print "Successfully destroyed the object ldapNameSpace \n";
  126. }
  127.  
  128. DestroyObject OLE $adsiNameSpaces;
  129. $errNum = Win32::OLELastError;
  130. if ($errNum != 0)
  131. {
  132.    print "OLE Error9: ", Win32::FormatMessage($errNum);
  133.    exit(1);
  134. }
  135. else
  136. {
  137.     print "Successfully destroyed the object adsiNameSpaces \n";
  138. }
  139.