我正在使用 PowerShell 5.1 遍历活动目录中的计算机列表,并且我正在努力解决应该很简单的事情。
在下面的代码中,我在 foreach 循环中为每台计算机选择名称和描述。我应该如何在循环中单独引用名称和描述?
$OU=@('OU=...')
$computers = $OU | foreach {Get-ADComputer -filter {
Name -notlike 'xxx*'
-and Name -notlike 'yyy*'
} -searchbase $_ -Properties description | Select name, description }
foreach ($computer in $computers) {
$computerName = $computer | select -ExpandProperty name
$computerDescription = $computer | select -ExpandProperty description
Write-Host "Host: $computerName [$computerDescription]"
}
我能够使用它来工作select -ExpandProperty
,但这似乎不必要地复杂。$computer 变量包含如下键/值对:
@{name=ABCDE12345; description=Kiosk PC [Domain Separated]}
我尝试使用点表示法$computer.name $computer.description
,但点被忽略并视为文本。
我试过用谷歌搜索这个,但我是 PowerShell 的新手,不知道如何表达我的问题!