Автоматический перенос старых перемещаемых профилей в архив с помощью Powershell.
Думаю многие системные администраторы использующие перемещаемые профили или folder redirection, сталкивались с проблемой старых профилей уволенных сотрудников.
Ведь не всегда можно проследить перенесли профиль в архив или нет.
Для решения данной проблемы предлагаю использовать следующий скрипт:
В переменных укажите путь к перемещаемым профилям.
В скрипте ниже пути к папкам folder redirection содержат username пользователя, по нему ведется поиск старого профиля в Active Directory.
Если профиля нет в Active Directory, старый профиль с папками folder redirection перемещается в архив.
Если вас интересует перенос старых перемещаемых профилей измените переменные $move1 = $($User.Name) на $move1 = «$User.DOMAIN.V2» , где DOMAIN.V2 префикс перемещаемого профиля Вашего домена.
# Select source folders
$Path1 = "F:\fileshare\UsersFolders\ProfilesIva"
$Path2 = "F:\fileshare\UsersFolders\Profiles"
$Path3 = "F:\fileshare\UsersFolders\ProfilesYar"
$DestinationFolder = "\\archivesrv\OLDusers\OLD"
$ScriptFolder = "D:\Powershell_Scripts\MoveFolders-No-Users"
# Generate short data prefix
$shortdate = get-date -format "dd.MM.yyyy"
## Start checking folders from first region
# Find folder names in $Path1
$userProfile = Get-ChildItem -Path $Path1
$unknownList = @()
# Try to find Active Directory usernames from user folders
foreach($user in $userProfile){
#Try to find user in Active Directory
$ADUser = Get-ADUser -Filter {SamAccountName -eq $User.Name}
#Test to see if the user exists
If($ADUser)
{
#User Exists
#Write-host "$($User.Name) Exists"
}
Else
{
# Create new variable with username (Only Deleted users from Active Directory)
$move1 = $($User.Name)
# Move find folders to $DestinationFolder
Copy-Item $Path1$move1 $DestinationFolder$move1-$shortdate -Verbose -Recurse -Erroraction SilentlyContinue *>> $ScriptFolder\Reports.txt
Remove-Item $Path1$move1 -Recurse -Force
}
}
#Further we will repeat previos commands with next region ($Path2 and $Path3)
##Next Region ($Path2)
$userProfile = Get-ChildItem -Path $Path2
$unknownList = @()
foreach($user in $userProfile){
$ADUser = Get-ADUser -Filter {SamAccountName -eq $User.Name}
If($ADUser)
{
#User Exists
}
Else
{
$move1 = $($User.Name)
Copy-Item $Path2$move1 $DestinationFolder$move1-$shortdate -Verbose -Recurse -Erroraction SilentlyContinue *>> $ScriptFolder\Reports.txt
Remove-Item $Path2$move1 -Recurse -Force
}
}
##Next Region ($Path3)
$userProfile = Get-ChildItem -Path $Path3
$unknownList = @()
foreach($user in $userProfile){
$ADUser = Get-ADUser -Filter {SamAccountName -eq $User.Name}
If($ADUser)
{
#User Exists
}
Else
{
$move1 = $($User.Name)
Copy-Item $Path3$move1 $DestinationFolder$move1-$shortdate -Verbose -Recurse -Erroraction SilentlyContinue *>> $ScriptFolder\Reports.txt
Remove-Item $Path3$move1 -Recurse -Force
}
}
# Generate Email Body
echo "Отчет по профилям удаленных сотрудников:" > $ScriptFolder\Body1.txt
echo " " >> $ScriptFolder\Body1.txt
echo " " >> $ScriptFolder\Body1.txt
echo " " >> $ScriptFolder\Body1.txt
$body = Get-Content $ScriptFolder\Body1.txt
$body2 = Get-Content $ScriptFolder\Reports.txt | Out-String
# Send Email message
Send-MailMessage -From admin@domain.local -To admins@domain.local -Subject "Отчет по старым перемещаемым профилям" -attachment $ScriptFolder\Reports.txt -Encoding ([System.Text.Encoding]::UTF8) -Body $body$body2 -SmtpServer mailserver.domain.local
# Remove old Items
Remove-Item $ScriptFolder\Reports.txt
Remove-Item $ScriptFolder\Body1.txt
После выполнения данный скрипт отправит отчет о перенесенных профилях.
Для автоматизации добавьте данный скрипт в планировщик задач.
Комментарии