Jump to content

Recommended Posts

Posted

Hi All,

I'm pretty new to Powershell and I find myself struggling to do something that is probably pretty basic.

I have a list of student names and email addresses on a CSV.

I need to use that information to extract the sam account names from AD so I can then run another script to set all the students' passwords.

The csv has headers

Name,Year,Group,Firstname,Surname,Email

I've tried this:

Import-csv C:\list.csv |foreach-object {$mail = $_."email"get-aduser -filter {mail -eq $mail} select-object name, samaccountname, mail |export-csv c:\logonids.csv -Append -Encoding UTF8}

 

Whenever I run it I get a list of each username appear on screen. About halfway thru this pops up:

get-aduser : The search filter cannot be recognizedAt line:4 char:1+ get-aduser -filter {mail -eq $mail}+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Get-ADUser], ADException + FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADUser

The output file is created but, needless to say, is empty.

I really don't know what I am doing :)

 

Any help??

Posted

I use this script to export a list of users from a particular OU to compare once to another list, might be of some use.

 

##################################################################
# AUTHOR  : MatthewL
# COMMENT : Exports users from within the Root OU to a CSV file to enable filter from HR system
##################################################################

# Define the location to export the CSV file to
$path = Split-Path -parent "C:\ADExport\*.*"

# Create a veriable for the date and time stamp for the file name
$LogDate = get-date -f yyyyMMdd-HHmm
$csvfile = $path + "\$logDate.csv"

# Working
#Get-ADUser -SearchBase "OU=Root,DC=domain,DC=org,DC=uk" -Filter * -Properties Enabled, EmailAddress, EmployeeID, EmployeeNumber, Surname, GivenName, Title | Select Enabled, EmailAddress, EmployeeID, EmployeeNumber, Surname, GivenName, Title |

# Searches Root OU and selects certain properties from all users including weather account is disabled
Get-ADUser -SearchBase "OU=Root,DC=domain,DC=org,DC=uk" -Filter * -Properties Enabled, EmailAddress, EmployeeID, EmployeeNumber, Surname, GivenName, Title, sAMAccountName |

# Selects objects from above and renames them into a friendly format
Select-Object @{Label = "Account Status";Expression = {$_.Enabled}},
@{Label = "UserID";Expression = {$_.EmailAddress}},
@{Label = "Employee Number";Expression = {$_.EmployeeID}},
@{Label = "NI Number";Expression = {$_.EmployeeNumber}},
@{Label = "Last Name";Expression = {$_.Surname}},
@{Label = "First Name";Expression = {$_.GivenName}},
@{Label = "Title";Expression = {$_.Title}} |

# Exports to a CSV based on the location and variables above
Export-CSV -Path $csvfile -NoTypeInformation

  • Thanks 1
Posted

It seems the code you have there has had some spaces remove or altered, could you type [CODE] then paste your code and then type [/code] it should make text appear like this and easier to review what the actual issue is. :)

 

This is a code box
Is
A
Code
Box

Posted

Don't know if this will help, but I found this in my "Yearly Update" folder. Sorry - no source to credit, but probs here!

Looks like I had the UserName and wanted to set the Password. If your students have the same format usernames you could use Excel to create a UserName column. The csv it referred to is long gone so I can't check what was there.

 

 

foreach($user in (import-Csv \\PathToCSV\Names.csv))
{
   Write-Host "Setting Password for $($User.username)"
   $ds = new-Object System.DirectoryServices.DirectorySearcher([ADSI]"","(&(objectcategory=user)(sAMAccountName=$($user.username)))")
   $usr = ($ds.Findone()).GetDirectoryEntry()
   $usr.SetPassword($user.password)
   $usr.SetInfo()
   Read-Host -Prompt "Press Enter key to continue"

 

 

P.S. If your school won't spring for a paid tool, the Import Tool here: https://www.solarwinds.com/free-tools/active-directory-admin-tools-bundle/registration saves me a lot of time each year.

Posted (edited)
Import-csv C:\Year12Sept2020.csv |
foreach-object {
$mail = $_."email"get-aduser -filter {mail -eq $mail}
select-object name, samaccountname, mail |
export-csv c:\year12logonids.csv -Append
}

Edited by Tinribs
Posted
Import-csv C:\Year12Sept2020.csv |
foreach-object {
$mail = $_."email"get-aduser -filter {mail -eq $mail}
select-object name, samaccountname, mail |
export-csv c:\year12logonids.csv -Append
}

 

Try the code below, although you may need to put the files in a folder on the root of c:\ as windows 10 restricts writing to the drive

 


Import-csv C:\\Year12Sept2020.csv | foreach-object {

   $mail = $_.email

   get-aduser -filter {mail -eq $mail} -Properties mail | select-object name, samaccountname, mail | export-csv c:\\year12logonids.csv -Append

   }

  • Thanks 1
Posted

Thanks heaps! That has done what I need. Just a basic list of usernames that I can use to import a bulk load of passwords to.

Very much appreciated

Posted
Import-csv C:\Year12Sept2020.csv |
foreach-object {
$mail = $_."email"get-aduser -filter {mail -eq $mail}
select-object name, samaccountname, mail |
export-csv c:\year12logonids.csv -Append
}

 

Looks like the syntax should be

 

Import-csv C:\Year12Sept2020.csv | foreach-object

{

$mail = $_."email"

get-aduser -filter {mail -eq $mail} | Select-Object name, samaccountname, mail | export-csv c:\year12logonids.csv -Append

}

 

But... on run mine comes back with

 

#TYPE Selected.Microsoft.ActiveDirectory.Management.ADUser

"name","samaccountname","mail"

"Test Account","Test",

 

In a new text file... bit strange as the email address is there as it's used to find the account in the first place...

 

Import-csv C:\Year12Sept2020.csv | foreach-object
{
$mail = $_."email"
$user = get-aduser -filter {mail -eq $mail}
$name = $user.name
$samname = $user.SamAccountName
Add-Content c:\year12logonids.csv "$name,$samname,$mail"
}

 

That should work though, give that a go. :)

  • Thanks 1
  • 1 month later...
Posted
Thanks I was looking for this code and I found this, I usually use terminal on Linux, but right now it's not available to me.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...