1

I have a CSV list of usernames from all over my domain, and I'm trying to compare the list against a specific OU and send the matches to another file. I'm brand new to powershell, so after a good amount of research and comparing with other scripts I came up with this:

$users = Import-csv C:\Users\me\Desktop\'RSA.csv' 
$(ForEach ($user in $users) 
{
Get-AdUser -identity $user -SearchBase "ou=Sub,ou=Root,dc=My,dc=Domain,dc=Name" -Filter * 
}) |
Select-Object SamAccountName |
Export-CSV -Path C:\Users\me\Downloads\test\output.csv -NoTypeInformation 

When I run this I get the error "Get-ADUser : Cannot validate argument on parameter 'Identity'. The Identity property on the argument is null or empty." If I run without the -identity $user it just pulls everything. Any suggestions on how to make this work?

cd19
  • 27
  • 1
  • 6

1 Answers1

0

When you are calling Get-ADUser rather than giving it a string with just the user name you are passing in an object with a property called username. You could use Select-Object -ExpandProperty Username or reference the just property.

Import-Csv 'C:\Users\me\Desktop\RSA.csv' | 
    Where-Object {(!([string]::IsNullorWhiteSpace($_.UserName)))} |
    ForEach-Object {
        Get-ADUser -Identity $_.UserName
    } |
    Select-Object SamAccountName |
    Export-CSV -Path C:\Users\me\Downloads\test\output.csv -NoTypeInformation 

Notes: Changed to a ForEach-Object loop for readability since it looked like you where trying to mostly use the pipeline. Also added a test to skip blank lines or usernames that are whitespace/empty string. Removed the SearchBase and Filter since you are looking up based on identity can't use those in the command. As there isn't a parameter set that allows you to use all of them. Get-Help Get-ADUser shows the available parameter sets.

BenH
  • 9,766
  • 1
  • 22
  • 35
  • Is the $_.UserName referencing the column header or another attribute? I ran your edits through and got a new error, "Parameter set cannot be resolved using the specified named parameters". Does that imply that there is something wrong with the username list in my CSV? – cd19 Nov 07 '17 at 19:32
  • @cd19 Yes it is referencing that header, when you use `Import-Csv` an array of PowerShell objects are created with properties that are the column headers. – BenH Nov 07 '17 at 19:35
  • @cd19 No, pretty sure that you can't use `Identity` and `SearchBase` at the same time. Since Identity is Unique, SearchBase doesn't matter – BenH Nov 07 '17 at 19:36
  • I had seen somewhere that that could be an issue, so I tried taking out the Searchbase but it still can't resolve the parameter set. – cd19 Nov 07 '17 at 19:43
  • @cd19 You will need to remove `-filter` as well. – BenH Nov 07 '17 at 20:07