1
votes

I am trying to read line by line of a text file into the get-adgroup cmdlet to get a list of which users are a member of the groups from a txt file

Clear-Host;
$groups = "./desktop/groups.txt"
$arrayofgroups = Get-Content $groups 

foreach ($item in $arrayofgroups) {
    Get-ADGroup $item -properties * | select samaccountname, members
}

The text file looks like this:

"group 1"
"group 2"

The below code returns:

Get-ADGroup : Cannot bind parameter 'Identity' to the target. Exception setting "Identity": "Cannot validate argument on parameter: 'Identity'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again." At line:6 char:15 + {get-adgroup <<<< $item -properties * | select samaccountname, members} + CategoryInfo : WriteError: (:) [Get-ADGroup], ParameterBindingException + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.ActiveDirectory.Management.Commands.GetADGroup

However, when I loop through the array, it returns to the group name ok

Clear-Host;
$groups = "./desktop/groups.txt"
$arrayofgroups = Get-Content $groups 

foreach ($item in $arrayofgroups ) {
    $item
}

I have verfied that if you simply run the below it works fine.

get-adgroup "group 1" -properties * | select samaccountname, members

I'm new to PowerShell from VBscript, and am not sure why this doesn't work, any thoughts?

1
What does $arrayofgroups.count return? If those names are quoted in the file, you should remove the quotes. - mjolinor

1 Answers

2
votes

Set a PSBreakpoint on the line containing the Get-ADGroup command, within the foreach loop. In the debugger, type $Item to examine the value of the $Item variable. Also try typing $Item.Length to see how many characters long it is exactly. Make sure that your source text file doesn't have any extra, unnecessary whitespace in it.