Jump to content

Recursion confusion and such


Recommended Posts

The code below will allow you to enter the PC name and it will go to that computer and find all the user profiles and delete them. But it will skip/omit the ones you tell it to. There is also a portion that checks the current logged on user and ignors that persons profile as well.

The code loops through and re-checks who is currently logged on before every deletion to just be on the safe side.

However i could not figure out a better way to do this particular part without having to call the functions within the functions and possibly causing the stack overflow. I read the wiki and still am unclear as to how to prevent it and implement the calls correctly.

The code is messy, and i am sure it can be cleaned up and made to be more efficient but it does work. Any suggestions as to how to clean it up and run smoother?

#include <Date.au3>
#include <Crypt.au3>
#include <WinAPI.au3>
#include <Constants.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <StatusBarConstants.au3>
#include <TabConstants.au3>
#include <WindowsConstants.au3>
#include <GUIComboBox.au3>
#include <GuiListView.au3>
#include <File.au3>
#include <GuiTab.au3>
#include <IE.au3>
#include <Array.au3>
#include <Color.au3>
#include <GuiRichEdit.au3>

FileInstall('.\Include\Psexec.exe', @TempDir & '\')
FileInstall('.\Include\Psloggedon.exe', @TempDir & '\')


Global $Wname
Global $cUser
Global Const $PsExecReg = 'HKCU\Software\Sysinternals\PsExec'
Global Const $PsLoggReg = 'HKCU\Software\Sysinternals\PsLoggedon'
Global Const $PS = 'psexec.exe'
Global Const $PL = 'psloggedon.exe'

Global $TxtLog = "_LoggedOn.txt"
Global $TxtDir = "C:\temp\"
Global $CurrentLoggedOn, $Pid_CMD, $CountLines

If RegRead($PsExecReg, 'EulaAccepted') = '' Then RegWrite($PsExecReg, 'EulaAccepted', 'REG_DWORD', 1)
If RegRead($PsLoggReg, 'EulaAccepted') = '' Then RegWrite($PsLoggReg, 'EulaAccepted', 'REG_DWORD', 1)


$Wname = InputBox("Enter Workstation Name", "PC NAME:")

;_Loggedon()
;sleep(3000)
;_File_Line_Count()
;_Open_File()
;_Trim_LoggedOn()


_List_Profiles()

#comments-start


    Func _Loggedon()

    $Pid_CMD = Run(@ComSpec & ' /c ' & $PL & ' -l -x \\' & $Wname & '| FIND "AD\" >> ' & $TxtDir & $Wname & $TxtLog)

    ;MsgBox(0, "Test", "CMD PID: " & $Pid_CMD)

    EndFunc   ;==>_Loggedon

    Func _File_Line_Count()

    $CountLines = _FileCountLines($TxtDir & $Wname & $TxtLog)
    MsgBox(64, "Error log recordcount", "There are " & $CountLines & " in the error.log.")

    EndFunc

    Func _Open_File()

    $File = FileOpen($TxtDir & $Wname & $TxtLog, 0)

    $Line = FileReadLine($File, $CountLines)

    $CurrentLoggedOn = $Line

    ConsoleWrite($Line & @CRLF & @CRLF & $CurrentLoggedOn & @CRLF & @CRLF & $Wname & @CRLF & @CRLF)

    FileClose($File)

    EndFunc

    Func _Trim_LoggedOn()

    $Trim = $CurrentLoggedOn

    $Text = StringStripWS($CurrentLoggedOn, 3)

    $len = StringLen($CurrentLoggedOn)
    MsgBox(0, "String length is:", $len)

    If StringRight($Text, 2) = "-m" Then
    MsgBox(0, "Admin Logged in", "OOPS ADMIN: " & $Text)
    Else
    $Result = StringTrimLeft($Text, 3)
    MsgBox(0, "test", $Result)
    ConsoleWrite($Result & @CRLF & @CRLF)
    EndIf

    $CurrentLoggedOn = $Result

    ConsoleWrite($CurrentLoggedOn & @CRLF & @CRLF)

    EndFunc

#comments-end

Func _List_Profiles()

    $FileList = _FileListToArray("\\" & $Wname & "\C$\Documents And Settings", "*", 2)

    If @error Then
        MsgBox(0, "error", "ERROR")
        Exit
    EndIf

    For $x = $FileList[0] To 1 Step -1

        FileDelete($TxtDir & $Wname & $TxtLog)

        $Pid_CMD = Run(@ComSpec & ' /c ' & $PL & ' -l -x \\' & $Wname & '| FIND "AD\" >> ' & $TxtDir & $Wname & $TxtLog)

        Sleep(3000)

        $CountLines = _FileCountLines($TxtDir & $Wname & $TxtLog)

        $File = FileOpen($TxtDir & $Wname & $TxtLog, 0)

        $Line = FileReadLine($File, $CountLines)

        $CurrentLoggedOn = $Line

        FileClose($File)

        $Trim = $CurrentLoggedOn

        $Text = StringStripWS($CurrentLoggedOn, 3)

        If StringRight($Text, 2) = "am" Then
            MsgBox(0, "Admin Logged in", "OOPS ADMIN: " & $Text)
        Else
            $Result = StringTrimLeft($Text, 3)

        EndIf

        $CurrentLoggedOn = $Result

        If $FileList[$x] = 'Administrator' Then
            _ArrayDelete($FileList, $x)
            Sleep(100)
        ElseIf $FileList[$x] = 'localservice' Then
            _ArrayDelete($FileList, $x)
            Sleep(100)
        ElseIf $FileList[$x] = 'networkservice' Then
            _ArrayDelete($FileList, $x)
            Sleep(100)
        ElseIf $FileList[$x] = 'All users' Then
            _ArrayDelete($FileList, $x)
            Sleep(100)
        ElseIf $FileList[$x] = 'Default User' Then
            _ArrayDelete($FileList, $x)
            Sleep(100)
                ElseIf $FileList[$x] = $CurrentLoggedOn Then
            Sleep(100)
        Else

            DirRemove("\\" & $Wname & "\C$\Documents And Settings\" & $FileList[$x], 1)
            _ArrayDelete($FileList, $x)
            Sleep(100)
        EndIf

    Next

    FileDelete($TxtDir & $Wname & $TxtLog)

EndFunc   ;==>_List_Profiles


MsgBox(0, "Profile Deletion Complete", "Profiles have been deleted on: " & $Wname)
Link to comment
Share on other sites

This is how I did it for a project I created, it will not include the standard user profiles in the documents and settings or user folder (Administrator, Default User, etc.) or the logged on user.

#include <_array.au3>
#include <File.au3>
Global $aUserList[1], $X = 0
If @OSVersion == "WIN_7" Or @OSVersion == "WIN_VISTA" Or @OSVersion = "WIN_2008" Then
    $NewOS = True
ElseIf @OSVersion == "WIN_XP" Then
    $NewOS = False
EndIf
If Not $NewOS Then
    $UserList = _FileListToArray("C:\Documents and Settings", "*.*", 2)
    For $I = 1 To $UserList[0]
        ReDim $aUserList[$X + 1]
        Select
            Case $UserList[$I] = "All Users"
            Case $UserList[$I] = "Default User"
            Case $UserList[$I] = "NetworkService"
            Case $UserList[$I] = "LocalService"
            Case $UserList[$I] = "Administrator"
            Case $UserList[$I] = @UserName
            Case Else
                $aUserList[$X] = $UserList[$I]
                $X += 1
        EndSelect
    Next
Else
    $UserList = _FileListToArray("C:\Users", "*.*", 2)
    For $I = 1 To $UserList[0]
        ReDim $aUserList[$X + 1]
        Select
            Case $UserList[$I] = "All Users"
            Case $UserList[$I] = "Default"
            Case $UserList[$I] = "Default User"
            Case $UserList[$I] = "Public"
            Case $UserList[$I] = @UserName
            Case Else
                $aUserList[$X] = $UserList[$I]
                $X += 1
        EndSelect
    Next
EndIf
ReDim $aUserList[$X]
_ArrayDisplay($aUserList)

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

ok that was odd. tried to access this post and others and got the 403 no soup for you blocked.

Anyway.... Thank you again Brew! It does appear to at least gather and filter the information i need faster.

See code below for the changes i needed to make.

#include <array.au3>
#include <File.au3>



FileInstall('.\Include\Psexec.exe', @TempDir & '\')
FileInstall('.\Include\Psloggedon.exe', @TempDir & '\')

Global $aUserList[1], $X = 0
Global $Wname
Global $cUser
Global Const $PsExecReg = 'HKCU\Software\Sysinternals\PsExec'
Global Const $PsLoggReg = 'HKCU\Software\Sysinternals\PsLoggedon'
Global Const $PS = 'psexec.exe'
Global Const $PL = 'psloggedon.exe'

Global $TxtLog = "_LoggedOn.txt"
Global $TxtDir = "C:\temp\"
Global $CurrentLoggedOn, $Pid_CMD, $CountLines

If RegRead($PsExecReg, 'EulaAccepted') = '' Then RegWrite($PsExecReg, 'EulaAccepted', 'REG_DWORD', 1)
If RegRead($PsLoggReg, 'EulaAccepted') = '' Then RegWrite($PsLoggReg, 'EulaAccepted', 'REG_DWORD', 1)

$Wname = InputBox("Enter Workstation Name", "PC NAME:")

If @OSVersion == "WIN_7" Or @OSVersion == "WIN_VISTA" Or @OSVersion = "WIN_2008" Then
    $NewOS = True
ElseIf @OSVersion == "WIN_XP" Then
    $NewOS = False
EndIf
If Not $NewOS Then

    $UserList = _FileListToArray("\\" & $Wname & "\C$\Documents And Settings", "*", 2)

    If @error Then
        MsgBox(0, "error", "ERROR")
        Exit
    EndIf

    FileDelete($TxtDir & $Wname & $TxtLog)

    $Pid_CMD = Run(@ComSpec & ' /c ' & $PL & ' -l -x \\' & $Wname & '| FIND "AD\" >> ' & $TxtDir & $Wname & $TxtLog)

    _Trimmy()

    For $I = 1 To $UserList[0]
        ReDim $aUserList[$X + 1]
        Select
            Case $UserList[$I] = "All Users"
            Case $UserList[$I] = "Default User"
            Case $UserList[$I] = "NetworkService"
            Case $UserList[$I] = "LocalService"
            Case $UserList[$I] = "Administrator"
            ;Case $UserList[$I] = ""
            ;Case $UserList[$I] = ""
            Case StringRight($UserList[$I], 3) = "adm"
            Case $UserList[$I] = $CurrentLoggedOn
            Case Else
                $aUserList[$X] = $UserList[$I]
                $X += 1
        EndSelect
    Next
Else
    $UserList = _FileListToArray("C:\Users", "*.*", 2)
    For $I = 1 To $UserList[0]
        ReDim $aUserList[$X + 1]
        Select
            Case $UserList[$I] = "All Users"
            Case $UserList[$I] = "Default"
            Case $UserList[$I] = "Default User"
            Case $UserList[$I] = "Public"
            Case $UserList[$I] = @UserName
            Case Else
                $aUserList[$X] = $UserList[$I]
                $X += 1
        EndSelect
    Next
EndIf
ReDim $aUserList[$X]

_ArrayDisplay($aUserList)


Func _Trimmy()

    Sleep(3000)
    $CountLines = _FileCountLines($TxtDir & $Wname & $TxtLog)
    $File = FileOpen($TxtDir & $Wname & $TxtLog, 0)
    $Line = FileReadLine($File, $CountLines)
    $CurrentLoggedOn = $Line
    FileClose($File)
    $Trim = $CurrentLoggedOn
    $Text = StringStripWS($CurrentLoggedOn, 3)
    $Result = StringTrimLeft($Text, 3)
    $CurrentLoggedOn = $Result


EndFunc   ;==>_Trimmy

However, I am stuck yet again on trying to get this to loop through over and over until everything is completed.

I need this part of code to re-run until the array is empty.

$UserList = _FileListToArray("\\" & $Wname & "\C$\Documents And Settings", "*", 2)

    If @error Then
        MsgBox(0, "error", "ERROR")
        Exit
    EndIf

    FileDelete($TxtDir & $Wname & $TxtLog)

    $Pid_CMD = Run(@ComSpec & ' /c ' & $PL & ' -l -x \\' & $Wname & '| FIND "AD\" >> ' & $TxtDir & $Wname & $TxtLog)

    _Trimmy()

    For $I = 1 To $UserList[0]
        ReDim $aUserList[$X + 1]
        Select
            Case $UserList[$I] = "All Users"
            Case $UserList[$I] = "Default User"
            Case $UserList[$I] = "NetworkService"
            Case $UserList[$I] = "LocalService"
            Case $UserList[$I] = "Administrator"
            ;Case $UserList[$I] = ""
            ;Case $UserList[$I] = ""
            Case StringRight($UserList[$I], 3) = "adm"
            Case $UserList[$I] = $CurrentLoggedOn
            Case Else
                $aUserList[$X] = $UserList[$I]
                $X += 1
        EndSelect
    Next
Else
    $UserList = _FileListToArray("C:\Users", "*.*", 2)
    For $I = 1 To $UserList[0]
        ReDim $aUserList[$X + 1]
        Select
            Case $UserList[$I] = "All Users"
            Case $UserList[$I] = "Default"
            Case $UserList[$I] = "Default User"
            Case $UserList[$I] = "Public"
            Case $UserList[$I] = @UserName
            Case Else
                $aUserList[$X] = $UserList[$I]
                $X += 1
        EndSelect
    Next
EndIf
ReDim $aUserList[$X]

This

DirRemove("\\" & $Wname & "\C$\Documents And Settings\" & $aUserList[$x], 1)

needs to be inserted right here

Case $UserList[$I] = "LocalService"
            Case $UserList[$I] = "Administrator"
            ;Case $UserList[$I] = ""
            ;Case $UserList[$I] = ""
            Case StringRight($UserList[$I], 3) = "adm"
            Case $UserList[$I] = $CurrentLoggedOn
            Case Else
                $aUserList[$X] = $UserList[$I]
                $X += 1
        EndSelect


>>>>>>>> DirRemove("\\" & $Wname & "\C$\Documents And Settings\" & $aUserList[$x], 1) <<<<<<<<<<<<


    Next
Else

I am assuming?

Then i need it to start the entire process over again. Need to recheck the currently logged on person. then delete the next profile from the pc and array if it isnt one of the exclude ones.

Link to comment
Share on other sites

The code I posted gives you an array of user names that are listed in the Users folder or the Documents and Settings folder on the computer. You don't reuse that code to delete the other users, you use the array that's created and loop through that once to delete the folders you need.

For $x = 0 to Ubound($aUserList) - 1
    DirRemove("\\" & $Wname & "\C$\Documents And Settings\" & $aUserList[$x], 1)
Next

Just put that after the code that I posted, in place of the _ArrayDisplay and it will loop through the array and delete the folders.

EDIT: or you could put that code in the Case statement that builds the array in the first place like this, so you wouldn't have to build the array at all.

Case $UserList[$I] = @UserName
    Case Else
            DirRemove("\\" & $Wname & "\C$\Documents And Settings\" & $UserList[$i], 1)
    EndSelect
Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...