使用Powershell清理孤立的Active Directory用户配置文件文件夹

时间:2016-12-20 21:11:20

标签: powershell active-directory roaming-profile

任务似乎微不足道!很多用户来来往往我们的环境。我时不时地想检查“孤儿”漫游配置文件夹并摆脱它们。如果用户名与文件夹名完全匹配,则我的代码有效,但对于Windows添加了.V2,.V3等的文件夹则不然。 -eq有效,但不是 - 或 - 包含,或者我尝试插入通配符的任何方式。我尝试了一个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 "  "

2 个答案:

答案 0 :(得分:1)

将第一行更改为:

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

提出了更好的解决方案。它使用RegEx从foldername中拆分.V2扩展名,因此检查没有该ext的名称。 中间的$ profile文件夹不是必需的。

# 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 "  "

答案 1 :(得分:0)

非常感谢@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 "  "