comparing home directories to user names with powershell
aside from just enjoying scripting, i particularly love scripting requests that start with something like -- “i want to know if this is possible…”. my coworker put it best about why scripting is enjoyable. it has a definite start and (usually) a definite end. it comes with a sense of accomplishment when you’re done. that’s nice. kind of like… building a birdhouse or something.
anyway, the request was to compare a user’s home directory value to the user id to see if the user has the correct mapping. sometimes while cloning accounts, this process may carry over something undesirable. pretty simple stuff right?
let’s pretend you have a user named “Reimer Davies”. his properties are as follows:
- user id: “ReDavies”
- home directory: \\myfileserver\redavies$
(yes, i know the mapping is old school home path. this is what i had to deal with so bear with the example. you can always change around the text manipulation statements to make it do what you want.)
anyway, here it is. i wrote it (hopefully verbosely enough) for the requester to follow along. all i did was split the home directory on the “\” and the “$” to get the exact user id out of it. the user id itself was simple since it’s just as the value sits. in the comparison, i dropped both to lowercases to processes it through. your preference on whether you want to use if/then like i did or switch. the best part was kicking it out to a custom object so i could push it into a gridview. the reason this is cool is that i don’t have to write it once to kick out a list of “true” results and then later revise to kick out a list of “false” results. you know how it goes. sometimes you don’t know what you want until you see what you don’t want.
pushing it to gridview gives the requester the ability to sort and search as required (in 2.0). anyway, let me know what you think.
# initialize a boring array for which we will push in user list information
$userListMatches = @()
$users = Get-QADUser -AccountExpiresAfter (Get-Date) -SearchRoot "ou=me,dc=mydomain,dc=com"
foreach ($user in $users) {
$hmdirtext = $user.homedirectory
$userid = $user.samaccountname
# break the user home directory into the user name only by splitting the \
$hmdir = $hmdirtext.split("\")[3]
# user name will still be user$ at this point so break the $ out
$hmdir = $hmdir.split("$")[0]
# create a noteproperty object so we can use it with grid view
$myUserSet = New-Object System.Object
# add the values to the object
$myUserSet | Add-Member -type noteproperty -Name User -Value $userid
$myUserSet | Add-Member -type noteproperty -Name Home -Value $hmdirtext
# now compare the values
if ($hmdir.ToLower() -eq $userid.ToLower()) {
# add this value when the match is true
$myUserSet | Add-Member -type noteproperty -Name Matches? -Value "True"
} else {
# add this value when the match is false
$myUserSet | Add-Member -type noteproperty -Name Matches? -Value "False"
}
# add the userset value to the userlistmatches array
$userListMatches += $myUserSet
}
# now kick out the grid
$userListMatches | Out-GridView
Get-QADUser -AccountExpiresAfter (Get-Date) -SearchRoot "ou=me,dc=mydomain,dc=com" | where {$_.homedirectory.tolower() -ne "\\myfileserver\" + "$_.samaccountname.tolower() + '$'}
ReplyDeleteFor a quick count if needed.
Perfect, exactly what I was looking for.
ReplyDeleteThanks for putting this together Marcus!