(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 version
On 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 updates
dim localFolder, srcPath, MyShare, tgtFolder
dim strComputer, strUserID, strPassword
' BEGIN ***User-Changeable Values*** BEGIN
localFolder = "c:\LOGS\"
srcPath = "c:\AdminScripts\"
MyShare="\\SERVER1\HOMEDRIVE\AdminUserID"
tgtFolder=MyShare & "\scripts\"
' END ***User-Changeable Values*** END
strRemoteLog= localFolder & "LocalLog.txt"
strLocalRun="cleanpclocal.bat"
' Set remote hostname and UserID
Set objArgs = WScript.arguments
If objArgs.Count <> 2 Then
WScript.Echo "Incorrect arguments submitted."
WScript.Echo "Syntax: [scriptname] hostname UserID"
wscript.Echo
WScript.Quit
Else
strComputer = 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-b1ee1efe20f7
Set 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 MyShare
Const OverwriteExisting = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile srcPath & strLocalRun , tgtFolder , OverwriteExisting
' Enumerate cimv2 on remote host strComputer
Set objWMIService = GetObject("winmgmts:" & _
"{impersonationLevel=Impersonate}!//" & strComputer & "\root\cimv2")
' Verify can connect and authenticate to remote host
If( 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 localFolder
strCommand = "cmd /c MD " & localFolder
Call CreateProcess
' Change directory to localFolder
strCommand="cmd /c cd /D " & localFolder & ">" & strRemoteLog
Call CreateProcess
' Build and run command to map network share to Z:\ on strComputer
strCommand = "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 drives
strCommand = "cmd /c @ECHO f | XCOPY Z:\scripts\" & strLocalRun & " " _
& localFolder & " /D /Y /V /C /H /R /Z>>" & strRemoteLog
Call CreateProcess
' Start PSEXEC against exe or script
strCommand="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 script
strCommand="cmd /c REN " & strRemoteLog & " cleanpclog-%COMPUTERNAME%.txt"
Call CreateProcess
strCommand="cmd /c MOVE /Y " & localFolder & "cleanpclog*.txt Z:\scripts\LOGS\"
Call CreateProcess
strCommand="cmd /c net use * /del /Y"
Call CreateProcess
strCommand="cmd /c del " & localFolder & "cleanpc* /q"
Call CreateProcess
WScript.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 process
Set 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=" & strCommand
errReturn= objWMIService.Create(strCommand, null, null, intProcessID)
' If process creation errored, then quit. Else echo ProcessID and continue subroutine below
If errReturn = 0 Then
Wscript.Echo "Process was started with process ID: " & intProcessID
WScript.Sleep 200
Else
Wscript.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
Wend
WScript.Echo "process ended:" & intProcessID
WScript.Echo
End Sub
' **FUNCTIONS**
' Function to monitor process
Function FindNoPrograms(strComputer,intProcessID)
Dim CNT, objWMIService,colProcesses
Set 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 = CNT
Set objWMIService = Nothing
Set colProcesses = Nothing
End Function
' Subroutine to get masked password
Function GetPass
' Mask Passwords Using Internet Explorer
' http://blogs.technet.com/b/heyscriptingguy/archive/2005/02/04/how-can-i-mask-passwords-using-an-inputbox.aspx
Set 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 strPassword
End If
' Return the password
GetPass = strPassword
End Function
page last modified: 2024-04-16; loaded in 0.0076 seconds