Source code of Windows XP (NT5)
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.

610 lines
21 KiB

  1. '********************************************************************
  2. '*
  3. '* File: SUBNET.VBS
  4. '* Created: July 1998
  5. '* Version: 1.0
  6. '*
  7. '* Main Function: Controls DHCP subnets on a machine.
  8. '* Usage: Subnet.VBS [/LIST /CREATE | /DELETE] /N:subnet [/S:server]
  9. '* [/O:outputfile] [/U:username] [/W:password] [/Q]
  10. '*
  11. '* Copyright (C) 1998 Microsoft Corporation
  12. '*
  13. '********************************************************************
  14. OPTION EXPLICIT
  15. ON ERROR RESUME NEXT
  16. 'Define constants
  17. CONST CONST_ERROR = 0
  18. CONST CONST_SHOW_USAGE = 1
  19. CONST CONST_PROCEED = 2
  20. 'Declare variables
  21. Dim strOutputFile, intOpMode, blnQuiet, i
  22. Dim strServer, strUserName, strPassword
  23. Dim strSubnetCommand, strSubnetName
  24. ReDim strArgumentArray(0)
  25. 'Initialize variables
  26. strArgumentArray(0) = ""
  27. blnQuiet = False
  28. strServer = ""
  29. strUserName = ""
  30. strPassword = ""
  31. strOutputFile = ""
  32. 'Get the command line arguments
  33. For i = 0 to Wscript.arguments.count - 1
  34. ReDim Preserve strArgumentArray(i)
  35. strArgumentArray(i) = Wscript.arguments.Item(i)
  36. Next
  37. 'Parse the command line
  38. intOpMode = intParseCmdLine(strArgumentArray, strSubnetCommand, strSubnetName, strServer, _
  39. strOutputFile, strUserName, strPassword, blnQuiet)
  40. If Err.Number then
  41. Print "Error 0x" & CStr(Hex(Err.Number)) & " occurred in parsing the command line."
  42. Print "Error description: " & Err.Description & "."
  43. Print "Quit now."
  44. WScript.Quit
  45. End If
  46. Select Case intOpMode
  47. Case CONST_SHOW_USAGE
  48. Call ShowUsage()
  49. Case CONST_PROCEED
  50. Print "Working ... "
  51. Call Subnet(strSubnetCommand, strSubnetName, strServer, _
  52. strOutputFile, strUserName, strPassword)
  53. Case CONST_ERROR
  54. 'Do nothing
  55. Case Else 'Default -- should never happen
  56. Print "Error occurred in passing parameters."
  57. End Select
  58. '********************************************************************
  59. '*
  60. '* Function intParseCmdLine()
  61. '* Purpose: Parses the command line.
  62. '* Input: strArgumentArray an array containing input from the command line
  63. '* Output: strSubnetCommand one of /list, /start, /stop
  64. '* subnetname name of the service to be started or stopped
  65. '* strServer a machine name
  66. '* strOutputFile an output file name
  67. '* strUserName name of the current user
  68. '* strPassword password of the current user
  69. '* blnQuiet specifies whether to suppress messages or not
  70. '* intParseCmdLine is set to CONST_SHOW_USAGE if there is an error
  71. '* in input and CONST_PROCEED otherwise.
  72. '*
  73. '********************************************************************
  74. Private Function intParseCmdLine(strArgumentArray, strSubnetCommand, strSubnetName, _
  75. strServer, strOutputFile, strUserName, strPassword, blnQuiet)
  76. ON ERROR RESUME NEXT
  77. Dim strFlag, i, intState
  78. strFlag = strArgumentArray(0)
  79. If strFlag = "" then 'No arguments have been received
  80. Print "Arguments are required."
  81. Print "Please check the input and try again."
  82. intParseCmdLine = CONST_ERROR
  83. Exit Function
  84. End If
  85. If (strFlag="help") OR (strFlag="/h") OR (strFlag="\h") OR (strFlag="-h") _
  86. OR (strFlag = "\?") OR (strFlag = "/?") OR (strFlag = "?") OR (strFlag="h") Then
  87. intParseCmdLine = CONST_SHOW_USAGE
  88. Exit Function
  89. End If
  90. For i = 0 to UBound(strArgumentArray)
  91. strFlag = LCase(Left(strArgumentArray(i), InStr(1, strArgumentArray(i), ":")-1))
  92. If Err.Number Then 'An error occurs if there is no : in the string
  93. Err.Clear
  94. Select Case LCase(strArgumentArray(i))
  95. Case "/q"
  96. blnQuiet = True
  97. Case "/list"
  98. strSubnetCommand = "list"
  99. Case "/create"
  100. strSubnetCommand = "create"
  101. Case "/delete"
  102. strSubnetCommand = "delete"
  103. Case Else
  104. Print strArgumentArray(i) & " is not a valid input."
  105. Print "Please check the input and try again."
  106. intParseCmdLine = CONST_ERROR
  107. Exit Function
  108. End Select
  109. Else
  110. Select Case strFlag
  111. Case "/n"
  112. strSubnetName = Right(strArgumentArray(i), Len(strArgumentArray(i))-3)
  113. Case "/s"
  114. strServer = Right(strArgumentArray(i), Len(strArgumentArray(i))-3)
  115. Case "/u"
  116. strUserName = Right(strArgumentArray(i), Len(strArgumentArray(i))-3)
  117. Case "/w"
  118. strPassword = Right(strArgumentArray(i), Len(strArgumentArray(i))-3)
  119. Case else
  120. Print "Invalid flag " & """" & strFlag & ":""" & "."
  121. Print "Please check the input and try again."
  122. intParseCmdLine = CONST_ERROR
  123. Exit Function
  124. End Select
  125. End If
  126. Next
  127. intParseCmdLine = CONST_PROCEED
  128. If strSubnetName = "" And (strSubnetCommand="start" or strSubnetCommand="stop") Then
  129. Print "Missing service name."
  130. Print "Please enter the name of the service to be started or stopped."
  131. intParseCmdLine = CONST_ERROR
  132. Exit Function
  133. End If
  134. End Function
  135. '********************************************************************
  136. '*
  137. '* Sub ShowUsage()
  138. '* Purpose: Shows the correct usage to the user.
  139. '* Input: None
  140. '* Output: Help messages are displayed on screen.
  141. '*
  142. '********************************************************************
  143. Private Sub ShowUsage()
  144. Wscript.echo ""
  145. Wscript.echo "Controls services on a machine." & vbLF
  146. Wscript.echo "SUBNET.VBS [/LIST | /START | /STOP] /N:subnetname [/S:server]"
  147. Wscript.echo "[/O:outputfile] [/U:username] [/W:password] [/Q] "
  148. Wscript.Echo " /N, /S, /O, /U, /W"
  149. Wscript.Echo " Parameter specifiers."
  150. Wscript.Echo " /LIST List all subnets on a machine."
  151. Wscript.Echo " /CREATE Create a subnet."
  152. Wscript.Echo " /DELETE Delete a subnet."
  153. Wscript.Echo " subnetname Name of the service to be started or stopped."
  154. Wscript.Echo " server A machine name."
  155. Wscript.Echo " outputfile The output file name."
  156. Wscript.Echo " username Username of the current user."
  157. Wscript.Echo " password Password of the current user."
  158. Wscript.Echo " /Q Suppresses all output messages." & vbLF
  159. Wscript.Echo "EXAMPLE:"
  160. Wscript.echo "SUBNET.VBS /S:MyMachine2 /LIST "
  161. Wscript.echo " Lists all DHCP subnets on MyMachine2."
  162. End Sub
  163. '********************************************************************
  164. '*
  165. '* Sub Subnet()
  166. '* Purpose: Controls subnets on a machine.
  167. '* Input: strSubnetCommand one of /list, /start, /stop
  168. '* subnetname name of the Subnet to be created/deleted
  169. '* strServer a machine name
  170. '* strOutputFile an output file name
  171. '* strUserName name of the current user
  172. '* strPassword password of the current user
  173. '* Output: Results are either printed on screen or saved in strOutputFile.
  174. '*
  175. '********************************************************************
  176. Private Sub Subnet(strSubnetCommand, strSubnetName, strServer, _
  177. strOutputFile, strUserName, strPassword)
  178. ON ERROR RESUME NEXT
  179. Dim objFileSystem, objOutputFile, objService, strQuery
  180. If strOutputFile = "" Then
  181. objOutputFile = ""
  182. Else
  183. 'Create a file object.
  184. set objFileSystem = CreateObject("Scripting.FileSystemObject")
  185. If Err.Number then
  186. Print "Error 0x" & CStr(Hex(Err.Number)) & " opening a filesystem object."
  187. Print "Error description: " & Err.Description & "."
  188. Exit Sub
  189. End If
  190. 'Open the file for output
  191. set objOutputFile = objFileSystem.OpenTextFile(strOutputFile, 8, True)
  192. If Err.Number then
  193. Print "Error 0x" & CStr(Hex(Err.Number)) & " opening file " & strOutputFile
  194. Print "Error description: " & Err.Description & "."
  195. Exit Sub
  196. End If
  197. End If
  198. 'Establish a connection with the server.
  199. If blnConnect(objService, strServer, strUserName, strPassword) Then
  200. Exit Sub
  201. End If
  202. 'Now execute the method.
  203. Call ExecuteMethod(objService, objOutputFile, strSubnetCommand, strSubnetName)
  204. If strOutputFile <> "" Then
  205. objOutputFile.Close
  206. If intResult > 0 Then
  207. Wscript.echo "Results are saved in file " & strOutputFile & "."
  208. End If
  209. End If
  210. End Sub
  211. '********************************************************************
  212. '*
  213. '* Function blnConnect()
  214. '* Purpose: Connects to machine strServer.
  215. '* Input: strServer a machine name
  216. '* strUserName name of the current user
  217. '* strPassword password of the current user
  218. '* Output: objService is returned as a service object.
  219. '*
  220. '********************************************************************
  221. Private Function blnConnect(objService, strServer, strUserName, strPassword)
  222. ON ERROR RESUME NEXT
  223. Dim objLocator
  224. blnConnect = False 'There is no error.
  225. ' Create Locator object to connect to remote CIM object manager
  226. Set objLocator = CreateObject("WbemScripting.SWbemLocator")
  227. If Err.Number then
  228. Print "Error 0x" & CStr(Hex(Err.Number)) & " occurred in creating a locator object."
  229. Print "Error description: " & Err.Description & "."
  230. Err.Clear
  231. blnConnect = True 'An error occurred
  232. Exit Function
  233. End If
  234. ' Connect to the namespace which is either local or remote
  235. Set objService = objLocator.ConnectServer (strServer, "ROOT\DHCP", strUserName, strPassword)
  236. If Err.Number then
  237. Print "Error 0x" & CStr(Hex(Err.Number)) & " occurred in connecting to server " & strServer & "."
  238. Print "Error description: " & Err.Description & "."
  239. Err.Clear
  240. blnConnect = True 'An error occurred
  241. Exit Function
  242. Else
  243. ObjService.Security_.impersonationlevel = 3
  244. End If
  245. End Function
  246. '********************************************************************
  247. '*
  248. '* Sub ExecMethod()
  249. '* Purpose: Executes a method.
  250. '* Input: objService a service object
  251. '* objOutputFile an output file object
  252. '* strSubnetCommand one of /list, /start, /stop
  253. '* servicename name of the service to be started or stopped
  254. '* Output: Results are either printed on screen or saved in objOutputFile.
  255. '*
  256. '********************************************************************
  257. Private Sub ExecuteMethod(objService, objOutputFile, strSubnetCommand, strSubnetName)
  258. ON ERROR RESUME NEXT
  259. Dim objEnumerator, objInstance, strMessage, intStatus
  260. ReDim strName(0), strDisplayName(0),strState(0), intOrder(0)
  261. strMessage = ""
  262. strName(0) = ""
  263. strDisplayName(0) = ""
  264. strState(0) = ""
  265. intOrder(0) = 0
  266. Select Case strSubnetCommand
  267. Case "delete"
  268. objService.Delete("DHCP_SUBNET='" & strSubnetName & "'")
  269. If Err.Number Then
  270. Print "Error 0x" & CStr(Hex(Err.Number)) & " occurred in deleting " & _
  271. "subnet " & strSubnetName & "."
  272. Print "Error description: " & Err.Description & "."
  273. Err.Clear
  274. Exit Sub
  275. End If
  276. Case "create"
  277. Set objInstance = objService.Get("DHCP_SUBNET").SpawnInstance_
  278. If Err.Number Then
  279. Print "Error 0x" & CStr(Hex(Err.Number)) & " occurred in getting " & _
  280. "DHCP_Subnet"
  281. Err.Clear
  282. Exit Sub
  283. End If
  284. objInstance.Name = "SteveMenzies"
  285. objInstance.Comment = "Notepad"
  286. objInstance.Address = "200.0.0.0"
  287. objInstance.Mask = "255.255.255.0"
  288. objInstance.Put_
  289. If Err.Number Then
  290. Print "Error 0x" & CStr(Hex(Err.Number)) & " occurred in creating " & _
  291. "DHCP_Subnet"
  292. Err.Clear
  293. Exit Sub
  294. End If
  295. Case "list"
  296. Set objEnumerator = objService.ExecQuery ( _
  297. "Select Name,Address,Comment From DHCP_SUBNET")
  298. If Err.Number Then
  299. Print "Error 0x" & CStr(Hex(Err.Number)) & " occurred during the query."
  300. Print "Error description: " & Err.Description & "."
  301. Err.Clear
  302. Exit Sub
  303. End If
  304. i = 0
  305. For Each objInstance in objEnumerator
  306. If objInstance is nothing Then
  307. Exit Sub
  308. Else
  309. ReDim Preserve strName(i), strAddress(i), strComment(i), intOrder(i)
  310. strName(i) = objInstance.Name
  311. strAddress(i) = objInstance.Address
  312. strComment(i) = objInstance.Comment
  313. intOrder(i) = i
  314. i = i + 1
  315. End If
  316. If Err.Number Then
  317. Err.Clear
  318. End If
  319. Next
  320. If i > 0 Then
  321. 'Display the header
  322. strMessage = Space(2) & strPackString("NAME", 15, 1, 0)
  323. strMessage = strMessage & strPackString("ADDRESS", 15, 1, 0)
  324. strMessage = strMessage & strPackString("COMMENT", 15, 1, 0) & vbLF
  325. WriteLine strMessage, objOutputFile
  326. Call SortArray(strName, True, intOrder)
  327. Call ReArrangeArray(strAddress, intOrder)
  328. Call ReArrangeArray(strComment, intOrder)
  329. For i = 0 To UBound(strName)
  330. strMessage = Space(2) & strPackString(strName(i), 15, 1, 0)
  331. strMessage = strMessage & strPackString(strAddress(i), 15, 1, 0)
  332. strMessage = strMessage & strPackString(strComment(i), 15, 1, 0)
  333. WriteLine strMessage, objOutputFile
  334. Next
  335. Else
  336. Wscript.Echo "No Subnets found!"
  337. End If
  338. End Select
  339. End Sub
  340. '********************************************************************
  341. '*
  342. '* Sub SortArray()
  343. '* Purpose: Sorts an array and arrange another array accordingly.
  344. '* Input: strArray the array to be sorted
  345. '* blnOrder True for ascending or False for descending
  346. '* strArray2 an array that has exactly the same number of elements as strArray
  347. '* and will be reordered together with strArray
  348. '* Output: The sorted arrays are returned in the original arrays.
  349. '* Note: Repeating elements are not deleted.
  350. '*
  351. '********************************************************************
  352. Private Sub SortArray(strArray, blnOrder, strArray2)
  353. ON ERROR RESUME NEXT
  354. Dim i, j, intUbound
  355. If IsArray(strArray) Then
  356. intUbound = UBound(strArray)
  357. Else
  358. Print "Argument is not an array!"
  359. Exit Sub
  360. End If
  361. blnOrder = CBool(blnOrder)
  362. If Err.Number Then
  363. Print "Argument is not a boolean!"
  364. Exit Sub
  365. End If
  366. i = 0
  367. Do Until i > intUbound-1
  368. j = i + 1
  369. Do Until j > intUbound
  370. If (strArray(i) > strArray(j)) and blnOrder Then
  371. Swap strArray(i), strArray(j) 'swaps element i and j
  372. Swap strArray2(i), strArray2(j)
  373. ElseIf (strArray(i) < strArray(j)) and Not blnOrder Then
  374. Swap strArray(i), strArray(j) 'swaps element i and j
  375. Swap strArray2(i), strArray2(j)
  376. ElseIf strArray(i) = strArray(j) Then
  377. 'Move element j to next to i
  378. If j > i + 1 Then
  379. Swap strArray(i+1), strArray(j)
  380. Swap strArray2(i+1), strArray2(j)
  381. End If
  382. End If
  383. j = j + 1
  384. Loop
  385. i = i + 1
  386. Loop
  387. End Sub
  388. '********************************************************************
  389. '*
  390. '* Sub Swap()
  391. '* Purpose: Exchanges values of two strings.
  392. '* Input: strA a string
  393. '* strB another string
  394. '* Output: Values of strA and strB are exchanged.
  395. '*
  396. '********************************************************************
  397. Private Sub Swap(ByRef strA, ByRef strB)
  398. Dim strTemp
  399. strTemp = strA
  400. strA = strB
  401. strB = strTemp
  402. End Sub
  403. '********************************************************************
  404. '*
  405. '* Sub ReArrangeArray()
  406. '* Purpose: Rearranges one array according to order specified in another array.
  407. '* Input: strArray the array to be rearranged
  408. '* intOrder an integer array that specifies the order
  409. '* Output: strArray is returned as rearranged
  410. '*
  411. '********************************************************************
  412. Private Sub ReArrangeArray(strArray, intOrder)
  413. ON ERROR RESUME NEXT
  414. Dim intUBound, i, strTempArray()
  415. If Not (IsArray(strArray) and IsArray(intOrder)) Then
  416. Print "At least one of the arguments is not an array"
  417. Exit Sub
  418. End If
  419. intUBound = UBound(strArray)
  420. If intUBound <> UBound(intOrder) Then
  421. Print "The upper bound of these two arrays do not match!"
  422. Exit Sub
  423. End If
  424. ReDim strTempArray(intUBound)
  425. For i = 0 To intUBound
  426. strTempArray(i) = strArray(intOrder(i))
  427. If Err.Number Then
  428. Print "Error 0x" & CStr(Hex(Err.Number)) & " occurred in rearranging an array."
  429. Print "Error description: " & Err.Description & "."
  430. Err.Clear
  431. Exit Sub
  432. End If
  433. Next
  434. For i = 0 To intUBound
  435. strArray(i) = strTempArray(i)
  436. Next
  437. End Sub
  438. '********************************************************************
  439. '*
  440. '* Function strPackString()
  441. '* Purpose: Attaches spaces to a string to increase the length to intLength.
  442. '* Input: strString a string
  443. '* intLength the intended length of the string
  444. '* blnAfter specifies whether to add spaces after or before the string
  445. '* blnTruncate specifies whether to truncate the string or not if
  446. '* the string length is longer than intLength
  447. '* Output: strPackString is returned as the packed string.
  448. '*
  449. '********************************************************************
  450. Private Function strPackString(strString, ByVal intLength, blnAfter, blnTruncate)
  451. ON ERROR RESUME NEXT
  452. intLength = CInt(intLength)
  453. blnAfter = CBool(blnAfter)
  454. blnTruncate = CBool(blnTruncate)
  455. If Err.Number Then
  456. Print "Argument type is incorrect!"
  457. Err.Clear
  458. Wscript.Quit
  459. End If
  460. If intLength > Len(strString) Then
  461. If blnAfter Then
  462. strPackString = strString & Space(intLength-Len(strString))
  463. Else
  464. strPackString = Space(intLength-Len(strString)) & strString & " "
  465. End If
  466. Else
  467. If blnTruncate Then
  468. strPackString = Left(strString, intLength-1) & " "
  469. Else
  470. strPackString = strString & " "
  471. End If
  472. End If
  473. End Function
  474. '********************************************************************
  475. '*
  476. '* Sub WriteLine()
  477. '* Purpose: Writes a text line either to a file or on screen.
  478. '* Input: strMessage the string to print
  479. '* objFile an output file object
  480. '* Output: strMessage is either displayed on screen or written to a file.
  481. '*
  482. '********************************************************************
  483. Sub WriteLine(ByRef strMessage, ByRef objFile)
  484. If IsObject(objFile) then 'objFile should be a file object
  485. objFile.WriteLine strMessage
  486. Else
  487. Wscript.Echo strMessage
  488. End If
  489. End Sub
  490. '********************************************************************
  491. '*
  492. '* Sub Print()
  493. '* Purpose: Prints a message on screen if blnQuiet = False.
  494. '* Input: strMessage the string to print
  495. '* Output: strMessage is printed on screen if blnQuiet = False.
  496. '*
  497. '********************************************************************
  498. Sub Print(ByRef strMessage)
  499. If Not blnQuiet then
  500. Wscript.Echo strMessage
  501. End If
  502. End Sub
  503. '********************************************************************
  504. '* *
  505. '* End of File *
  506. '* *
  507. '********************************************************************
  508. '********************************************************************
  509. '*
  510. '* Procedures calling sequence: SERVICE.VBS
  511. '*
  512. '* intParseCmdLine
  513. '* ShowUsage
  514. '* ListJobs
  515. '* blnConnect
  516. '* ExecuteQuery
  517. '* strPackString
  518. '* SortArray
  519. '* Swap
  520. '* WriteLine
  521. '*
  522. '********************************************************************