Announcement

Collapse
No announcement yet.

Deleting a user profile

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Deleting a user profile

    I found this script on this forum (post was closed), I was hoping to get some help on it.

    Code:
    '=========================================================================================
    ' CleanProfiles.vbs
    ' Version: 1.03
    ' Last Update: 2006.04.08
    '=========================================================================================
    ' Purpose:
    ' Delete profile left-over folders from the local servers
    '=========================================================================================
    ' Syntax:
    ' ccscript CleanProfiles.vbs
    '=========================================================================================
    
    Option Explicit
    Const EVENT_SUCCESS = 0, EVENT_ERROR = 1
    Dim arrSafeProfiles(6), iLogType: iLogType = 0
    Dim sOutPut, iSkipped, iDeleted, iErrors
    iSkipped = 0: iDeleted = 0: iErrors = 0
    
    arrSafeProfiles(0) = "All Users"
    arrSafeProfiles(1) = "Default User"
    arrSafeProfiles(2) = "LocalService"
    arrSafeProfiles(3) = "NetworkService"
    arrSafeProfiles(4) = "Administrator"
    arrSafeProfiles(5) = WScript.CreateObject("WScript.Network").ComputerName
    arrSafeProfiles(6) = WScript.CreateObject("WScript.Network").UserName
    
    DeleteProfiles(ProfilesFolder)
    SummaryAction
    EventLog iLogType, sOutPut
    
    Sub DeleteProfiles(sProfilesFolder)
    	On Error Resume Next
    	Dim oFSO: Set oFSO = CreateObject("Scripting.FileSystemObject")
    	Dim oProfilesFolder: Set oProfilesFolder = oFSO.GetFolder(sProfilesFolder)
    	Dim oFolders: Set oFolders = oProfilesFolder.SubFolders
    	Dim oFolder, sFolder2Delete
    	For Each oFolder In oFolders
    		If Not IsSafeProfile(oFolder.Name) Then
    			sFolder2Delete = oFolder.Name
    			LogEntry " + Deleting Profile '" & sFolder2Delete & "'"
    			Err.Clear
    			oFSO.DeleteFolder oFolder.Path, True
    			If Err.Number = 0 Then
    				iDeleted = iDeleted + 1
    				LogEntry "    '" & sFolder2Delete & "' deleted successfully." & vbNewLine
    			Else
    				iLogType = 1
    				iErrors = iErrors + 1
    				LogEntry "    Can't delete '" & sFolder2Delete & "' (Error " & Err.Number & " - " & Err.Description & ")" & vbNewLine
    			End If
    		Else
    			iSkipped = iSkipped + 1
    			LogEntry " - Skipping Profile '" & oFolder.Name & "'" & vbNewLine
    		End If
    	Next
    	Set oFolders = Nothing
    	Set oProfilesFolder = Nothing
    	Set oFSO = Nothing
    End Sub
    
    Function IsSafeProfile(sProfile)
    	On Error Resume Next
    	Dim sData, arrLines, sLine, sUser, i, bFlag: bFlag = False
    	sData = Cmd("query session | find " & Chr(34) & "rdpwd" & Chr(34) & " |find " & Chr(34) & "rdp-tcp#" & Chr(34))
    	arrLines = Split(sData, vbNewLine)
    	If UBound(arrLines) > 0 Then
    	For Each sLine In arrLines
    		sUser = Trim(Mid(sLine,20,20))
    		If sUser <> "" Then
    			If InStr(UCase(sProfile), UCase(sUser)) > 0 Then
    				LogEntry "   User '" & sUser & "' is logged in!"
    				bFlag = True
    				Exit For
    			End If
    		End If
    	Next
    	Else
    		LogEntry "   Can't check if '" & sProfile & "' is logged in!"
    		iLogType = 1
    		bFlag = True
    	End If
    	For i = 0 To UBound(arrSafeProfiles)
    		If InStr(UCase(sProfile), UCase(arrSafeProfiles(i))) > 0 Then
    			LogEntry "   Profile '" & sProfile & "' is in the safe profiles list!"
    			bFlag = True
    			Exit For
    		End If
    	Next
    	IsSafeProfile = bFlag
    End Function
    
    Function Cmd(sCommandLine)
    	On Error Resume Next
    	Dim sFileOut, oFileOut, sCmd
    	Dim oWshShell: Set oWshShell = CreateObject("WScript.Shell")
    	Dim oFSO: Set oFSO = CreateObject("Scripting.FileSystemObject")
    	sFileOut = oFSO.GetTempName
    	sCmd = "%COMSPEC% /c " & sCommandLine & " >" & sFileOut
    	oWshShell.Run sCmd, 0, True
    	If oFSO.FileExists(sFileOut) Then
    		If oFSO.GetFile(sFileOut).Size > 0 Then
    			Set oFileOut = oFSO.OpenTextFile(sFileOut)
    			Cmd = oFileOut.Readall
    			oFileOut.Close
    		End If
    		oFSO.DeleteFile(sFileOut)
    	End If
    	Set oFileOut = Nothing
    	Set oFSO = Nothing
    	Set oWshShell = Nothing
    End Function
    
    Function ProfilesFolder()
    	Dim sProfilesFolder
    	Dim oWshShell: Set oWshShell = WScript.CreateObject("WScript.Shell")
    	Dim oWshSysEnv: Set oWshSysEnv = oWshShell.Environment("Process")
    	sProfilesFolder = oWshSysEnv("USERPROFILE")
    	sProfilesFolder = Left(sProfilesFolder,InStrRev(sProfilesFolder,"\"))
    	ProfilesFolder = sProfilesFolder
    	Set oWshSysEnv = Nothing
    	Set oWshShell = Nothing
    End Function
    
    Sub SummaryAction
    sOutPut = vbNewLine & sOutPut & _
    			"   Actions Summary:" & vbNewLine & _
    			"   -------------------------------------------" & vbNewLine & _
    			"   " & iSkipped & " profile(s) skipped." & vbNewLine & _
    			"   " & iDeleted & " profile(s) deleted." & vbNewLine & _
    			"   " & iErrors & " error(s) encountered." & vbNewLine
    End Sub
    
    Sub LogEntry(sEntry)
    	sOutPut = sOutPut & sEntry & vbNewLine 
    End Sub
    
    Sub EventLog(EventType, sEntry)
    	Dim oShell: Set oShell = Wscript.CreateObject("Wscript.Shell")
    	oShell.LogEvent EventType, WScript.ScriptName & " status:" & _
    						vbNewLine & vbNewLine & sEntry
    	Set oShell = Nothing
    End Sub

    I am writing a VB6 program to help me with user clean up. How can this be modified for VB6 and for a remote PC?

    Thanks for your time!

  • #2
    Re: Deleting a user profile

    VBScript is quite close to Visual Basic, so changing isn't much work. Mostly variable declarations are a bit more strict. Why would you like to use VB6 anyway? IMHO, certainly it is not worth of the trouble. VB 6 has been phased out, and no support is offered anymore. Thus you really should consider some other tool to develop with.

    Remote execution could be done via Sysinternals' psexec tool.

    -vP

    Comment


    • #3
      Re: Deleting a user profile

      Originally posted by vonPryz View Post
      VBScript is quite close to Visual Basic, so changing isn't much work. Mostly variable declarations are a bit more strict. Why would you like to use VB6 anyway? IMHO, certainly it is not worth of the trouble. VB 6 has been phased out, and no support is offered anymore. Thus you really should consider some other tool to develop with.

      Remote execution could be done via Sysinternals' psexec tool.

      -vP
      Because I know VB6, and I am too lazy to learn anything else! LOL

      I am writing a tool to help me. It's like a check list of things to do when a user leaves, but more than a check list. Write now it's working to copy users home folder to an old users share then delete the home folder, delete the AD account, open webpages to remove accounts, open an excel phone list to remove them from it and more. I like using VB6, it's easy, I know there is no support, but when I need help people on forums are always nice and willing to help.

      Comment

      Working...
      X