jad0083
asked on
How to select first 2 objects inside a child-item with a where clause
Good Day,
Was wondering if there's a way to return just the first 2 objects under get-childitem that has a where clause, something like this?:
Was wondering if there's a way to return just the first 2 objects under get-childitem that has a where clause, something like this?:
foreach ($Replica in Get-ChildItem | where {$_.name.Substring(0,3) -ne $PReplica.substring(0,3)} | select -First 2) {}
ASKER
Just tried it, but it seems that its only returning the name property, whereas I need the actual object for the method to work:
Set-SqlAvailabilityReplica -AvailabilityMode AsynchronousCommit -FailoverMode Manual -InputObject $Replica
remove
| Select -ExpandProperty name
ASKER
I tried this:
SQL Server PowerShell provider error: Failed to read child items. [The pipeline has been stopped.]
When I remove the Select -First 2, it does work
Thanks,
--J
$Reps = Get-ChildItem | Foreach {$_ | where {$_.Name.Substring(0,3) -eq $PReplica.substring(0,3)}} | Select -First 2
but am now getting:SQL Server PowerShell provider error: Failed to read child items. [The pipeline has been stopped.]
When I remove the Select -First 2, it does work
Thanks,
--J
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
what do you get when you run
$Reps | GM
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
You're right, I was focused on putting everything in one line/command, and just directly running a foreach loop on it. I separated them out and is now working fine!
Open in new window