Link to home
Start Free TrialLog in
Avatar of jad0083
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?:

foreach ($Replica in Get-ChildItem | where {$_.name.Substring(0,3) -ne $PReplica.substring(0,3)} | select -First 2) {}

Open in new window

Avatar of K B
K B
Flag of United States of America image

Have a look at this method

Get-ChildItem | ForEach {
    $_ | Select -ExpandProperty name | where {$_.name -ne 'something'} 
}   | Select -First 2

Open in new window

Avatar of jad0083
jad0083

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

Open in new window

remove

| Select -ExpandProperty name 

Open in new window

Avatar of jad0083

ASKER

I tried this:
$Reps = Get-ChildItem | Foreach {$_ | where {$_.Name.Substring(0,3) -eq $PReplica.substring(0,3)}} | Select -First 2 

Open in new window

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
Avatar of K B
K B
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
what do you get when you run
$Reps | GM

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jad0083

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!