任务似乎微不足道!很多用户在我们的环境中来来去去。时不时地我想检查“孤立”漫游配置文件文件夹并摆脱它们。如果用户名与文件夹名称完全匹配,但不适用于 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 " "