簡體   English   中英

使用Powershell清理孤立的Active Directory用戶配置文件文件夾

[英]Using Powershell to clean up orphan Active Directory user profile folders

任務似乎微不足道! 在我們的環境中,很多用戶來來往往。 我不時地想要檢查“孤立”漫游配置文件文件夾並擺脫它們。 如果用戶名與文件夾名稱完全匹配,但對於Windows已添加.V2,.V3等的文件夾,則我的代碼起作用。例如-eq -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 "  "

將第一行更改為:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM