Support pure balls to share files with Powershell

In every organization there is network communication between users, in which access have all. What to do when users do not delete the temporary files from their folders in "exchange" and the resource begins to take up too much space?

Task:
1) Automatic cleaning of user folders on a network share, preserving the directory structure up to level 1. At the root of the resource folder located on the member's last name.
2) Preservation of data over the past day in the folder "Yesterday" (the rights of users to read-only). This must be the case that the user forgot to pick up an important document yesterday.
3) Logging of file copy failures. For analysis.
4) the Ability to quickly reconfigure the script to use on another server\folder.
Source data:
1) Network folder Share on //server/obmen, which looks at D:\obmen
Solution:


PowerShell allows you to work with xml regular means, this is to store the settings we'll use this format.

The listing file settings.xml
the
<?xml version="1.0"?>
<settings>
<MainDir>D:\obmen</MainDir>
<OldDir> D:\obmen \Yesterday</OldDir>
<NameOldDir>Yesterday</NameOldDir>
<ShareName>Obmen</ShareName>
<AclDir>D:\acl</AclDir>
<service>test_service_1</service>
<service>test_service_2</service>
</settings>


In the script file clearshare.ps1 first read settings:
the
[xml]$settings = Get-Content D:\ps_project\Обмен\settings.xml
$mainfolder = $settings.settings.MainDir #the path to the folder sharing
$oldfolder = $settings.settings.OldDir #path of the folder Yesterday
$services = $settings.settings.service #Services that should be  shut  down
$ShareName = $settings.settings.sharename #Name balls
$NameOldDir = $settings.settings.NameOldDir #the name of the folder Yesterday
$acldir = $settings.settings.AclDir #the name of the reference folder acl


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


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


Clean the folder Yesterday, write error in the file:
the
Remove-Item "$oldfolder\*" -Recurse-Force 2> "$mainfolder\remove_error_log.txt"


Roaming today in yesterday, while exclude the folder itself Yesterday:
the
Get-ChildItem $mainfolder -Exclude $NameOldDir | Move-Item -destination $oldfolder 2> "$mainfolder\move_error_log.txt"


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


Included services team
the
start-service -DisplayName $services


Ball included
the
$share = [wmiClass] 'Win32_share'
$share.Create($mainfolder, $ShareName, "0", "1000")


Assign READ rights for users and administrators to FullControl on the folder "Yesterday" using the copy reference folder:
the
$acl = Get-Acl -path $acldir
Set-Acl -Path $oldfolder -AclObject $acl


So we've got a script that will clean up the temporary user data every night, leaving the ability to collect data for the forgetful people. He also classifies the errors up and has a configuration file to adapt to any shared folder.

Of course the script must be add in the scheduler at time convenient to You. (Thanks to ame)

UPD: Modified the process of recreating the folder structure.
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

ODBC Firebird, Postgresql, executing queries in Powershell

Installation LivestreetCMS on MODX Revolution package 10 clicks

The Ministry of communications wants to ban phones without GLONASS