簡體   English   中英

Powershell相當於LINQ的Select命令?

[英]Powershell equivalent of LINQ's Select command?

我正在嘗試運行以下Powershell腳本。

import-module ActiveDirectory

$computers = Get-ADComputer -filter * -SearchBase "OU=myOU,DC=vw,DC=local" | select-object name

Invoke-Command -ComputerName $computers -ScriptBlock {gpupdate /target:Computer}

問題是$computers不像string[] -ComputerName期望的那樣。 它實際上是一個ADComputer數組,有一個名為name的參數。

# Get-ADComputer -filter * -SearchBase "OU=myOU,DC=vw,DC=local" | select-object name | Format-Custom

class ADComputer
{
  name = PC1
}

class ADComputer
{
  name = PC2
}

class ADComputer
{
  name = PC3
}

獲取名稱字符串數組的正確方法是什么? 如果我在C#,我知道它會

string[] computerNames = computers.Select(computer => computer.name).ToArray();

但我想學習如何正確地在Powershell中做到這一點。

您可以使用

Select-Object -ExpandProperty Name

或(可能是最接近的等價物)

ForEach-Object { $_.Name }

請注意,要強制結果為數組(例如,如果要訪問其Count屬性),則應使用@()包圍表達式。 否則結果可能是數組單個對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM