Can I pass Powershell Get-ADUser -Properties in a

2020-07-20 04:40发布

问题:

I'd like to do something like this, mainly for code readability:

$ADProperties = "extensionAttribute1,Department,Company,telephoneNumber"
$ADFilter = "{(objectClass -eq "user") -and (enabled -eq $True)}"

Get-ADUser -Filter $ADFilter -Properties $ADProperties

I get errors like: Get-Aduser : One or more properties are invalid.

Seems like I've seen a way to pass either the Filter or Properties somehow, but can't find it now.

EDIT - This works for -Properties:

$ADProperties = "EmployeeID","EmployeeNumber","extensionAttribute1","Department"

(Thanks Eris) but this throws error:

$ADProperties = extensionAttribute1,Department,Company,telephoneNumber 

Also, per Vasili, this works:

$str = @("foo","bar")

This works for -Filter

$ADFilter = {(objectClass -eq "user") -and (enabled -eq $True)}

Thanks everyone. MOB

回答1:

The Properties parameter takes a string[] (array) data type, as such, it is expecting something more like this:

@("extensionAttribute1","Department","Company","telephoneNumber")



回答2:

You can do it if you construct your command in a String Variable like this:

$cmd = Get-ADUser -Filter $ADFilter -Properties $ADProperties

And then invoke it

invoke-command $cmd