【问题标题】:Using Powershell to clean up orphan Active Directory user profile folders使用 Powershell 清理孤立的 Active Directory 用户配置文件文件夹
【发布时间】:2016-12-20 21:11:19
【问题描述】:

任务似乎微不足道!很多用户在我们的环境中来来去去。时不时地我想检查“孤立”漫游配置文件文件夹并摆脱它们。如果用户名与文件夹名称完全匹配但不适用于 Windows 添加了 .V2、.V3 等的文件夹,则我的代码有效。 -eq 有效,但不适用于 -like 或 -contains,或者我尝试插入通配符的任何方式。我尝试了 where-object 构造,但也失败了。

例如,如果我们有一个用户 bob.smith,我不想让我的“孤立”列表包含名为 bob.smith 或 bob.smith.V2 的配置文件文件夹。

这是我的代码:

# Put profile folder names in array

$profilefolders = Get-ChildItem -Name "\\server\profiles$"



# Initialize arrays 

 $orphanprofiles = @()



foreach ($userprofile in $profilefolders)
    {
#  Do profile names match up with user accounts?
    If(Get-ADUser -Filter {SamAccountName -eq $userprofile})

        {
        # User account exists: no action
        }

    Else
        {
        # Profile is an orphan. Add to list.
        $orphanprofiles += $userprofile
                         }
                }




# put array values in a string variable with newline char for screen   output
$outputprofiles = $orphanprofiles -join "`n"

Write-Output "`n"
Write-Output "Orphan profiles: `n$outputprofiles"
Write-Output "  "

【问题讨论】:

    标签: powershell active-directory roaming-profile


    【解决方案1】:

    将第一行改为:

    $profilefolders = Get-ChildItem -Name "\\server\profiles$"|where {$_ -notmach '.*\.V\d$'}
    

    想出了一个更好的解决方案。它使用 RegEx 将 .V2 扩展名与文件夹名分开,因此检查没有该扩展名的名称。 不需要中间的 $profilefolders。

    # Initialize arrays 
    $orphanprofiles = @()
    $pattern = '^(.*?)(\.V\d)$'
    
    Get-ChildItem -Name "\\server\profiles$"| 
        ForEach {
            If ($_ -match $pattern) {
                If (!(Get-ADUser -Filter {SamAccountName -eq $matches[1]}))
                    {$orphanprofiles += $_} else {}
            } Else {
                If (!(Get-ADUser -Filter {SamAccountName -eq $_}))
                    {$orphanprofiles += $_}
            }
        }
    
    
    # put array values in a string variable with newline char for screen   output
    $outputprofiles = $orphanprofiles -join "`n"
    Write-Output "`n"
    Write-Output "Orphan profiles: `n$outputprofiles"
    Write-Output "  "
    

    【讨论】:

    • 太棒了!第一个答案有效,第二个生成一堆红色,从 !在第 8 行。里面有很多东西我不明白,所以我会去阅读它。非常感谢您对学习曲线的提升。
    • 该死! #1 不是答案:我不想忽略每个附加了 .Vx 的文件夹,只需忽略其中的 .Vx 部分。
    • @SKaye 我更改了第二个脚本,增加了一对带有 `!` not 运算符的括号。家里没有AD,所以无法测试。
    • 感谢 LotPings,我还通过添加双引号消除了红色,例如
    • If ("!Get-ADUser -Filter {SamAccountName -eq $matches[1]}")
    【解决方案2】:

    非常感谢@LotPings。这是我现在的代码(包含所有调试语句):

    # Initialize array
    $orphanprofiles = @()
    
    # set regex pattern
    $pattern = '^(.*?)(\.V\d)$'
    
    Get-ChildItem -Name "\\server\profiles$"| 
        ForEach {
            Write-Host "Testfolder: " $_
            If ($_ -match $pattern) 
            {
              Write-Host "Loop 1 Foldername " $_
              Write-Host "Loop 1 Match " $matches[1]
              $Vnuser = $matches[1]
    
                If(Get-ADUser -Filter {SamAccountName -eq $Vnuser})
                    {
                 # match: do nothing
                    }
                   Else 
                    {
                    $orphanprofiles += $_
                    Write-Host "Loop one inside If statement: " $_
                    }
    
    
         }
    
          Else
            {
               Write-Host "Folders without .Vn: " $_
               If(Get-ADUser -Filter {SamAccountName -eq $_})
                    {
                    # match: do nothing
                    }
                Else
                    {
                    $orphanprofiles += $_
                    Write-Host "Loop 2 foldername: " $_
                    }
    
       } 
    
       }
    
    
    
    # put array values in a string variable with newline char for screen output
    $outputprofiles = $orphanprofiles -join "`n"
    Write-Output "`n"
    Write-Output "Orphan profiles: `n$outputprofiles"
    Write-Output "  "
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-25
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      • 2019-08-17
      • 1970-01-01
      • 1970-01-01
      • 2014-04-29
      相关资源
      最近更新 更多