Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Issue with foreach and Get-ADUser -Filter

$
0
0

I think I'm having an issue with variable type handling.  Can someone explain why the first block of code works, but the 2nd doesn't? 

$studentsList = ipCSV C:\studentsSummer.csv 
foreach ($student in $studentsList) {
    $emailAddress = $null
    $emailAddress = $student.emailacct
    $tmpStudent = $null
    $tmpStudent = Get-ADUser -Filter {mail -eq $emailAddress}
    Add-ADGroupMember $student.crn -Members $tmpStudent
    }


$studentsList = ipCSV C:\studentsSummer.csv 
foreach ($student in $studentsList) {
    $tmpStudent = $null
    $tmpStudent = Get-ADUser -Filter {mail -eq $student.emailacct}
    Add-ADGroupMember $student.crn -Members $tmpStudent
    }

Assume the CSV is like:

EMAILACCT,CRN,COURSE
Kimberly.Anne@fake.biz,CRN2,5
Jim.Abele@fake.biz,CRN1,5
Paul.Atterman@fake.biz,CRN1

I've tried several varatoins with single and double quotes as well as ([string]$student.emailacct) but i can't seem to crack this one today.

EDIT:

oops, the error would have been helpful:

Get-ADUser : Property: 'emailacct' not found in object of type: 'System.Management.Automation.PSCustomObject'.
At line:3 char:29+     $tmpStudent = Get-ADUser <<<<  -Filter {mail -eq $student.emailacct}+ CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException+ FullyQualifiedErrorId : Property: 'emailacct' not found in object of type: 'System.Management.Automation.PSCusto
   mObject'.,Microsoft.ActiveDirectory.Management.Commands.GetADUser

Add-ADGroupMember : Cannot validate argument on parameter 'Members'. The argument is null or empty. Supply an argument
that is not null or empty and then try the command again.
At line:4 char:44
+     Add-ADGroupMember $student.crn -Members <<<<  $tmpStudent+ CategoryInfo          : InvalidData: (:) [Add-ADGroupMember], ParameterBindingValidationException+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.AddADGrou
   pMember



Mike Crowley | MVP
My Blog -- Planet Technologies




Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>