タスクは些細なようです!私たちの環境には多くのユーザーが行き来しています。時々、「孤立した」ローミング プロファイル フォルダをチェックして、それらを取り除きたいと思います。私のコードは、ユーザー名がフォルダー名と正確に一致する場合に機能しますが、Windows が .V2、.V3 などを追加したフォルダーには一致しません。ワイルドカードを挿入します。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 " "