Solved

Help with script to get OU of specific computer objects (from CSV)

Posted on 2016-07-25
2
56 Views
Last Modified: 2016-07-25
I am trying to display the OU of a set of computer objects in Active Directory(from CSV)
I wrote this up and, it doesn't do anything, seems like a simple thing....
import-module activedirectory
$cred = Get-Credential
import-CSV C:\Users\ME\Desktop\scripts\servers.csv |
foreach-object {
Get-ADComputer $_.name  -Properties * | FT name, Distinguishedname
}

Open in new window

This works fine, if I give it  a server name "Get-ADComputer servername  -Properties * | FT name, Distinguishedname"
0
Comment
Question by:SquigglyMonkey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 41727867
Should work (sort of), so your input file is probably incorrect.
You'll need a "real" csv with a header line, a column "Name", and only the host name in that column, not the FQDN.
As far as "sort of" is concerned:
- you're not using the credentials you're querying
- there's no need to retrieve all properties if you're filtering most of them out
- Format-Table should be at the very end, not inside the ForEach loop
Import-Module ActiveDirectory
$cred = Get-Credential
Import-CSV C:\Users\ME\Desktop\scripts\servers.csv |
	ForEach-Object {
		Get-ADComputer $_.name -Credential $cred
	} |
	Format-Table Name, Distinguishedname

Open in new window

0
 
LVL 1

Author Closing Comment

by:SquigglyMonkey
ID: 41727876
Derp..... i need more coffee.
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question