Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Powershell script to list user status based on CSV file not working due to apostrophe (Irish surname) !

Posted on 2016-08-08
3
Medium Priority
?
101 Views
Last Modified: 2016-08-08
Hi All,

Can anyone here please assist me in modifying the PowerShell script below so that it works for the Irish surname or any surname with the Apostrophe ?

Script:
$names = Import-CSV "C:\test\names.csv"
$outFile = "C:\test\status.csv"
if (Test-Path $outFile) {Remove-Item $outFile -Force}
Add-Content -Path $outFile -Value "name,samaccountname,enabled"

foreach ($name in $names)
{
    $result = Get-ADUser -filter "Surname -eq '$($name.surname)' -and GivenName -eq '$($name.firstname)'" -Properties * | select enabled,samaccountname,displayname
    Add-Content -Path $outFile -Value "$($result.displayname),$($result.samaccountname),$($result.enabled)"
}

Open in new window


The script is comparing the name of the people in the CSV file

CSV File format:
First Name, Last Name
Jason, Smith
April, O'Neill
Franky, O'Hara
Lisa,Auchterlonie-Crooks
...

Open in new window


with the Active Directory users and the result will be exported into another CSV file with Display Name,samAccountName,account status column.

Any help would be greatly appreciated.

Thanks.
0
Comment
[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
3 Comments
 
LVL 16

Assisted Solution

by:FOX
FOX earned 400 total points
ID: 41747326
The names.csv should contain the AD usernames instead of the first and last names. The header of the username should read Users. From the username we can now pull out whatever we want to do the comparison.

Import-csv "c:\temp\names.csv" | ForEach{Get-ADuser  $_.users -properties * | ft Surname,GivenName,samaccountname,enabled} | out-file "c:\temp\namesreport"
0
 
LVL 85

Accepted Solution

by:
oBdA earned 1600 total points
ID: 41747336
First, you need to correct your csv file so that the column names actually match the properties you're using in the script.
GivenName, Surname
Jason, Smith
April, O'Neill
Franky, O'Hara
Lisa,Auchterlonie-Crooks

Open in new window

The you can use an ldap filter:
$names = Import-CSV "C:\test\names.csv"
$outFile = "C:\test\status.csv"
if (Test-Path $outFile) {Remove-Item $outFile -Force}
Add-Content -Path $outFile -Value "name,samaccountname,enabled"

foreach ($name in $names) {
    $result = Get-ADUser -LDAPFilter "(&(sn=$($name.Surname))(givenName=$($name.GivenName)))" -Properties * | Select-Object enabled,samaccountname,displayname
    Add-Content -Path $outFile -Value "$($result.displayname),$($result.samaccountname),$($result.enabled)"
}

Open in new window


EDIT: corrected header --> "Surname" instead of "sn"
1
 
LVL 8

Author Closing Comment

by:Senior IT System Engineer
ID: 41748061
Thanks guys !
0

Featured Post

Are You Ready for GDPR?

With the GDPR deadline set for May 25, 2018, many organizations are ill-prepared due to uncertainty about the criteria for compliance. According to a recent WatchGuard survey, a staggering 37% of respondents don't even know if their organization needs to comply with GDPR. Do you?

Question has a verified solution.

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

A bad practice commonly found during an account life cycle is to set its password to an initial, insecure password. The Password Reset Tool was developed to make the password reset process easier and more secure.
Wouldn't it be nice if objects in Active Directory automatically moved into the correct Organizational Units? This is what AutoAD aims to do and as a plus, it automatically creates Sites, Subnets, and Organizational Units.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
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…

730 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