文件夹与AD域比较

时间:2016-08-31 19:27:51

标签: powershell active-directory directory

我正在尝试从不再存在的旧用户清理我们的存储并获得misc结果。

我将所有用户名共享为文件夹名称(例如\\storage\homedir\joeblow)AD用户名:Joeblow。在不同年份,用户已从域中删除,但文件夹从未从共享中删除。所以我正在分享并使用它来查询域以查找所述用户是否仍然存在。我似乎大多数时间工作,随机失败。不知道为什么,因为手动有时候代码会执行得很好。有什么想法吗?

以下是代码:

Import-Module ActiveDirectory
$folders = dir \\storage.domain.edu\C$\homedir3 | where {$_.PSIsContainer -eq $true}
$folder4 = "\\storage.domain.edu\C$\homedir4"

function New-Folder($Name, $Size) {
    $folderList = New-Object PSObject
    $folderList | Add-Member -MemberType NoteProperty -Name Name -Value $Name
    $folderList | Add-Member -MemberType NoteProperty -Name Size -Value $Size
    $folderList | Add-Member -MemberType NoteProperty -Name User -Value $User
}

foreach ($i in $folders) {
    Write-Host "Folder presently is folder $i"
    #$ADuser=Get-ADUser -Filter {$i.sAMAccountName.ToUpper() -eq"$i"} -SearchBase "OU=Active,OU=UserAccounts,DC=ad,DC=domain,DC=edu"
    #$ADuser=Get-ADUser -Filter {samaccountname -eq $i} -SearchBase "OU=Active,OU=UserAccounts,DC=ad,DC=domain,DC=edu" -Property samaccountname
    $r = "\\STORAGE.domain.EDU\HOMEDIR\$i"
    $r = $r.ToUpper()
    $ADuser = Get-ADUser -Filter {sameaccountname -eq $i} -SearchBase "OU=Active,OU=UserAccounts,DC=ad,DC=domain,DC=edu" -Property homeDirectory, samaccountname

    if (!$ADuser) {
        Write-Host "Null yo"
        $ADuser = Get-ADUser -Filter {homeDirectory -eq $r} -SearchBase "OU=Active,OU=UserAccounts,DC=ad,DC=domain,DC=edu" -Property homeDirectory, samaccountname
    }
    $ADname = $ADuser.samaccountname.ToUpper()
    $ADuser = $ADuser.homeDirectory
    $ADuser = $ADuser.toupper()
    Write-Host "R be this: $r and equal $ADuser"
    #Write-Host "The person's folder name is:  $ADuser"
    #$iUp = $i.ToUpper()

    Write-Host "The folder's is: $i"
    if ($ADuser -eq $r) {
        Write-Host "User: $ADname is in HomeDir3 $ADuser"
    } else {
        #(Test-Path "$folder4\$ADuser".trim()) {
        #$notFound = $i

        $folderList = New-Folder -Name $ADuser -User
        Write-Host "$ADname failed"
    }
}
#}

Write-Host "$folderList"

#$leftOvers = Compare-Object $folder4 $notFound
#Write-Host " Here is whats left $leftOvers"

#$i | Out-File -append "C:\deadaccounts.txt"

0 个答案:

没有答案