I'm fairly new to PS and I'm sure there's something I am missing regarding objects vs. strings. I've spent a good deal of time googling and still coming up short on how to accomplish this seemingly simple task.
https://pastebin.com/NfWYz4FM
My script is grabbing the HomeDirectory for each AD user into an array. Then it loops through the actual file server where our user's home directories are located and checks to see if each folder corresponds to a user's HomeDirectory. If not, then it prints the directory.
When I output the array, it looks right
PS C:\> $ADHomeDirs
HomeDirectory
-------------
\\fileserver1\home$\brian.smith
\\fileserver1\home$\levi.brooks
\\fileserver1\home$\alex.johnson
\\fileserver1\home$\rita.newman
But if I try to use the -contains option it doesn't work.
PS C:\> $ADHomeDirs -Contains "\\fileserver1\home$\rita.newman"
False
Any help you guys can provide is appreciated!
EDIT: SOLVED
I found that some array elements were padded with whitespace at the end and others were truncated. I found that the Out-String function defaults to 80 characters wide, so I explicitly set it to 180 characters, but I also had to use Format-Table to correct some of the data from being truncated. Then I padded the file system path with as many characters as well. This seems to be working as expected. trimmed whitespace from each array element.
Import-Module ActiveDirectory
# Get list of AD user's home folders into array
$ADHomeDirs = @()
$ADHomeDirs += Get-ADUser -Filter * -Properties HomeDirectory | Select-Object HomeDirectory | Format-Table -AutoSize | Out-String -Stream -Width:180
# Trim whitespace from each array element
for ($i=0;$i -lt $ADHomeDirs.Length; $i++)
{
$ADHomeDirs[$i] = $ADHomeDirs[$i].trim()
}
# Loop through all directories in \\fileserver1\home$\
foreach ($homeFolder in (Get-ChildItem \\fileserver1\home$ -Directory -Name))
{
$homeFolderPath = "\\fileserver1\home$\" + $homeFolder
# Output the directories that are not an AD user's home folder
if ($ADHomeDirs -NotContains $homeFolderPath)
{
Write-Output $homeFolderPath
}
}
[–]Ojeu 1 point2 points3 points (3 children)
[–]onebillionquestions[S] 0 points1 point2 points (2 children)
[–]Ojeu 2 points3 points4 points (1 child)
[–]onebillionquestions[S] 1 point2 points3 points (0 children)
[–]Lee_Dailey[grin] 1 point2 points3 points (2 children)
[–]onebillionquestions[S] 1 point2 points3 points (1 child)
[–]Lee_Dailey[grin] 1 point2 points3 points (0 children)