如何比较主文件夹和Active Directory与复制/删除文件夹?

时间:2019-04-01 13:20:38

标签: powershell

我在一个项目上工作,我有问题。分配:powershell脚本必须按名称将F:UserHome上的Home用户文件夹与AD进行比较。如果名称文件夹与ActiveDirectory中的名称相同,则不要执行任何操作并继续。

但是如果用户不在AD中,脚本必须进行比较:

  1. 如果家庭地址包含数据,请将此地址从F:移至Synology NAS(这是另一台服务器)。
  2. 如果家用地址是空的,则可以将其删除/删除。

但是我也必须为我的老板将这个首页地址分类为2个文件夹(UserToMove.txt / UserToRemove.txt)-在相同的条件下。

如果您听不懂什么,我可以再解释一次。对我来说很重要

这是我创建的脚本,但是无法正常工作:-里面一团糟

$homeDriveRoot = "F:\UserHome"
$leaversRoot = "\new storage on NAS"

$folders = Get-ChildItem $homeDriveRoot | Select -ExpandProperty Name


foreach($folder in $folders) {


$folder
#Compare by name
$u = Get-ADUser -identity $folder -Filter {Enabled -eq $true}|Select ExpandProperty Name

#If>0
if (($u).count -gt 0) {

   #If empty - remove
    if(($u) -eq $null){ Copy-Object -Path "$homeDriveRoot$_" -Destination C:\Users\branym.adm\desktop\remove.csv -Force}
   #If<0 write to file
   else{Copy-Object -Path "$homeDriveRoot$_" -Destination C:\Users\branym.adm\Desktop\active.csv -Force};


} 

#If dont search
else { echo "lost $u folder"}

}

1 个答案:

答案 0 :(得分:0)

我认为这可能对您有帮助

$homeDriveRoot = "F:\UserHome"
$leaversRoot   = "\new storage on NAS"

# create two variables for the output text files. (they will end up on your desktop)
$removeFile = Join-Path -Path ([Environment]::GetFolderPath("Desktop")) -ChildPath 'UserToReMove.txt'
$moveFile   = Join-Path -Path ([Environment]::GetFolderPath("Desktop")) -ChildPath 'UserToMove.txt'

# check if the destination folder in $leaversRoot exists. If not create it first
if (!(Test-Path -Path $leaversRoot -PathType Container)) {
    New-Item -Path $leaversRoot -ItemType Directory | Out-Null
}

# get a list of all folders in the $homeDriveRoot. 
# The items in the list are FolderInfo objects, not simply strings.
# If your PowerShell version is less than 3.0, write it like this:
# $folders = Get-ChildItem -Path $homeDriveRoot | Where-Object { $_.PSIsContainer }
$folders = Get-ChildItem -Path $homeDriveRoot -Directory
foreach($folder in $folders) {
    # see if we can find an AD user with this SamAccountName
    $user = Get-ADUser -Identity $folder.BaseName
    if (!$user -or $user.Enabled -eq $false) {
        # there is no active AD user found for this folder name
        # test if the folder is empty or not
        # by using Select-Object -First 1 the enumeration of files and/or folders stops at the first item
        if ((Get-ChildItem -Path $folder.FullName -Force | Select-Object -First 1 | Measure-Object).Count -eq 0) {
            # the folder is empty, so it can be deleted
            # Add a line to the $removeFile
            Add-Content -Path $removeFile -Value $folder.BaseName
            Remove-Item -Path $folder.FullName -Force -Confirm:$false -WhatIf
        }
        else {
            # the folder has items in it, so move it to NAS
            # Add a line to the $moveFile
            Add-Content -Path $moveFile -Value $folder.BaseName
            Move-Item -Path $folder.FullName -Destination $leaversRoot -Force -WhatIf
        }
    }
}

如果结果是您期望的结果,请从-WhatIfRemove-Item cmdlet中释放Move-Item开关。 这些开关是用于测试的,实际上没有移动或移除任何东西。