FAC_IT
asked on
PowerShell need to pull user info from ad group
I have a script that will pull the users from a group. I also need a have the AD properties of the users. I will show you what I have now.
Get-ADGroupMember -Identity vpn | ForEach-Object {
Get-ADUser -Filter $name -SearchBase "ou=Users, dc=Domain, dc=com" -Properties * | Select-Object -Property Name,SamAccountName,Descri ption,Emai lAddress,L astLogonDa te,Manager ,Title,Dep artment,wh enCreated, Enabled,Or ganization | Sort-
Object -Property Name } | Export-Csv c:\temp\test1.csv
what am I doing wrong?
Get-ADGroupMember -Identity vpn | ForEach-Object {
Get-ADUser -Filter $name -SearchBase "ou=Users, dc=Domain, dc=com" -Properties * | Select-Object -Property Name,SamAccountName,Descri
Object -Property Name } | Export-Csv c:\temp\test1.csv
what am I doing wrong?
You are asking for the properties of $name - which is an undefined var. You'll have to use $_.Name instead.
Get-ADGroupMember -Identity vpn | ForEach-Object {
Get-ADUser -Filter $_.Name -SearchBase "ou=Users, dc=Domain, dc=com" -Properties * |
Select-Object -Property Name, SamAccountName, Description, EmailAddress, LastLogonDate, Manager, Title, Department, whenCreated, Enabled, Organization |
Sort-Object -Property Name
} | Export-Csv c:\temp\test1.csv
You can prevent from such mistakes if you runSet-StrictMode -version latest
as very first command of your prompt or script. That switches to a more strict policy about using undefined vars (and more), which is useful in particular to detect typos.
There is an update in the second code which I posted (I missed to remove the $name)...
Get-ADGroupMember -Identity vpn | Get-ADUser -SearchBase "ou=Users, dc=Domain, dc=com" -Properties * | Select-Object -Property Name,SamAccountName,Description,EmailAddress,LastLogonDate,Manager,Title,Department,whenCreated,Enabled,Organization | Sort-Object -Property Name } | Export-Csv c:\temp\test1.csv
The last approach (of Subsun, using the pipe directly) is the best one, as it just passes the object as necessary along the pipe. This often works as expected.
ASKER
This is the error I am getting when I use -filter $_.name: Get-ADUser : Error parsing query: 'Brandi Snead' Error Message: 'syntax error' at position: '8'.
At C:\users\user\Desktop\User s.ps1:3 char:1
+ Get-ADUser -Filter $_.name -SearchBase "ou=Users,dc=domain,dc=com " -Proper ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~
+ CategoryInfo : ParserError: (:) [Get-ADUser], ADFilterParsingException
+ FullyQualifiedErrorId : Error parsing query: 'Brandi Snead' Error Message: 'syntax error' at position: '8'.,Micr
osoft.ActiveDirectory.Mana gement.Com mands.GetA DUser
This what I get when I use $_.Samaccountname
Get-ADUser : A positional parameter cannot be found that accepts argument 'burbina'.
At C:\users\user\Desktop\User s.ps1:3 char:1
+ Get-ADUser $_.Samaccountname -SearchBase "ou=Users,dc=Domain,dc=com " -Pr ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-ADUser], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFoun d,Microsof t.ActiveDi rectory.Ma nagement.C ommands.Ge tADUser
At C:\users\user\Desktop\User
+ Get-ADUser -Filter $_.name -SearchBase "ou=Users,dc=domain,dc=com
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ParserError: (:) [Get-ADUser], ADFilterParsingException
+ FullyQualifiedErrorId : Error parsing query: 'Brandi Snead' Error Message: 'syntax error' at position: '8'.,Micr
osoft.ActiveDirectory.Mana
This what I get when I use $_.Samaccountname
Get-ADUser : A positional parameter cannot be found that accepts argument 'burbina'.
At C:\users\user\Desktop\User
+ Get-ADUser $_.Samaccountname -SearchBase "ou=Users,dc=Domain,dc=com
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-ADUser], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFoun
Use the syntax as shown in http:#a39242052 .
ASKER
Qlemo,
When I run it like you want in a39242052 it is asking for a Filter. I try a * and it came back with everything in the users OU.
When I run it like you want in a39242052 it is asking for a Filter. I try a * and it came back with everything in the users OU.
Try..
Get-ADGroupMember -Identity vpn | ?{$_.distinguishedName -like "*ou=Users,dc=Domain,dc=com"} | Get-ADUser -Properties * | Select-Object -Property Name,SamAccountName,Description,EmailAddress,LastLogonDate,Manager,Title,Department,whenCreated,Enabled,Organization | Sort-Object -Property Name | Export-Csv c:\temp\test1.csv
ASKER
I get no errors and it creates a file but there is nothing in it. What does the ? do?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window
Or
Open in new window