Need help with script

ssjaronx4ssjaronx4 Member Posts: 37 ■■□□□□□□□□
Hi,

I need to write a script which can read in usernames from a CSV file one at a time and search three different forests until it finds the account. I then need it to output the domain or the user and export them all to a file.

I'm not too good at writting this sort of thing but I do know a bit of powershell. I can get it to read in the names, search and output to a text file using the command:

import-csv c:\names.txt | foreach {$._name} | get-qaduser | fl UserPrincipalName >> c:\Output.txt


As this is searching just the domain I'm in, when it reads in a name from one of the other domains \ forests it cannot find it and fails. Does anyone know how I can get the above command to ignore failures and continue to the next name?

Also, if anyone could help me turn this into one script to search each domain using the -service parameter that would be great as even if I can get this to ignore any failures I'm still gunna gunna have to run it 3 times in order to get a full list of UPNs.

Any suggestions are very much appiciated.

Thanks guys,

Aron

Comments

  • HeroPsychoHeroPsycho Inactive Imported Users Posts: 1,940
    If somebody doesn't respond before then, be patient, I can help you out with this tonight.
    Good luck to all!
  • ssjaronx4ssjaronx4 Member Posts: 37 ■■□□□□□□□□
    That would be great if you could Hero.

    I've managed to find a way to do this but its not very efficient.

    What I've done is use DSQuery in a batch file. First it searches all 4 domains within my forest:

    DSQuery user -name XXXX -startnode forestroot >> c:\Output.txt

    Next it searches a 3 domains outside of my forest:

    DSQuery user -name XXXX -domain Domain.ForestA.com >> c:\Output.txt
    DSQuery user -name XXXX -domain Domain.ForestB.com >> c:\Output.txt
    DSQuery user -name XXXX -domain Domain.ForestC.com >> c:\Output.txt

    It runs all 4 searches for each of the 4000 user accounts meaning my work PC is currently running 16000 individual searches against domain controllers in separate sites across the UK in order to pull this data =P. To make it even worse I've had to use wildcards within each user name as some of the external domains have a mixture of names which have a dot inbetween the first and lastname and some that dont. Even though this should work I'd still like to find a better way of doing things like this in future.


    I'm resonably familiar with the basic commands and syntax used with powershell and EMS from when I did the Exchange 07 TS earlier last year however I havent had a chance to use it much since then. I know it is completely capable of doing the above in a far more efficient way I just cannot get the syntax right for reading for CSV files. The command I origionally posted would work if all of the users were in the same domain as me however as the whole reason for this script was to find the domain in which these users accounts reside its not too helpful. I tried many variations of the above, trying some stuff from the MS Press powershell book I have at work but could not get the damn thing right. What I really need is some examples of how to run commands which read from CSV files and seach multple domains so I can get my head around the syntax.


    I was thinking a script like the following would have been the most efficent way of doing this search :

    Import-CSV c:\file.csv
    Foreach user Search all domain in default forest

    If user found in forest then output domain to Output.txt and goto next user

    If not then search domain.forestA.com

    If user found in domain.forestA.com then output domain to Output.txt and goto next user

    If not then search domain.forestB.com

    If user found in domain.forestB.com then output domain to Output.txt and goto next user

    If not then search domain.forestC.com

    If user found in domain.forestC.com then output domain to Output.txt and goto next user

    If not then write "User not found" to Output1.txt and goto next user

    Do until end of file.


    This would mean the script would try searching the default forest and forestA which have the highest probability of having the accounts. With my DSQuery script Im running thousands of searches against the two lesser domains as I have no way of telling the script to stop if it find the user and move onto the next =P.
  • HeroPsychoHeroPsycho Inactive Imported Users Posts: 1,940
    That's what I was thinking as well.

    PowerShell Tutorial - Loop Processing | PowerShell Pro!

    You'd basically have an array of users through a get-content or something like that.

    Then you'd have a series of if statements, and tell PowerShell to tack them into a hash table with the properties you want. Finally, you'd export that hash table to a CSV.

    I can help once I get home from work. Too much to do while working. icon_wink.gif
    Good luck to all!
  • HeroPsychoHeroPsycho Inactive Imported Users Posts: 1,940
    Didn't have time last night. Probably can help sometime this weekend though.
    Good luck to all!
Sign In or Register to comment.