0

I have an array that I'm populating by some other means. It essentially contains this sample data;

PSComputerName              ResourceName   InstanceName       InDesiredState ConfigurationName StartDate          
--------------              ------------   ------------       -------------- ----------------- ---------          
server1.domain.local WindowsFeature Backup             True           BaseConfig        24/04/2018 14:31:23
server1.domain.local Registry       fDenyTSConnections True           BaseConfig        24/04/2018 14:31:23
server1.domain.local Registry       UserAuthentication True           BaseConfig        24/04/2018 14:31:23
server1.domain.local File           DscDemo            True           BaseConfig        24/04/2018 14:31:23
server5.domain.local WindowsFeature Backup             True           BaseConfig        24/04/2018 14:31:23
server5.domain.local Registry       fDenyTSConnections True           BaseConfig        24/04/2018 14:31:24
server5.domain.local Registry       UserAuthentication True           BaseConfig        24/04/2018 14:31:24

This is dynamic, and I'll have no idea when the array is populated what data will be inside. I need to split the array, into unique arrays (or PSObjects) based on the PSComputerName.

So in this case, I'd need to end up with two seperate objects/arrays for server1 and server5, bearing in mind there could be 6 servers or 80 servers. I can't think of the best way to approach this.

0

3 Answers 3

4

Why not just group the data and work on each group individually? Assuming $objects contains the data in the question

$objects | Group-Object pscomputername | Foreach-object{
    $_.Group
}

Each $_.Group will be a collection of entries associated to a unique computer. $_.Name would be the pscomputername of the current grouped item.

So you might need to have another inner loop to iterate of those results as well.

Sign up to request clarification or add additional context in comments.

Comments

0

Ignore, not sure why I was banging my head against a wall.

$Servers = $CSV.PSComputerName | select -Unique

ForEach($server in $Servers){

    $arr = $CSV | Where-Object {$_.PSComputerName -eq $server}

    Write-Host "Compliancy for $Server"
    $arr
    Write-Host "End compliancy for $server"

}

Comments

0

group-object. See "Example 4: Group event log events by ID"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.