Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Need help in modifying an existing script

Posted on 2016-12-01
5
Medium Priority
?
63 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:P S
  • 2
  • 2
5 Comments
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 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:P S
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:P S
ID: 41908555
Thanks oBdA. Script worked perfectly
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Microsoft Office 365 is a subscriptions based service which includes services like Exchange Online and Skype for business Online. These services integrate with Microsoft's online version of Active Directory called Azure Active Directory.
Transferring FSMO roles is done when an admin wants to split roles between certain Domain Controllers or the Domain Controller holding the Roles has been forcefully demoted using dcpromo / forceremoval
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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…

886 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