(view source code of ng_cleanpcwmi.vbs as plain text)
' Runs cleanup script against remote host and profile (UserID).' Uses WMI on remote host, and a UNC network share (MyShare).' Logs output and uploads log to network share' Removes script from remote host when done' Waits until each process is done before proceceeding to next' Cleans UserID temp files, along with Oracle JRE, Adobe Flash, Chrome, FireFox, and IE caches' System, Network, and Local profiles' Cleans RecycleBin, SystemInformationVolume, system temp, and VolumeSystemCopy' Verifies password is not blank' Runs on systems from Windows 2000 to Win7 (and possibly higher)' To Do:' - Make bulk versionOn Error Resume Next
' ***********' SECTION 01' Get argument(s), system values, and prepare registry and folder path variable(s)' ***********' Set folder and file variables' Note: do NOT use DFS for the folder - it will not work. Using UNC here.' Can use html, FTP, etc. with minor updatesdim localFolder, srcPath, MyShare, tgtFolderdim strComputer, strUserID, strPassword' BEGIN ***User-Changeable Values*** BEGINlocalFolder = "c:\LOGS\"
srcPath = "c:\AdminScripts\"
MyShare="\\SERVER1\HOMEDRIVE\AdminUserID"
tgtFolder=MyShare & "\scripts\"
' END ***User-Changeable Values*** ENDstrRemoteLog= localFolder & "LocalLog.txt"
strLocalRun="cleanpclocal.bat"
' Set remote hostname and UserIDSet objArgs = WScript.arguments
If objArgs.Count <> 2 Then
WScript.Echo "Incorrect arguments submitted."
WScript.Echo "Syntax: [scriptname] hostname UserID"
wscript.Echo
WScript.Quit
ElsestrComputer = objArgs.item(0)
strUserID = objArgs.item(1)
End If
' Prompt for masked password in IE with ActiveX object using Function GetPass'strPassword=""'strPassword=GetPass' Prompt for masked command line passwords' http://gallery.technet.microsoft.com/scriptcenter/25ba8659-f76e-4654-b3e0-b1ee1efe20f7Set objPassword = CreateObject("ScriptPW.Password")
WScript.StdOut.Write "Please enter your password:"
strPassword = objPassword.GetPassword()
WScript.Echo
If strPassword="" Then
WScript.Echo "Blank Password is not valid"
WScript.Quit
End If
' Copy strLocalRun to MyShareConst OverwriteExisting = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile srcPath & strLocalRun , tgtFolder , OverwriteExisting
' Enumerate cimv2 on remote host strComputerSet objWMIService = GetObject("winmgmts:" & _
"{impersonationLevel=Impersonate}!//" & strComputer & "\root\cimv2")
' Verify can connect and authenticate to remote hostIf( IsEmpty( objWMIService ) = True ) Then
WScript.Echo( "OBJECT_NOT_INITIALIZED :: " & strComputer )
WScript.Quit( OBJECT_NOT_INITIALIZED )
End If
' ***********' SECTION 02' Perform corrective actions: stop smc, del HWID file, del reg values, restart SMC' Sleep commands allow smc to stop, and for high-latent networks/hosts' ***********' Make local localFolderstrCommand = "cmd /c MD " & localFolder
Call CreateProcess' Change directory to localFolderstrCommand="cmd /c cd /D " & localFolder & ">" & strRemoteLog
Call CreateProcess' Build and run command to map network share to Z:\ on strComputerstrCommand = "cmd /c net use z: " _
& MyShare & " /user:%USERDOMAIN%\%USERNAME% " & strPassword & ">>" & strRemoteLog
Call CreateProcess' Query hard drive space HERE if desired, though script below clears some space..' If not enough space then log it, upload log, then jump to cleanup' Copy strLocalRun from MyShare to HOST2\localFolder if source is newer' since psexec cannot run scripts located on shared drivesstrCommand = "cmd /c @ECHO f | XCOPY Z:\scripts\" & strLocalRun & " " _
& localFolder & " /D /Y /V /C /H /R /Z>>" & strRemoteLog
Call CreateProcess' Start PSEXEC against exe or scriptstrCommand="cmd /c Z:\psexec -s \\%computername% /accepteula " _
& localFolder & strLocalRun & " " & strUserID & ">>" & strRemoteLog
Call CreateProcess' Rename logfile to include hostname, upload to share, unmap networked drive, and delete scriptstrCommand="cmd /c REN " & strRemoteLog & " cleanpclog-%COMPUTERNAME%.txt"
Call CreateProcessstrCommand="cmd /c MOVE /Y " & localFolder & "cleanpclog*.txt Z:\scripts\LOGS\"
Call CreateProcessstrCommand="cmd /c net use * /del /Y"
Call CreateProcessstrCommand="cmd /c del " & localFolder & "cleanpc* /q"
Call CreateProcessWScript.Quit
' ***********' APPENDIX' Subroutines, functions' ***********' Error return codes for Create method of the Win32_Process Class' http://msdn.microsoft.com/en-us/library/windows/desktop/aa389388(v=vs.85).aspx' 0=Successful Completion' 2=Access Denied' 3=Insufficient Privilege' 8=Unknown failure' 9=Path Not Found' 21=Invalid Parameter' **SUBROUTINES**' Spawn process strCommand, handle errReturn, wait until done.Sub CreateProcess' Spawn processSet objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")
' CAUTION: un-commenting the next line (wscript.echo) will display passwords in CLEAR TEXT on your screen'WScript.Echo "strCommand=" & strCommanderrReturn= objWMIService.Create(strCommand, null, null, intProcessID)
' If process creation errored, then quit. Else echo ProcessID and continue subroutine belowIf errReturn = 0 Then
Wscript.Echo "Process was started with process ID: " & intProcessID
WScript.Sleep 200
ElseWscript.Echo "Process could not be started due to error: " & errReturn
WScript.Quit(1)
End If
' Monitor process using function FindNoPrograms. Breaks out of 'while' after ProcessID is reported done (i.e., >0)While FindNoPrograms(strComputer,intProcessID ) >0
wscript.sleep 200
WendWScript.Echo "process ended:" & intProcessID
WScript.Echo
End Sub
' **FUNCTIONS**' Function to monitor processFunction FindNoPrograms(strComputer,intProcessID)
Dim CNT, objWMIService,colProcessesSet objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery _
("Select * from Win32_Process Where ProcessID = " & Chr(39) & intProcessID & Chr(39) )
For Each objProcess in colProcesses
CNT = CNT + 1
Next FindNoPrograms = CNTSet objWMIService = Nothing
Set colProcesses = Nothing
End Function
' Subroutine to get masked passwordFunction GetPass' Mask Passwords Using Internet Explorer' http://blogs.technet.com/b/heyscriptingguy/archive/2005/02/04/how-can-i-mask-passwords-using-an-inputbox.aspxSet objExplorer = WScript.CreateObject _
("InternetExplorer.Application", "IE_")
objExplorer.Navigate "file:///C:\FIXDPATH\CMDTOOLS\passmask.htm"
objExplorer.ToolBar = 0
objExplorer.StatusBar = 0
objExplorer.Width = 400
objExplorer.Height = 350
objExplorer.Left = 300
objExplorer.Top = 200
objExplorer.Visible = 1
Do While (objExplorer.Document.Body.All.OKClicked.Value = "")
Wscript.Sleep 250
Loop strPassword = objExplorer.Document.Body.All.UserPassword.Value
strButton = objExplorer.Document.Body.All.OKClicked.Value
objExplorer.Quit
Wscript.Sleep 250
If strButton = "Cancelled" Then
Wscript.Quit
'Else' Wscript.Echo strPasswordEnd If
' Return the passwordGetPass = strPasswordEnd Function
page last modified: 2025-10-11; loaded in 0.0108 seconds