I am trying to get the Server OU location from a list of server. The code I wrote works but dose not seem to be very efficient. Is there a better way of coding this?
Thanks in advance.
That should be faster overall, but you do need to be aware of how much time it takes for a single query to run. AD queries with very large result sets sometimes wind up timing out, but that shouldn’t become a problem unless you have many thousands of computers in that text file.
That turns an array of strings into a single string, separated by ’ -or '. In this case, we’re building a filter string such as SamAccountName -eq ‘Computer1$’ -or SamAccountName -eq ‘Computer2$’ -or SamAccountName -eq ‘Computer3$’ (where Computer1, Computer2 and Computer3 are the strings that came out of your text file.)
One more quick question Dave, goggled for it but couldn’t find anything on it . I know that $_ feeds the current object in the pipeline but why is every computername appended with the $ ?
That’s an Active Directory thing, not specific to PowerShell. The SamAccountNames of computer accounts (as well as domain trust accounts, managed service accounts, and possibly some other types I’m forgetting) always end with a $ symbol.