Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
5.3 KiB

  1. ' clonepr.vbt start
  2. ' CloneSecurityPrincipal sample VBScript
  3. '
  4. ' Clones accounts from one domain to another
  5. '
  6. ' Copyright (c) 1999 Microsoft Corporation
  7. // this is a Visual Basic Script "Template", used in conjunction with the
  8. // MS Visual C++ Preprocessor to overcome some of the source management
  9. // limitations of VBScript. Not perfect, but better than a stick in the eye.
  10. //
  11. // use cl /EP foo.vbt > foo.vbs to expand the template
  12. const SCRIPT_FILENAME = "clonepr.vbs"
  13. const SCRIPT_SOURCE_NAME = __FILE__
  14. const SCRIPT_DATE = __DATE__
  15. const SCRIPT_TIME = __TIME__
  16. const ARG_COUNT = 7
  17. // this is all our common code.
  18. #include "clonepr.vbi"
  19. Main
  20. wscript.quit(0)
  21. sub Main
  22. if wscript.arguments.count <> ARG_COUNT then
  23. PrintUsageAndQuit
  24. end if
  25. ' copy the command-line arguments for parsing
  26. dim args()
  27. Redim args(0)
  28. args(0) = ""
  29. dim i
  30. for i = 0 to wscript.arguments.count - 1
  31. Redim Preserve args(i)
  32. args(i) = wscript.arguments.item(i)
  33. next
  34. ' command line parameters
  35. dim srcDC ' source domain controller
  36. dim srcDom ' source domain
  37. dim srcSam ' source principal SAM name
  38. dim dstDC ' destination controller
  39. dim dstDom ' destination domain
  40. dim dstSam ' destination principal SAM name
  41. dim dstCN ' CN=dstSam
  42. dim dstCNnew ' CN=dstSam, escaped
  43. dim dstDNTmp ' destination principal Full Distinguished Name
  44. dim dstDN ' destination principal Full Distinguished Name, escaped
  45. ' parse the saved command-line arguments, extracting the values
  46. srcDC = GetArgValue("srcdc", args)
  47. srcDom = GetArgValue("srcdom", args)
  48. srcSam = GetArgValue("srcsam", args)
  49. dstDC = GetArgValue("dstdc", args)
  50. dstDom = GetArgValue("dstdom", args)
  51. dstSam = GetArgValue("dstsam", args)
  52. dstDNTmp= GetArgValue("dstdn", args)
  53. dstCN = "CN=" & dstSam
  54. dstCNnew= adsPathname.GetEscapedElement(0, dstCN)
  55. If (UCase(dstCN) <> UCase(dstCNnew)) And (UCase(dstCN) = UCase(Left(dstDNTmp, Len(dstCN)))) Then
  56. dstDN = dstCNnew & Mid(dstDNTmp, Len(dstCN) + 1)
  57. Else
  58. dstDN = dstDNTmp
  59. End If
  60. ' ensure the user did not pass any unrecognized command-line arguments
  61. if CheckForBadArgs(args) then
  62. Echo "Unknown command-line arguments specified"
  63. PrintUsageAndQuit
  64. end if
  65. ' establish authenticate connections to the source and destination domain
  66. ' controllers
  67. on error resume next
  68. clonepr.Connect srcDC, srcDom, dstDC, dstDom
  69. if Err.Number then DumpErrAndQuit
  70. Echo "Connected to source and destination domain controllers"
  71. ' bind to the source object
  72. dim srcPath
  73. srcPath = "WinNT://" & srcDom & "/" & srcDC & "/" & srcSam
  74. dim srcObject
  75. set srcObject = GetObject(srcPath)
  76. select case Err.Number
  77. case E_ADS_UNKNOWN_OBJECT
  78. Bail "Source object " & srcSam & " not found. Path used: " & srcPath
  79. case 0
  80. ' do nothing
  81. case else
  82. DumpErrAndQuit
  83. end select
  84. Echo "Bound to source " & srcObject.Class & " " & srcObject.Name
  85. if ShouldCloneObject(srcObject) then
  86. CloneSecurityPrincipal srcObject, srcSam, dstDom, dstDC, dstSam, dstDN
  87. end if
  88. end sub
  89. function ShouldCloneObject(byref srcObject)
  90. on error resume next
  91. sid.SetAs ADS_SID_WINNT_PATH, srcObject.AdsPath & "," & srcObject.Class
  92. if Err.Number then DumpErrAndQuit
  93. dim sidString
  94. sidString = sid.GetAs(ADS_SID_SDDL)
  95. if Err.Number then DumpErrAndQuit
  96. if IsBuiltInSid( sidString ) then
  97. Echo srcObject.Name & " is a builtin Account."
  98. Echo "BuiltIn Users and Groups cannot be cloned"
  99. ShouldCloneObject = False
  100. exit function
  101. end if
  102. ShouldCloneObject = True
  103. end function
  104. sub PrintUsageAndQuit
  105. Echo "Usage: cscript " & SCRIPT_FILENAME & " /srcdc:<dcname> /srcdom:<domain>"
  106. Echo "/srcsam:<name> /dstdc:<dcname> /dstdom:<domain> /dstsam:<name>"
  107. Echo "/dstdn<distinguished name>"
  108. Echo ""
  109. Echo "Parameters:"
  110. Echo " /srcdc - source domain controller NetBIOS computer name (without leading \\)"
  111. Echo ""
  112. Echo " /srcdom - source domain NetBIOS name"
  113. Echo ""
  114. Echo " /srcsam - source principal SAM name"
  115. Echo ""
  116. Echo " /dstdc - destination domain controller NetBIOS computer name (without "
  117. Echo " leading \\)"
  118. Echo " This script must be run on the machine indicated here."
  119. Echo ""
  120. Echo " /dstdom - destination domain DNS name"
  121. Echo ""
  122. Echo " /dstsam - destination principal SAM name"
  123. Echo ""
  124. Echo " /dstdn - destination principal Full Distinguished Name"
  125. Echo ""
  126. Echo "Notes:"
  127. Echo ""
  128. Echo "If the destination principal does not exist, it will be created."
  129. Echo "In that case, the container naming context of the destination Full"
  130. Echo "Distinguished Name (i.e. the parent container) must exist."
  131. Echo ""
  132. Echo "Currently logged-on user must be a member of the Administrators"
  133. Echo "group of both the source and destination domains."
  134. Echo ""
  135. Echo SCRIPT_DATE & " " & SCRIPT_TIME
  136. wscript.quit(0)
  137. end sub
  138. ' clonepr.vbt end