Hello all,
Trying to get specific details of users in a group using Powershell. The code I’m using is:
# Define variables
$adGroupMembers = @()
$adGroupMembersDetails = @()
$adGroupName = read-host "Please Enter Group Name"
#Get group members
$adGroupMembers = Get-ADGroupMember $adGroupName | Select SamAccountName
ForEach ($groupMember in $adGroupMembers) {
$groupMember
Get-ADUser $groupMember -Properties SamAccountName,Surname,GivenName,Mail,Enabled | Select SamAccountName,Surname,GivenName,Mail,Enabled | Export-CSV "C:\PowerShellScripts\Output\AD_User_Info.csv" -Append -notypeinformation
}
The error I receive is:
Get-ADUser : Cannot find an object with identity: 'samaccountname -eq ‘@{SamAccountName=SAMACCOUNTNAME}’ under:
‘DC=cs,DC=DOMAINNAME,DC=com’.
At C:\PowerShellScripts\Scripts\GetGroupMembers.ps1:12 char:2
-
Get-ADUser $filterstring # -Properties SamAccountName,Surname,Giv ... -
~~~~~~~~~~~~~~~~~~~~~~~~- CategoryInfo : ObjectNotFound: (samaccountname …tName=SAMACCOUNTNAME}:ADUser) [Get-ADUser], ADIdentityNotFo
undException - FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,M
icrosoft.ActiveDirectory.Management.Commands.GetADUser
- CategoryInfo : ObjectNotFound: (samaccountname …tName=SAMACCOUNTNAME}:ADUser) [Get-ADUser], ADIdentityNotFo
The last part works fine if pulling from a text file or a .csv, but not if it’s pulling from an array. I’ve read posts where Get-ADUser can’t pull from an array, but once it’s in a ForEach, it’s referencing a single variable pulled from the array, not the entire array, correct?
Thanks in advance for any help.
JR