Solved

Need help in modifying an existing script

Posted on 2016-12-01
5
47 Views
Last Modified: 2016-12-01
I've a list of Samaccountname and request is to get firstname, lastname, emailaddress. I can do that but at times, out of the list, few samaccountnames are not in AD. I want a script to write down in output CSV file in front of the ID which is not available in AD as "This ID is not available".

I am using below script.

GC C:\input.txt | % {Get-Aduser $_ -Pr * | Select GivenName,Surname,displayname,emailaddress} | Export-csv C:\report.csv -nti


let me know if anything else is required.
0
Comment
Question by:A D
[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
  • 2
5 Comments
 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 41908372
This should do the trick; I took the liberty of adding a boolean column 'Available':
$ADProperties = 'SamAccountName', 'GivenName', 'Surname', 'DisplayName', 'EmailAddress'
$Properties = @('Available') + $ADProperties
Get-Content -Path 'C:\input.txt' | ForEach-Object {
	If ($ADUser = Get-ADUser -Filter "SamAccountName -eq '$($_)'" -Properties $ADProperties | Select-Object -Property $Properties) {
		$ADUser.Available = $True
	} Else {
		$ADUser = '' | Select-Object -Property $Properties
		$ADUser.SamAccountName = $_
		$ADUser.Available = $False
	}
	$ADUser
} | Export-csv -Path C:\report.csv -nti

Open in new window

Note: It's bad style to bother AD with returning all available user properties, only to then filter out most of them in PS.
1
 

Author Comment

by:A D
ID: 41908383
Thanks oBdA. It worked perfectly. I agree to not to bother AD with all the properties when you only need  few in output. I'll make my script better in future. Thanks again.
0
 
LVL 84

Expert Comment

by:oBdA
ID: 41908433
A D,
you should accept the answer that helped, not your own comment.
0
 

Author Closing Comment

by:A D
ID: 41908555
Thanks oBdA. Script worked perfectly
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

756 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