Author Posts

May 2, 2014 at 4:26 pm

Hi there
I have a script that finds out if the user is in the right addresslist in exchange based on the addresslist filter(which is based on group membership)
so something like this:
$users=get-distributionlist department1|get-distributiongroupmember
foreach($user in $users){
if ((Get-ADuser $user.alias -Properties showInAddressBook) -notmatch "department1") {Write-Host "Not IN department1"}

this works great and I get the users that are not members(and should be since they are in the right group)
my question is:
1. the write host that tells me the user is not in the group(I want to keep that rather then looking at the console)how do I do that?

2. I sometimes get error like this: "get-aduser : cannot find an object with identity: 'user100' under : test.com ....... how do I catch these(these are probably orphan users that cannot be found by get-aduser)

Thanks in advance

May 2, 2014 at 4:31 pm

Try {
$InDepartment = $False
if ((Get-ADuser $user.alias -Properties showInAddressBook) -notmatch “department1″) {
$InDepartment = $True
}
} Catch {
# $user.alias wasn't found
}

This way, $InDepartment will be $True/$False based on the last user you checked. If the user doesn't exist, it'll run the Catch{} block. You can do whatever you need there. All of this would go inside your existing ForEach loop.

You'll have to decide what to do with $InDepartment. I don't know enough about what you're trying to do to suggest anything.

Reference: about_variables, about_try_catch_finally

May 3, 2014 at 3:16 am

Thanks
that sounds good
but since I have around 30,000 users to check in multiple groups I wanted to "collect" the users that aren't in the list(the negatives/write-host "not in list")
so at the end of the script I can see them in a list or export them to csv.
same for the exceptions(user not found...)

May 3, 2014 at 1:14 pm

So you have to think like PowerShell.

Let's say your script is named FindPeople.ps1. In it, you would use Write-Output to write out the names you want in the CSV file.

Then run...

.\FindPeople.ps1 | Export-CSV results.csv

Anything you output via Write-Output will go into the pipeline, and can be piped on to another command, like Export-CSV. So long as you output only one kind of thing (say, a string containing a user name), it'll work great.

May 3, 2014 at 6:53 pm

Sorry for bumping in. I also remembered the Tee-Object command, which will do both, show you the results on the console and also allows you to process data further in the pipeline (like exporting it for example).
The help for the Tee-Object should be sufficient to understand how it works.

May 6, 2014 at 5:48 am

Sorry for the delay in reply
first thank you both for giving me some ideas
I tried to use the tee-object to export to file but that only gave me blank file with no entries and I lost the console output since I removed the write-host.
I tried to run the script with the |export-csv but that also returned blank files.
so at the end I just used:
Add-Content c:\scripts\$center.log "User $($user.alias) is not in the $list"
and that worked fine(no output to console but it will do:))
Thanks again
I find the most interesting things are the ones you fail in and then start thinking of "how do I do that?"