📜 ⬆️ ⬇️

Support clean balls to share files using Powershell

Each organization has a network resource for exchanging data between users, to which all have access. What to do when users themselves do not delete temporary files from their folders in the “exchange” and the resource starts to take up too much space?

Task:
1) Automatic cleaning of user folders on a shared network share with preservation of the directory structure up to level 1. In the root of the resource folders are located by the names of users.
2) Saving data for the past day in the "Yesterday" folder (read-only user rights). This is necessary in case the user forgot to pick up an important document yesterday.
3) Logging file copying errors. For analysis.
4) Ability to quickly reconfigure the script for use on another server \ folder.
Initial data:
1) Network folder “Exchange” on // server / obmen, which looks at D: \ obmen
Decision:


PowerShell allows you to work with the xml format using standard tools, so we will use this format to store the settings.
')
Listing of settings.xml file
<?xml version="1.0"?> <settings> <MainDir>D:\obmen</MainDir> <OldDir> D:\obmen \</OldDir> <NameOldDir></NameOldDir> <ShareName>Obmen</ShareName> <AclDir>D:\acl</AclDir> <service>test_service_1</service> <service>test_service_2</service> </settings> 


In the clearshare.ps1 script file, first of all we read the settings:
 [xml]$settings = Get-Content D:\ps_project\\settings.xml $mainfolder = $settings.settings.MainDir #    $oldfolder = $settings.settings.OldDir #   $services = $settings.settings.service #,    $ShareName = $settings.settings.sharename #  $NameOldDir = $settings.settings.NameOldDir #   $acldir = $settings.settings.AclDir #   acl 


To ensure the availability of files, we close all user sessions by disabling the share:
 $share = Get-WmiObject Win32_Share | where {$_.name -eq $ShareName} $share.delete() 


You may also need to disable services. The list of services described in the settings file may increase .:
 stop-service -DisplayName $services 


Clear the Yesterday folder, write errors to the file:
 Remove-Item "$oldfolder\*" -Recurse -Force 2> "$mainfolder\remove_error_log.txt" 


We move today to yesterday, while excluding the “Yesterday” folder itself:
 Get-ChildItem $mainfolder -Exclude $NameOldDir | Move-Item -destination $oldfolder 2> "$mainfolder\move_error_log.txt" 


Create a user folder structure:
 foreach ($foldname in Get-ChildItem $oldfolder | where {$_.Attributes -eq 'Directory'}) { $a = $mainfolder + "\" + $foldname.name; New-Item -type directory -path $a } 


We include services by team
 start-service -DisplayName $services 


Turn on the ball
 $share = [wmiClass] 'Win32_share' $share.Create($mainfolder, $ShareName, "0", "1000") 


We assign READ rights for users and FullControl for admins to the “Yesterday” folder through copies from the reference folder:
 $acl = Get-Acl -path $acldir Set-Acl -Path $oldfolder -AclObject $acl 


So, we got a script that will clear the temporary data of users every night, leaving the opportunity to collect data for forgetful people. It also logs copy errors and has a configuration file to adapt to any shared folder.

Naturally, the script must be added to the task scheduler at a time convenient to you. (Thanks ame )

UPD: Improved the process of recreating the folder structure.

Source: https://habr.com/ru/post/186358/


All Articles