【问题标题】:How compare Home folders and Active Directory with copy/remove folders?如何将主文件夹和 Active Directory 与复制/删除文件夹进行比较?
【发布时间】:2019-04-01 23:00:32
【问题描述】:

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

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

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

但我也必须为我的老板将这个家庭 adresar 分类到 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"}

}

【问题讨论】:

  • 用户主目录是如何命名的?与 AD SamAccountName 相同?
  • 嗨,是的。用户名主目录与 AD SamAccountName 相同...

标签: powershell


【解决方案1】:

我认为这可能会对您有所帮助:

$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
        }
    }
}

如果结果符合您的预期,请从 Remove-ItemMove-Item cmdlet 中移除 -WhatIf 开关。 这些开关用于测试,实际上没有任何东西被移动或移除。

【讨论】:

  • 超级,THX。我明天或今晚试试。如果我明天有问题,我可以打给你吗?以及如何将数据从一台服务器移动到另一台服务器。如何注册到同一域中的另一台服务器?此脚本在 CZPRGS024.macz 上启动,但 (move)$leaversRoot 的服务器是 synology.macz/home。我不知道如何注册。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-26
相关资源
最近更新 更多