Not so long ago, I upgraded an RDS farm from Windows Server 2012 R2 to Windows Server 2016, where the main task was to reconfigure the roaming profile folders due to the name change from v2 to v6. After the transfer, the total number of folders was over 6000, they already took up decent disk space. There was an idea to remove unnecessary ones according to the criteria,
- Active Directory user account disabled more than 60 days ago.
- Move a folder over the network to an archive server.
- Move no more than 25 folders per day, so that in case of something there is not a large number of applications.
- Keep all rights to folders and subfolders.
- Place the script in scheduled tasks and run as scheduled,
- #Requires - Modules ActiveDirectory
- #Requires - RunAsAdministrator
- function Date {
- Get - Date - format "HH: mm: ss dd.MM.yyyy"
- }
- # Specify the folder
- for creating logs and the format of the file name,
- if it is not there, then create it
- $ log_folder = "$ PSScriptRoot \ Logs \" + $ ($ MyInvocation.MyCommand.Name -replace (".ps1 ", "
- "))
- $ log = "$ log_folder \ $ (Get-Date -Format"
- yyyy_MM_dd_HH_mm_ss "). txt"
- if (!(Test - Path $ log_folder - PathType Container - ErrorAction SilentlyContinue)) {
- New - Item $ log_folder - ItemType D - Force | Out - Null
- }
- "$ (Date) Start Processing" | Tee - Object $ log - Append
- # # # Maximum we move 25 folders
- $ max_folders_to_move = 25
- # Define variables where the sources of the source folders and their final location are specified
- $ source_ts_profiles_folder = "\\ Dfs07.root.pyatilistnik.org \ m $ \ Share \ tsProfiles_TermNew"
- $ dest_ts_profiles_folder = "\\ SBU03.root.pyatilistnik.org \ D $ \ share \ Unused Files \ tsProfiles_TermNew"
- # Define variables where the sources of the source folders and their final location are specified
- $ source_home_folder1 = "\\ Dfs05.root.pyatilistnik.org \ S $ \ Share \ home1"
- $ source_home_folder2 = "\\ Dfs05.root.pyatilistnik.org \ S $ \ Share \ home2"
- $ dest_home_folder = "\\ S03.root.pyatilistnik.org \ D $ \ share \ Unused Files \ home"
- # Specifies an Active Directory lookup that specifies a global catalog server
- $ DCs = @ {}
- foreach($ domain in (Get - ADForest).Domains) {
- $ DC = ""
- $ DC = (Get - ADDomain $ domain).ReplicaDirectoryServers | select - First 1
- $ DCs.Add($ domain, $ DC)
- }
- $ GC = $ DCs.
- "Root.pyatilistnik.org" + ": 3268"
- "$ (Date) Will use` "
- $ GC`" as Global Catalog Server "| Tee-Object $ log -Append
- ### Declaring the variable $ i for the first time
- $ i = 1
- foreach ($ folder in (Get-ChildItem $ source_ts_profiles_folder -Directory)) # | select -First 10
- {
- if ($ i -gt $ max_folders_to_move) {Continue}
- $ username = $ domain = $ domain_long = $ user = $ server = $ lastLogonTimeStamp = $ null
- # Divide the folder name of the domain \ username.v6 format
- $ domain = $ ($ folder.Name -split "\.") [- 2]
- $ v = $ ($ folder.Name -split "\.") [- 1]
- $ domain_long = $ DCs.Keys | ? {$ _ -like "$ domain *"}
- $ username = $ folder.Name -replace (". $ domain. $ v", "")
- if (! $ domain_long)
- {
- "$ (Date) Unable to identify domain. Please check folder`
- "$ ($ folder.Name)` "
- " | Tee-Object $ log -Append
- Continue
- }
- $ server = $ DCs.$ domain_long
- # "$ (Date) Trying to find user` "
- $ username`" in domain $ domain / $ domain_long using DC $ server "| Tee-Object $ log -Append
- try {
- $ user = Get-ADUser $ username -Properties lastLogonTimeStamp -Server $ server -ErrorAction Stop
- }
- catch {
- "$ (Date) $ ($ _. Exception.message)" | Tee-Object $ log -Append
- }
- $ lastLogonTimeStamp_diff = (New-TimeSpan -Start $ ([datetime] :: FromFileTime ($ user.lastLogonTimeStamp)) -End $ (Get-Date)). Days
- # It is indicated that we are looking for users with the status off for at least 60 days
- if ($ user.Enabled -eq $ false -and $ lastLogonTimeStamp_diff -gt 60)
- {
- "$ (Date) User`
- "$ username`"
- Enabled status is $($ user.Enabled), lastLogonTimeStamp diff days: $ lastLogonTimeStamp_diff "| Tee-Object $ log -Append
- "$ (Date) Going to move folder $ i / $ max_folders_to_move` "
- $($ folder.Fullname)` "to`
- "$ dest_ts_profiles_folder`"
- "| Tee-Object $ log -Append
- # Calling the utility of the robocopy utility with the necessary keys
- $ log_robocopy = "$ log_folder \ robocopy_" + (Get - Date - Format "yyyy_MM_dd_HH_mm_ss") + ".log" & robocopy `" $ ($ folder.Fullname) `
- " `"
- $ dest_ts_profiles_folder\ $($ folder.Name) `" / NJH / MOVE / S / B / UNILOG: $ log_robocopy # / L
- $ i ++
- }
- }
- ###
- $ i = 1
- foreach ($ folder in (Get-ChildItem $ source_home_folder1, $ source_home_folder2 -Directory)) # | select -First 10
- {
- if ($ i -gt $ max_folders_to_move) {Continue}
- $ username = $ user = $ users_found = $ lastLogonTimeStamp = $ null
- $ username = $ folder.Name # -replace (". $ domain. $ v", "")
- # "$ (Date) Trying to find user`
- "$ username`" in thw whole AD forest "| Tee-Object $ log -Append
- try {
- $ user = Get - ADUser $ username - Properties lastLogonTimeStamp - Server $ GC - ErrorAction Stop
- } catch {
- "$ (Date) $ ($ _. Exception.message)" | Tee - Object $ log - Append
- }
- $ users_found = ($ user | Measure - Object).Count
- # "$ (Date) Found users: $ users_found" | Tee - Object $ log - Append
- if ($ users_found - ne 1) {
- "$ (Date) Need to check AD for folder` "
- $($ folder.FullName)` '"| Tee-Object $ log -Append
- Continue
- }
- $ lastLogonTimeStamp_diff = (New-TimeSpan -Start $ ([datetime] :: FromFileTime ($ user.lastLogonTimeStamp)) -End $ (Get-Date)). Days
- # It is indicated that we are looking for users with the status off for at least 60 days
- if ($ user.Enabled -eq $ false -and $ lastLogonTimeStamp_diff -gt 60)
- {
- "$ (Date) User`
- "$ username`"
- Enabled status is $($ user.Enabled), lastLogonTimeStamp diff days: $ lastLogonTimeStamp_diff "| Tee-Object $ log -Append
- "$ (Date) Going to move folder $ i / $ max_folders_to_move` "
- $($ folder.Fullname)` "to`
- "$ dest_home_folder`"
- "| Tee-Object $ log -Append
- # Calling the utility of the robocopy utility with the necessary keys
- $ log_robocopy = "$ log_folder \ robocopy_" + (Get - Date - Format "yyyy_MM_dd_HH_mm_ss") + ".log" & robocopy `" $ ($ folder.Fullname) `
- " `"
- $ dest_home_folder\ $($ folder.Name) `" / NJH / MOVE / S / B / UNILOG: $ log_robocopy
Join the conversation! Your thoughts help the community grow.