Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for Natalie Green's cleanpcwmi.vbs

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

  1. ' Runs cleanup script against remote host and profile (UserID).
  2.  
  3. ' Uses WMI on remote host, and a UNC network share (MyShare).
  4. ' Logs output and uploads log to network share
  5. ' Removes script from remote host when done
  6. ' Waits until each process is done before proceceeding to next
  7. ' Cleans UserID temp files, along with Oracle JRE, Adobe Flash, Chrome, FireFox, and IE caches
  8. ' System, Network, and Local profiles
  9. ' Cleans RecycleBin, SystemInformationVolume, system temp, and VolumeSystemCopy
  10. ' Verifies password is not blank
  11. ' Runs on systems from Windows 2000 to Win7 (and possibly higher)
  12.  
  13. ' To Do:
  14. '	- Make bulk version
  15.  
  16. On Error Resume Next
  17.  
  18.  
  19.  
  20. ' ***********
  21. ' SECTION 01
  22. ' Get argument(s), system values, and prepare registry and folder path variable(s)
  23. ' ***********
  24.  
  25.  
  26. ' Set folder and file variables
  27. ' Note: do NOT use DFS for the folder - it will not work. Using UNC here.
  28. ' Can use html, FTP, etc. with minor updates
  29. dim localFolder, srcPath, MyShare, tgtFolder
  30. dim strComputer, strUserID, strPassword
  31.  
  32. ' BEGIN	***User-Changeable Values***	BEGIN
  33. localFolder = "c:\LOGS\"
  34. srcPath = "c:\AdminScripts\"
  35. MyShare="\\SERVER1\HOMEDRIVE\AdminUserID"
  36. tgtFolder=MyShare & "\scripts\"
  37. ' END	***User-Changeable Values***	END
  38.  
  39. strRemoteLog= localFolder & "LocalLog.txt"
  40. strLocalRun="cleanpclocal.bat"
  41.  
  42. ' Set remote hostname and UserID
  43. Set objArgs = WScript.arguments
  44. If objArgs.Count <> 2 Then
  45. WScript.Echo "Incorrect arguments submitted."
  46. WScript.Echo "Syntax: [scriptname] hostname UserID"
  47. wscript.Echo
  48. WScript.Quit
  49. Else
  50. strComputer = objArgs.item(0)
  51. strUserID = objArgs.item(1)
  52. End If
  53.  
  54. ' Prompt for masked password in IE with ActiveX object using Function GetPass
  55. 'strPassword=""
  56. 'strPassword=GetPass
  57.  
  58. ' Prompt for masked command line passwords
  59. ' http://gallery.technet.microsoft.com/scriptcenter/25ba8659-f76e-4654-b3e0-b1ee1efe20f7
  60. Set objPassword = CreateObject("ScriptPW.Password")
  61. WScript.StdOut.Write "Please enter your password:"
  62. strPassword = objPassword.GetPassword()
  63. WScript.Echo
  64. If strPassword="" Then
  65. 	WScript.Echo "Blank Password is not valid"
  66. 	WScript.Quit
  67. End If
  68.  
  69.  
  70. ' Copy strLocalRun to MyShare
  71. Const OverwriteExisting = True
  72. Set objFSO = CreateObject("Scripting.FileSystemObject")
  73. objFSO.CopyFile srcPath & strLocalRun , tgtFolder , OverwriteExisting
  74.  
  75.  
  76.  
  77. ' Enumerate cimv2 on remote host strComputer
  78. Set objWMIService = GetObject("winmgmts:" & _
  79. "{impersonationLevel=Impersonate}!//" & strComputer & "\root\cimv2")
  80.  
  81. ' Verify can connect and authenticate to remote host
  82. If( IsEmpty( objWMIService ) = True ) Then
  83.     WScript.Echo( "OBJECT_NOT_INITIALIZED :: " & strComputer )
  84.     WScript.Quit( OBJECT_NOT_INITIALIZED )
  85. End If
  86.  
  87.  
  88.  
  89.  
  90. ' ***********
  91. ' SECTION 02
  92. ' Perform corrective actions: stop smc, del HWID file, del reg values, restart SMC
  93. ' Sleep commands allow smc to stop, and for high-latent networks/hosts
  94. ' ***********
  95.  
  96. ' Make local localFolder
  97. strCommand = "cmd /c MD " & localFolder
  98. Call CreateProcess
  99.  
  100. ' Change directory to localFolder
  101. strCommand="cmd /c cd /D " & localFolder & ">" & strRemoteLog
  102. Call CreateProcess
  103.  
  104. ' Build and run command to map network share to Z:\ on strComputer
  105. strCommand = "cmd /c net use z: " _
  106. 	& MyShare & " /user:%USERDOMAIN%\%USERNAME% " & strPassword & ">>" & strRemoteLog
  107. Call CreateProcess
  108.  
  109. ' Query hard drive space HERE if desired, though script below clears some space..
  110. ' If not enough space then log it, upload log, then jump to cleanup
  111.  
  112. ' Copy strLocalRun from MyShare to HOST2\localFolder if source is newer
  113. ' since psexec cannot run scripts located on shared drives
  114. strCommand = "cmd /c @ECHO f | XCOPY Z:\scripts\" & strLocalRun & " " _
  115. 	& localFolder & " /D /Y /V /C /H /R /Z>>" & strRemoteLog
  116. Call CreateProcess
  117.  
  118. ' Start PSEXEC against exe or script
  119. strCommand="cmd /c Z:\psexec -s \\%computername% /accepteula " _
  120. 	& localFolder & strLocalRun & " " & strUserID & ">>" & strRemoteLog
  121. Call CreateProcess
  122.  
  123. ' Rename logfile to include hostname, upload to share, unmap networked drive, and delete script
  124. strCommand="cmd /c REN " & strRemoteLog & " cleanpclog-%COMPUTERNAME%.txt"
  125. Call CreateProcess
  126. strCommand="cmd /c MOVE /Y " & localFolder & "cleanpclog*.txt Z:\scripts\LOGS\"
  127. Call CreateProcess
  128. strCommand="cmd /c net use * /del /Y"
  129. Call CreateProcess
  130. strCommand="cmd /c del " & localFolder & "cleanpc* /q"
  131. Call CreateProcess
  132.  
  133. WScript.Quit
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140. ' ***********
  141. ' APPENDIX
  142. ' Subroutines, functions
  143. ' ***********
  144.  
  145. ' Error return codes for Create method of the Win32_Process Class
  146. ' http://msdn.microsoft.com/en-us/library/windows/desktop/aa389388(v=vs.85).aspx
  147. ' 0=Successful Completion
  148. ' 2=Access Denied
  149. ' 3=Insufficient Privilege
  150. ' 8=Unknown failure
  151. ' 9=Path Not Found
  152. ' 21=Invalid Parameter
  153.  
  154. ' **SUBROUTINES**
  155.  
  156. ' Spawn process strCommand, handle errReturn, wait until done.
  157. Sub CreateProcess
  158.  
  159. ' Spawn process
  160. Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")
  161. ' CAUTION: un-commenting the next line (wscript.echo) will display passwords in CLEAR TEXT on your screen
  162. 'WScript.Echo "strCommand=" & strCommand
  163. errReturn= objWMIService.Create(strCommand, null, null, intProcessID)
  164.  
  165. ' If process creation errored, then quit. Else echo ProcessID and continue subroutine below
  166. If errReturn = 0 Then
  167. 	Wscript.Echo "Process was started with process ID: " & intProcessID
  168. 	WScript.Sleep 200
  169. Else
  170.     Wscript.Echo "Process could not be started due to error: " & errReturn
  171.     WScript.Quit(1)
  172. End If
  173.  
  174. ' Monitor process using function FindNoPrograms. Breaks out of 'while' after ProcessID is reported done (i.e., >0)
  175. While FindNoPrograms(strComputer,intProcessID ) >0
  176.  wscript.sleep 200
  177. Wend
  178.  
  179. WScript.Echo "process ended:" & intProcessID
  180.  
  181.  
  182. WScript.Echo
  183. End Sub
  184.  
  185.  
  186.  
  187. ' **FUNCTIONS**
  188.  
  189. ' Function to monitor process
  190. Function FindNoPrograms(strComputer,intProcessID)
  191. 		Dim CNT, objWMIService,colProcesses
  192.  
  193. 		Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
  194.  
  195. 		Set colProcesses = objWMIService.ExecQuery _
  196. 		    ("Select * from Win32_Process Where ProcessID = " & Chr(39) & intProcessID & Chr(39)  )
  197.  
  198. 			For Each objProcess in colProcesses
  199. 		    	CNT = CNT + 1
  200. 		    Next
  201.  
  202. 		    FindNoPrograms = CNT
  203.  
  204.  
  205. 		Set objWMIService = Nothing
  206. 		Set colProcesses = Nothing
  207.  
  208. End Function
  209.  
  210.  
  211.  
  212.  
  213. ' Subroutine to get masked password
  214. Function GetPass
  215. ' Mask Passwords Using Internet Explorer
  216. ' http://blogs.technet.com/b/heyscriptingguy/archive/2005/02/04/how-can-i-mask-passwords-using-an-inputbox.aspx
  217.  
  218. Set objExplorer = WScript.CreateObject _
  219.     ("InternetExplorer.Application", "IE_")
  220.  
  221. objExplorer.Navigate "file:///C:\FIXDPATH\CMDTOOLS\passmask.htm"   
  222. objExplorer.ToolBar = 0
  223. objExplorer.StatusBar = 0
  224. objExplorer.Width = 400
  225. objExplorer.Height = 350 
  226. objExplorer.Left = 300
  227. objExplorer.Top = 200
  228. objExplorer.Visible = 1             
  229.  
  230. Do While (objExplorer.Document.Body.All.OKClicked.Value = "")
  231.     Wscript.Sleep 250                 
  232. Loop 
  233.  
  234. strPassword = objExplorer.Document.Body.All.UserPassword.Value
  235. strButton = objExplorer.Document.Body.All.OKClicked.Value
  236. objExplorer.Quit
  237. Wscript.Sleep 250
  238.  
  239. If strButton = "Cancelled" Then
  240.     Wscript.Quit
  241. 'Else
  242. '    Wscript.Echo strPassword
  243. End If
  244.  
  245. ' Return the password
  246. GetPass = strPassword
  247.  
  248. End Function
  249.  

page last uploaded: 2016-12-15, 11:20