0

测试 1 按预期工作。为什么测试 2 的工作方式不同?

$user="bsimms"
Write-Host "test 1"
$test1out=Get-ADUser  $user -Properties * | Select-Object -Property Name, samAccountName, EmployeeID
Write-Host "test1: $test1out"
$myitems="Name, samAccountName, EmployeeID"

Write-Host "test 2"
$test2out=Get-ADUser  $user -Properties * | Select-Object -Property $myitems
Write-Host "test2: $test2out"

Output C:\>powershell .\file.ps1 
test 1 
test1: @{Name=Bob Simms; samAccountName=bsimms; EmployeeID=0123450} 
test 2 
test2: @{Name, samAccountName, EmployeeID=Microsoft.ActiveDirectory.Management.ADPropertyValueCollection}
4

1 回答 1

0

您必须定义$myitems为属性名称数组。

PS > $myitems = @("Name", "samAccountName", "EmployeeID")
PS > Get-ADUser  $user -Properties * | Select-Object -Property $myitems
于 2020-11-07T17:00:01.253 回答