I am trying to run the following Powershell script.
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. It really is a Array of ADComputer with one paramter called 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
string[] computerNames = computers.Select(computer => computer.name).ToArray();
but I want to learn how to do it in Powershell correctly.
You can use
or (probably the closest equivalent)
Note that to force the result to be an array (e.g. if you want access to its
Countproperty), you should surround the expression with@(). Otherwise the result might be an array or a single object.