用powershell归档用户文件夹



我目前正在做一个项目,用powershell来帮助清理和节省我们服务器上的空间。我在一所中学工作,我们有超过1000个用户。我创建了一个脚本,在一个位置为每个用户创建一个文件夹,并只给该用户和我自己访问文件夹,以便他们在我们的NAS上存储他们的工作和一般文档。不过,我将来会遇到的问题是,当学生离开学校时,我还没有办法存档他们的文件夹,所以几年后,将会出现只有1000个用户,但创建了2000多个个人文件夹的问题,其中许多文件夹可以存档一段时间,然后删除以节省NAS上的空间。我创建的用于生成文件夹的脚本如下(为了隐私,我已经编辑了AD组名称和服务器位置)

Import-Module ActiveDirectory
Import-Module NTFSSecurity
$ADUsers = Get-ADGroupMember -Identity *user AD Group*
ForEach ($ADUser in $ADUsers) 
{
New-Item -ItemType Directory -Path "*Server location*$($ADUser.sAMAccountname)"
$userfolder = "*Server location*$($ADUser.sAMAccountname)"
Get-Item $userfolder | Disable-NTFSAccessInheritance
Get-Item $userfolder | Add-NTFSAccess -Account $ADUser.sAMAccountname -AccessRights FullControl
Get-Item $userfolder | Remove-NTFSAccess -account *user AD Group* -AccessRights FullControl
}

这对于文件夹创建很好,但我正在试图找到一种方法来存档已经离开的学生的用户文件夹。创建CSV文件的我有一个想法得到当前用户名的广告组,然后比较它们的文件夹目录创建的脚本,并保持所有匹配的文件夹,但所有文件夹,不出现在CSV文件移动到另一个位置存档但是我不知道如果这是最好的方式或如果我俯瞰着解决方案,已经在这种类型的事情。获得已离开用户的列表是困难的,因为他们只是从系统中消失,我只有当前用户的列表。

我目前正在尝试使用CSV文件做这个,我的想法是做这样的事情..

Get-ADGroupMember -Identity *user AD Group* | Select-Object samaccountname | Export-Csv -Path "*server location*user test csv.csv"
Get-ChildItem "*server location*" | Select-Object PSChildName | Export-Csv -Path "*server location*folder list.csv"
New-Item -ItemType file *server location*combined_files.csv –force
Get-Content "*server location*user test csv.csv", "*server location*folder list.csv" | Add-Content *server location*combined_files.csv

上面的脚本创建了一个包含用户SamAccountNames的CSV文件和一个包含第一个脚本创建的文件夹名称的CSV文件,并将这两个CSV文件合并在一起,创建了一个新的CSV文件,看起来像

a
a
b
c
c
d

但是我不知道如何删除所有重复的条目只留下唯一的条目所以新的CSV看起来是这样的

b
d

这样我就可以使用这个新的CSV文件将其中包含的所有文件夹移动到新的文件夹位置进行归档。

我的想法是正确的,这是最好的方法吗?还是有更好的方法来剥这只猫的皮?

所以我已经设法找出了一个解决方案,我想做什么,我已经发布了下面的脚本其他人寻找解决问题的方法。基本逻辑是这样的

  1. 创建AD
  2. 中存在的用户的CSV文件
  3. 为已创建的文件夹创建CSV文件
  4. 比较两个文件在一起,并从文件夹列表中删除当前用户,留下一个属于已离开网站的人的文件夹名称列表,并保存为文本文件
  5. 通过删除为创建文本文件而生成的2个CSV文件来进行一点清理
  6. 对文本文件进行一些编辑,以删除CSV的
  7. 格式中生成的引号
  8. 创建一个新目录用于归档,如果你没有合适的位置
  9. 循环遍历文件夹,并将具有相应用户名的文件夹从txt文件移动到新位置

我已经编辑了服务器位置,adgroups等,但脚本仍然会工作,一旦你把你的信息在那里。

#This creates a CSV file of the all the users that are a member of the AD Group
Get-ADGroupMember -Identity *ADGroup* | Select-Object samaccountname | Export-Csv -Path "*CSV File Location*"
#This creates a CSV File of all the folders that have been generated over time for the use of a personal drive
Get-ChildItem *Server location* | Select-Object PSChildName | Export-Csv -Path "*CSV File Location*"
#This compares the 2 CSV files together, and removes names in the current user list CSV from the Current User Folder list CSV 
#and creates a Text file that only contains the names of the folders of users who are no longer in AD and are assumed to have left the site
$disabledUsers = Get-Content -Path "*CSV File Location*"
$enabledUsers = Get-Content -Path "*CSV File Location*" | foreach {
if ($_ -notin $disabledUsers) { $_ }
}
Set-Content -Path "Text File location" $enabledUsers

#This is just to perform a little clean up of the csv files as they are no longer needed
Remove-Item -Path "*CSV File Location*"
Remove-Item -Path "*CSV File Location*"

#This removes the quotations that are created from converting the CSV files to a text file
(Get-Content *Text File location* -Encoding UTF8) | ForEach-Object {$_ -replace '"',''} | Out-File *Text File location* -Encoding UTF8
#This creates the new folder to store the user folders for archiving
New-Item -ItemType Directory -Path "*New Archive Folder Location*"
#This is the loop that then goes through the text file that contains all the users that no longer exist in the system
#and moves their folders to the archive location
$Userlist = Get-Content *Text File location* -Encoding UTF8
ForEach ($user in $Userlist)
{
Move-Item *server Location*$User -Destination *Archive Location*
} 

最新更新