Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Monday, March 6, 2017 7:20 AM
I am writing a script to export all display names from userids which are present in the extensionattribute1 column in a csv file. The following script works for me
Import-Csv C:\Powershell\import.csv | ForEach-Object {Get-ADUser $_.extensionattribute1 -properties displayname| Select 'DisplayName’}
But The problem I am encountering is that there are some values in the csv in which the userid is not present. Instead, there is the number zero . So this script returns a "Cannot find and object with identity" error for them. This is obviously because the display name doesnot not exist for them. But I want to show a graceful output like 0 in instead of throwing the error. So I can export it into a csv.
So I tried using if-else in the below script but problem is when I try to export this it gives me an empty file.
import-module activedirectory
Import-Csv C:\Powershell\import.csv | ForEach-Object {
if ($_.extensionattribute2 -ne '0')
{
Get-ADUser $_.extensionattribute2 -properties displayname| Select 'DisplayName’
}
else {0}}
On the results pane i am getting this, which will definitely give error in exporting to csv
0
0
0
DisplayName
LastName, FirstName
LastName, FirstName
LastName, FirstName
0
0
0
LastName, FirstName
Please let me know how I can export it into a csv as a single column with the display names and 0's where there are no display names.
All replies (13)
Monday, March 6, 2017 9:56 AM ✅Answered | 1 vote
Import-Csv C:\Powershell\imports.csv | ForEach-Object {
[pscustomobject]@{
GroupName = $_.name
Approver1DisplayName = (Get-Aduser -Filter "SamAccountName -eq '$($_.extensionattribute1)'" -Properties DisplayName).DisplayName
Approver2DisplayName = (Get-Aduser -Filter "SamAccountName -eq '$($_.extensionattribute2)'" -Properties DisplayName).DisplayName
}
} |
Export-Csv output.Csv
\(ツ)_/
Monday, March 6, 2017 7:40 AM
I think we already showed you this:
Import-Csv C:\Powershell\import.csv |
ForEach-Object {
Get-ADUser -Filter "SamAccountName -eq '$($_.extensionattribute2)'" -properties displayname
} |
Select-Object 'DisplayName'
\(ツ)_/
Monday, March 6, 2017 7:51 AM
Hi jrv, thanks for assisting again. That command is just returning the displaynames. Whereas I want to display 0 where there is no display name and the display name where there is one. Reason I want it to appear like this is because they will have there corresponding groups names besides them in the row.
Monday, March 6, 2017 8:25 AM
Okay this one returns nothing where it doesnt find a display name. This is also okay as its not returning an error.
Import-Csv C:\Powershell\imports.csv | ForEach-Object {
[pscustomobject]@{
GroupName = $_.name
Approver1DisplayName = (Get-Aduser -Filter "SamAccountName -eq '$($_.extensionattribute1)'" -Properties DisplayName).DisplayName
Approver2DisplayName = (Get-Aduser -Filter "SamAccountName -eq '$($_.extensionattribute2)'" -Properties DisplayName).DisplayName
}
}
But how do I get them to appear as three columns so they can be exported to a csv? The three columns would be GroupName , Approver1DisplayName , Approver2DisplayName
Monday, March 6, 2017 9:22 AM
Approver2DisplayName = if(($s=Get-Aduser -Filter "SamAccountName -eq '$($_.extensionattribute2)'" -Properties DisplayName)){$s.DisplayName}else{'zero'}
\(ツ)_/
Monday, March 6, 2017 9:33 AM
That works jrv. Thank you for taking out time and helping with this. :)
One last thing. How can I get the results in three colums so I can pipe the export command infront of it and export it into a csv. I tried select-object but it gives me an error.
Monday, March 6, 2017 10:17 AM | 1 vote
Hi I know about the export-csv command. But when I run it, it creates a csv file with weird columns. I have attached pictures showing the csv file that this script created. As you can see its not the values that are returned by this script when i run it without the export-csv command.
Monday, March 6, 2017 10:42 AM
That is because you are trying to run it remotely. Don't run it remotely.
\(ツ)_/
Monday, March 6, 2017 11:02 AM
Excuse my lack of understanding, but what do you mean when you say running it remotely? I have the active directory module for Powershell installed an my computer. I create and run my scripts on my pc itself. The AD services are installed on a separate sever which is my DC. Will I have to run this script from the DC itself?
Monday, March 6, 2017 11:19 AM
The script I posted does not produce the output you showed. Perhaps you are using an old copy of PowerShell 2 on XP.
\(ツ)_/
Tuesday, March 7, 2017 2:54 AM
I am using powershell 2.0 on windows 7 64 bit. I can try updating to powershell 3 though.
Tuesday, March 7, 2017 3:24 AM
Update to PS 5 (WMF 5). Don't waste time with earlier versions.
PS 2 does not execute many things well in a pipeline.
\(ツ)_/
Tuesday, March 7, 2017 4:11 AM
I had already updated to PS3 before reading your last reply and tried running the script. It worked like a charm! The file gets exported correctly using PS3. I will go ahead and further upgrade to PS 5 now. Thanks for all the help jrv