Powershell Script that lists AD users of Trusted Domains

I have a powershell script that I run to list AD users that are members of a one way trust, and am having some issues making a couple of changes.  The current PS is as follows:

Import-Module ActiveDirectory
(Get-ADTrust).target | % {
Get-ADUser -filter * -Server $_ | Select Name,sAMAccountName | Export-CSV "C:\User Accounts.csv" -nti -append
}

Open in new window


The first problem is that it won't run through without me entering a * at a prompt that says:
cmdlet Get-AdTrust at command pipeline position 1
Supply values for the following parameters:
Filter:

This writes all user accounts to one CSV which I can import to SQL but the issue is that it doesn't list the domain and I'd like to have it show only enabled accounts.  Can someone give me a hand with getting each exported item to show the ADTrust target domain in a column next to the Name and sAMAccountName, so I can properly associate the user with the particular domain- and I can have the script automatically run without needing to specify the * on first run?

Thanks in advance.
LVL 6
Uptime Legal SystemsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Will SzymkowskiSenior Solution ArchitectCommented:
The first problem is that it won't run through without me entering a * at a prompt that says:

This is happening because when you use Get-ADTrust you have to specify a -Filter property which is required and it is the 1st positional parameter. If you do not specify anything when you run your script it will prompt you.

If you do not want to use the -Filter parameter you can also use the -Identity parameter and point to the exact trust rather than the filter.

Example below...
Import-Module ActiveDirectory
Get-ADTrust -Identity corp.example.com | % {
Get-ADUser -filter * -Server $_ | Select Name,sAMAccountName | Export-CSV "C:\User Accounts.csv" -nti -append
}

Open in new window


Will.
Uptime Legal SystemsAuthor Commented:
Thanks for the post.

I think maybe my issue is that I'm not sure where the -filter needs to be, as it won't accept it after the .target portion.  There are several hundred domains under this specific trust- and domains may be added or removed periodically- so I need it to be able to automatically run based off the list of domains in the trust (so I'm not able to have it run off the specific domain entry).

(Get-ADTrust).target -filter *

Open in new window

The above results in an error.

Any advice on getting it to append the name of the target to the CSV per user account in that particular domain?  I know I can export the distinguishedName but it's more clean up to sort out after the CSV is created.
Will SzymkowskiSenior Solution ArchitectCommented:
(Get-ADTrust).target -filter *

The above is incorrect syntax. It should be (Get-ADTrust -Filter *).target

Will.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Uptime Legal SystemsAuthor Commented:
You're right, that was a 'code boo-boo' on my part.  Works as you listed.

I'll see if I can manipulate the distinguishedName to accomplish the per domain identification I need I guess.  Thanks for taking a look.
Will SzymkowskiSenior Solution ArchitectCommented:
Excellent,

Glad to help!

Will.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.