Solved

Need help in modifying an existing script

Posted on 2016-12-01
5
8 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
  • 2
  • 2
5 Comments
 
LVL 82

Accepted Solution

by:
oBdA earned 500 total points
Comment Utility
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.
0
 

Author Comment

by:A D
Comment Utility
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 82

Expert Comment

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

Author Closing Comment

by:A D
Comment Utility
Thanks oBdA. Script worked perfectly
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now