【问题标题】:Archiving user folders with powershell使用 powershell 归档用户文件夹
【发布时间】:2021-08-12 05:52:51
【问题描述】:

我目前正在使用 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
}

这适用于文件夹创建,但我正在尝试找到一种方法来存档已离开学生的用户文件夹。我有一个想法,通过从 AD 组获取当前用户名来创建 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 文件将其中包含的所有文件夹移动到新文件夹位置进行归档。

我的想法是否正确,这是最好的方法?或者有没有其他更好的方法给这只猫剥皮?

【问题讨论】:

  • Sort-Object -Unique -Property whatever 应该可以吗?
  • ('a','a','b','c','c','d' | Group-Object | where-object { $_.Count -eq 1}).Name
  • 感谢大家的想法,我会继续研究它,我在考虑 sort-object cmdlet,但不确定这是否会删除重复项并在 csv 文件中留下 1 个条目,所以那个 aa b cc d 的例子会变成 abcd 吗?我会在进行更多测试后回来。

标签: powershell csv directory-structure archiving


【解决方案1】:

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

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

我已经编辑了服务器位置、广告组等,但是一旦你把你的信息放在那里,脚本仍然可以工作。

#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*
} 

【讨论】:

    猜你喜欢
    • 2020-04-12
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 2012-11-27
    • 2022-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多