Solved

Need help in modifying an existing script

Posted on 2016-12-01
5
53 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 85

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 85

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

Are You Headed to Black Hat USA 2017?

Getting ready for Black Hat next week? Kick things off with the WatchGuard Badge Challenge and test your puzzle and cipher skills. Do you have what it takes to earn our limited edition Firebox Badge? Get started today - https://crimsonthorn.net

Question has a verified solution.

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

Had a business requirement to store the mobile number in an environmental variable. This is just a quick article on how this was done.
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Suggested Courses

617 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