Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for sharemg2.vbs

(view source code of sharemg2.vbs as plain text)

  1. ' Use custom error handling
  2. On Error Resume Next
  3.  
  4.  
  5. ' Define constants
  6. Const wbemFlagReturnImmediately = &h10
  7. Const wbemFlagForwardOnly       = &h20
  8. Const ForReading                = 1
  9. Const ForWriting                = 2
  10. Const ForAppending              = 8
  11.  
  12.  
  13. ' Check "named" command line arguments (command line switches)
  14. Select Case WScript.Arguments.Named.Count
  15. 	Case 0
  16. 		' Default: logging enabled
  17. 		boolLog = True
  18. 	Case 1
  19. 		' /N (No logging) is the only valid switch
  20. 		If WScript.Arguments.Named.Exists( "N" ) Then
  21. 			boolLog = False
  22. 		Else
  23. 			Syntax( )
  24. 		End If
  25. 	Case Else
  26. 		' Since /N is the only valid switch, no more than 1 switch is allowed
  27. 		Syntax( )
  28. End Select
  29.  
  30.  
  31. ' Check "unnamed" command line arguments
  32. Select Case WScript.Arguments.Unnamed.Count
  33. 	Case 0
  34. 		' Default if none specified is local computer (".")
  35. 		Set objWMIService = GetObject( "winmgmts://./root/cimv2" )
  36. 		' Retrieve computer name
  37. 		Set colItems = objWMIService.ExecQuery( "Select * from Win32_ComputerSystem", , _
  38. 		               wbemFlagReturnImmediately + wbemFlagForwardOnly )
  39. 		For Each objItem in colItems
  40. 			strComputer = objItem.Name
  41. 		Next
  42. 	Case 1
  43. 		' Command line argument should not contain "?"
  44. 		strComputer = UCase( Wscript.Arguments.Unnamed( 0 ) )
  45. 		If InStr( strComputer, "?" ) > 0 Then
  46. 			Syntax( )
  47. 		End If
  48. 	Case Else
  49. 		' Maximum is 1 "unnamed" command line parameter
  50. 		Syntax( )
  51. End Select
  52.  
  53.  
  54. ' Initialize variables for message and log headers
  55. strBat = "@ECHO OFF"
  56. strLog = "Computer name: " & strComputer & vbCrLf & "Share name:" & vbTab & "Share path:"
  57. strMsg = "Shares on " & strComputer & ":"
  58.  
  59.  
  60. ' Create log and batch file if requested
  61. If boolLog Then
  62. 	CreateNewLogs( )
  63. End If
  64.  
  65.  
  66. ' Connect to the specified (or default) computer
  67. Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/cimv2" )
  68.  
  69.  
  70. ' Enumerate (non-printer) share info
  71. Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_Share WHERE Type = 0", _
  72.                "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly )
  73. For Each objItem In colItems
  74. 	' The /GRANT:... part at the end  is required if the shares are to be
  75. 	' recreated on Windows Server 2003; remove it for Windows 2000 Servers
  76. 	strBat = strBat & "NET SHARE " & Chr(34) & objItem.Name & Chr(34) & "=" _
  77. 	       & Chr(34) & objItem.Path & Chr(34) & " /GRANT:Everyone,Full" & vbCrLf
  78. 	strLog = strLog & objItem.Name & vbTab & objItem.Path & vbCrLf
  79. 	strMsg = strMsg & vbCrLf & objItem.Name & "=" & objItem.Path
  80. Next
  81.  
  82.  
  83. ' Display result
  84. WScript.Echo strMsg
  85.  
  86.  
  87. 'Append the results to the log and batch files unless No Logging was specified (/N switch)
  88. If boolLog Then
  89. 	' Open File System object
  90. 	Set fso = CreateObject( "Scripting.FileSystemObject" )
  91.  
  92. 	' Write result to log file
  93. 	Set filOut = fso.OpenTextFile( strComputer & "_shares.csv", ForAppending, True ) 
  94. 	filOut.Write( strLog )
  95. 	filOut.Close
  96.  
  97. 	' Write result to batch file
  98. 	strBat = strBat & "GOTO:EOF" & vbCrLf
  99. 	Set filOut = fso.OpenTextFile( strComputer & "_recreate_shares.bat", ForAppending, True ) 
  100. 	filOut.Write( strBat )
  101. 	filOut.Close
  102.  
  103. 	' Release File System object
  104. 	set fso = Nothing
  105. End If
  106.  
  107.  
  108. ' End of main program
  109. WScript.Quit( 0 )
  110.  
  111.  
  112. Sub CreateNewLogs( )
  113. 	' Open File System object
  114. 	Set fso = CreateObject( "Scripting.FileSystemObject" )
  115. 	' Display error number and description if applicable
  116. 	If Err Then ShowError( )
  117.  
  118. 	' Create new log file
  119. 	Set filOut = fso.OpenTextFile( strComputer & "_shares.csv", ForWriting, True )
  120. 	If Err Then ShowError( )
  121. 	' Write header
  122. 	filOut.WriteLine( strLog )
  123. 	If Err Then ShowError( )
  124. 	' Close file
  125. 	filOut.Close
  126. 	If Err Then ShowError( )
  127. 	' Reset variable
  128. 	strLog = ""
  129.  
  130. 	' Create new batch file
  131. 	Set filOut = fso.OpenTextFile( strComputer & "_recreate_shares.bat", ForWriting, True )
  132. 	' Write header
  133. 	filOut.WriteLine( strBat )
  134. 	If Err Then ShowError( )
  135. 	' Close file
  136. 	filOut.Close
  137. 	If Err Then ShowError( )
  138. 	' Reset variable
  139. 	strBat = ""
  140.  
  141. 	' Release File System object
  142. 	set fso = Nothing
  143. End Sub
  144.  
  145.  
  146. Sub ShowError( )
  147. 	strMsg = vbCrLf & "Error # " & Err.Number & vbCrLf _
  148. 	       & Err.Description & vbCrLf & vbCrLf
  149. 	Syntax( )
  150. End Sub
  151.  
  152.  
  153. Sub Syntax( )
  154. 	strMsg = strMsg & vbCrLf _
  155. 	       & "ShareMg2.vbs,  Version 2.00" & vbCrLf _
  156. 	       & "Prepare to migrate shares from the specified server to a Windows 2003 Server." _
  157. 	       & vbCrLf & vbCrLf _
  158. 	       & "Usage:  [ CSCRIPT ]  SHAREMG2.VBS  [ servername ]  [ /N ]" _
  159. 	       & vbCrLf & vbCrLf _
  160. 	       & "Where:  " & Chr(34) & "servername" & Chr(34) _
  161. 	       & " is the optional name of the server to be probed" & vbCrLf _
  162. 	       & "                     (default is local computer name)" & vbCrLf _
  163. 	       & "        /N           Do not log results, nor create the batch file" _
  164. 	       & vbCrLf & vbCrLf _
  165. 	       & "ShareMg2.vbs creates a batch file named servername_recreate_shares.bat, and" _
  166. 	       & vbCrLf _
  167. 	       & "a text file listing the original shares. Use servername_recreate_shares.bat" _
  168. 	       & vbCrLf _
  169. 	       & "to recreate the shares on a new file server. Remove the /GRANT part in" _
  170. 	       & vbCrLf _
  171. 	       & "servername_recreate_shares.bat to use it with Windows 2000 servers." _
  172. 	       & vbCrLf & vbCrLf _
  173. 	       & "Written by Rob van der Woude" & vbCrLf _
  174. 	       & "http://www.robvanderwoude.com" & vbCrLf & vbCrLf _
  175. 	       & "Created with Microsoft's Scriptomatic 2.0 tool" & vbCrLf _
  176. 	       & "http://www.microsoft.com/downloads/details.aspx?" & vbCrLf _
  177. 	       & "    FamilyID=09dfc342-648b-4119-b7eb-783b0f7d1178&DisplayLang=en"
  178. 	WScript.Echo strMsg
  179. 	WScript.Quit(1)
  180. End Sub
  181.  

page last uploaded: 2017-04-06, 13:33