Removing user profiles from terminal servers

Get help with using AutoHotkey and its commands and hotkeys
User avatar
AlleyArtwork
Posts: 27
Joined: 09 Jun 2015, 21:08

Removing user profiles from terminal servers

20 Jul 2015, 17:30

Hello,
I am trying to build something that will assist in managing user profiles within a large terminal server farm.
The intent is to use this for rebuilding user's profiles quickly, or for account terminations.

Goal:
Get the SID of a user from their domain name
Delete Registry key from HKEY_LOCAL_MACHINE\software\microsoft\windows nt\currentversion\profilelist\%userSID%
Delete folder from c:\Users\%username%

Questions:
How do you get the SID of a different user?
Will FileDelete work with \\%servername%:HKEY_LOCAL_MACHINE\software\microsoft\windows nt\currentversion\profilelist\%userSID%

Concept (certainly not working, but general idea of what I'm trying to put together):
Use Inputbox to simply enter user domain name into
save variable, query ASDI edit (or something else?) to get user SID.
Premade comma separated value of server list
loop server list looking for reg key and user folder and delete them


Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



Thanks in advance for looking. :)
loop {
MsgBox, Life is like an Infinite Message Box of chocolates.`nThere are %a_index% chocolates in this box.
}
User avatar
TLM
Posts: 1322
Joined: 01 Oct 2013, 07:52

Re: Removing user profiles from terminal servers

20 Jul 2015, 20:49

AlleyArtwork wrote:Will FileDelete work with \\%servername%:HKEY_LOCAL_MACHINE\software\microsoft\windows nt\currentversion\profilelist\%userSID%

no, please see RegDelete
User avatar
AlleyArtwork
Posts: 27
Joined: 09 Jun 2015, 21:08

Re: Removing user profiles from terminal servers

21 Jul 2015, 08:50

TLM, Thanks! I completely missed RegDelete when looking at the RegRead help.
As for the user SID, I believe I can comspec with the following:
wmic useraccount where (name='username' and domain='currentdomain') get name,sid
I'm one step closer!
loop {
MsgBox, Life is like an Infinite Message Box of chocolates.`nThere are %a_index% chocolates in this box.
}
User avatar
AlleyArtwork
Posts: 27
Joined: 09 Jun 2015, 21:08

Re: Removing user profiles from terminal servers

21 Jul 2015, 09:33

I believe this is the final working version.
I'll probably put in a tooltop or something to keep tabs on progress, or use a gui instead of input box to display status in a GUI window, but for functionality, this seems to work.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



This could also be handy for removing those pesky .BAK profiles in a fell swoop!
Last edited by AlleyArtwork on 29 Jul 2015, 21:40, edited 1 time in total.
loop {
MsgBox, Life is like an Infinite Message Box of chocolates.`nThere are %a_index% chocolates in this box.
}
User avatar
TLM
Posts: 1322
Joined: 01 Oct 2013, 07:52

Re: Removing user profiles from terminal servers

22 Jul 2015, 16:25

Glad that helped. You don't have to create a txt file to get the userSID.
Simply use WMI and WQL via COM

Code: [Select all] [Download] GeSHi © Codebox Plus

For item in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_UserAccount Where name = '" username "' and domain = 'BGRL'")
userSID := item.name

msgbox % userSID ; << for display purposes only
User avatar
AlleyArtwork
Posts: 27
Joined: 09 Jun 2015, 21:08

Re: Removing user profiles from terminal servers

29 Jul 2015, 21:40

Thanks again. I didn't know how to do it this way, only how to output to cmd to txt file, then parse that file for the info needed. Happy not to have to write to a file just for that. I am probably doing many things inefficiently, but always excited to try it a new way!
loop {
MsgBox, Life is like an Infinite Message Box of chocolates.`nThere are %a_index% chocolates in this box.
}
User avatar
AlleyArtwork
Posts: 27
Joined: 09 Jun 2015, 21:08

Re: Removing user profiles from terminal servers

30 Jul 2015, 09:43

Followup Question:
Am I missing an option or using the wrong option for removing the directories, or is it expected to take this amount of time to perform this action?

The deletion of the directory seems to take an excessively long time (500 to 700 seconds) during the removal of the user files from the terminal servers: FileRemoveDir, \\%A_LoopField%\c$\users\%username%, 1

Manually deleting the profile from User Profiles (after the slow loading Profiles window populates from Computer properties > advanced > profile settings) is fast after the Profiles load.
It is still faster to do this manually on each server than through the script even with 15 servers in the farm. I was hoping the automation would ensure no chance of human error (forgetting a server) and be faster than manually removing profiles, but with what I have, this isn't the case.
I am probably doing it wrong, but that's why i'm in the Ask for Help section ;)
loop {
MsgBox, Life is like an Infinite Message Box of chocolates.`nThere are %a_index% chocolates in this box.
}
User avatar
AlleyArtwork
Posts: 27
Joined: 09 Jun 2015, 21:08

Re: Removing user profiles from terminal servers

02 Aug 2016, 10:52

I know this is an old post, but the other day I had a dire need to clean up a whole bunch of servers, I finally got a chance to look at this again and I did eventually get a working script, I thought I'd share.
This modified this from the examples in the Help file.

What it does:
- msgbox to remind YOU to log off of the server if you're on it before running, then launches inputbox for server name.
- This will search the registry for Roaming Profile users.
- It grabs their User ID and SID from the regkey
- Delete their Users folder (\\server\c$\users\UserID)
- Deletes the ProfileList reg key once their user folder is cleaned up.
- Deletes the recycle bin files, once all user folders are cleaned up,

Work to do:
- There are still other hooks tied to the SID (but not the user name) in the registry. Eventually i'd like to get those as well, but initial attempts searching the Registry for occurrences of the SID seem to take an excruciatingly long time, so I omitted those for now. Trying to determine if the entries are always in a particular location, then can hard set those locations to delete based on location which would be much faster than searching everything.
- Add all of this inside another parsed loop if you need to do multiple servers in succession by entering them into the InputBox separated by a comma. (currently we can only afford to take out one server from the farm at a time but might need to do multiples autonomously in the future).

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

loop {
MsgBox, Life is like an Infinite Message Box of chocolates.`nThere are %a_index% chocolates in this box.
}

Return to “Ask For Help”

Who is online

Users browsing this forum: Helgef, lcalazans, Mimir and 60 guests