Solved

Need help in modifying an existing script

Posted on 2016-12-01
5
36 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 83

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 83

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
This article will help you understand what HashTables are and how to use them in PowerShell.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

825 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