[英]Powershell equivalent of LINQ's Select command?
I am trying to run the following Powershell script. 我正在尝试运行以下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}
The issue is $computers
is not a string[]
like -ComputerName
expects. 问题是
$computers
不像string[]
-ComputerName
期望的那样。 It really is a Array of ADComputer
with one paramter called name. 它实际上是一个
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
}
What is the correct way to get a array of strings for the names? 获取名称字符串数组的正确方法是什么? If I was in C# I know it would be
如果我在C#,我知道它会
string[] computerNames = computers.Select(computer => computer.name).ToArray();
but I want to learn how to do it in Powershell correctly. 但我想学习如何正确地在Powershell中做到这一点。
You can use 您可以使用
Select-Object -ExpandProperty Name
or (probably the closest equivalent) 或(可能是最接近的等价物)
ForEach-Object { $_.Name }
Note that to force the result to be an array (eg if you want access to its Count
property), you should surround the expression with @()
. 请注意,要强制结果为数组(例如,如果要访问其
Count
属性),则应使用@()
包围表达式。 Otherwise the result might be an array or a single object. 否则结果可能是数组或单个对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.