I've got an Arraylist with custom objects and want to find all objects where "Jobname" is identical. Group-Object does not work the way I wanted it because it just counts all properties (10). I'm even starting to think that I'm using the Arraylist wrong. The creation of the objects in the code is just for testing, in reality I'm using a loop to fill my Array.
$global:BackupJobObjectArray = New-Object System.Collections.ArrayList
function AddBackupJobFoldertoArray ($jobservername, $backupjobfolder, $backupjobname, $jobmandant, $size, $sizeGB, $sizeTB, $BackupServer, $vbkcount, $lastmodifiedfile)
{
$BackupJobObject = New-Object System.Object
$BackupJobObject | Add-Member -MemberType NoteProperty -Name "Jobname" -Value $backupjobname
$BackupJobObject | Add-Member -MemberType NoteProperty -Name "Mandant" -Value $jobmandant
$BackupJobObject | Add-Member -MemberType NoteProperty -Name "Servername" -Value $jobservername
$BackupJobObject | Add-Member -MemberType NoteProperty -Name "Filepath" -Value $backupjobfolder
$BackupJobObject | Add-Member -MemberType NoteProperty -Name "Size" -Value $size
$BackupJobObject | Add-Member -MemberType NoteProperty -Name "SizeGB" -Value $sizeGB
$BackupJobObject | Add-Member -MemberType NoteProperty -Name "SizeTB" -Value $sizeTB
$BackupJobObject | Add-Member -MemberType NoteProperty -Name "Backupserver" -Value $BackupServer
$BackupJobObject | Add-Member -MemberType NoteProperty -Name "VBKCount" -Value $vbkcount
$BackupJobObject | Add-Member -MemberType NoteProperty -Name "LastModified" -Value $lastmodifiedfile
$global:BackupJobObjectArray.Add($BackupJobObject) | Out-Null
}
AddBackupJobFoldertoArray "server01" "folder01" "jobname01" "mandant01" "1000" "100" "10" "backupserver01" "1" "01.01.2001"
AddBackupJobFoldertoArray "server02" "folder02" "jobname02" "mandant01" "1000" "100" "10" "backupserver01" "1" "01.01.2001"
AddBackupJobFoldertoArray "server03" "folder03" "jobname03" "mandant01" "1000" "100" "10" "backupserver01" "1" "01.01.2001"
AddBackupJobFoldertoArray "server04" "folder04" "jobname04" "mandant01" "1000" "100" "10" "backupserver01" "1" "01.01.2001"
AddBackupJobFoldertoArray "server05" "folder05" "jobname05" "mandant01" "1000" "100" "10" "backupserver01" "1" "01.01.2001"
AddBackupJobFoldertoArray "server06" "folder06" "jobname06" "mandant02" "1000" "100" "10" "backupserver01" "1" "01.01.2001"
AddBackupJobFoldertoArray "server07" "folder07" "jobname07" "mandant02" "1000" "100" "10" "backupserver01" "1" "01.01.2001"
AddBackupJobFoldertoArray "server08" "folder08" "jobname08" "mandant02" "1000" "100" "10" "backupserver01" "1" "01.01.2001"
AddBackupJobFoldertoArray "server09" "folder09" "jobname09" "mandant02" "1000" "100" "10" "backupserver01" "1" "01.01.2001"
AddBackupJobFoldertoArray "server10" "folder10" "jobname10" "mandant02" "1000" "100" "10" "backupserver01" "1" "01.01.2001"
AddBackupJobFoldertoArray "server21" "folder16" "jobname16" "mandant03" "1000" "100" "10" "backupserver03" "1" "01.01.2001"
AddBackupJobFoldertoArray "server22" "folder10" "jobname16" "mandant03" "1000" "100" "10" "backupserver03" "1" "01.01.2001"
AddBackupJobFoldertoArray "server22" "folder03" "jobname16" "mandant03" "1000" "100" "10" "backupserver03" "1" "01.01.2001"
AddBackupJobFoldertoArray "server11" "folder11" "jobname11" "mandant01" "1000" "100" "10" "backupserver02" "1" "01.01.2001"
AddBackupJobFoldertoArray "server12" "folder12" "jobname12" "mandant01" "1000" "100" "10" "backupserver02" "1" "01.01.2001"
AddBackupJobFoldertoArray "server13" "folder13" "jobname13" "mandant01" "1000" "100" "10" "backupserver02" "1" "01.01.2001"
AddBackupJobFoldertoArray "server14" "folder14" "jobname14" "mandant01" "1000" "100" "10" "backupserver02" "1" "01.01.2001"
AddBackupJobFoldertoArray "server15" "folder15" "jobname15" "mandant01" "1000" "100" "10" "backupserver02" "1" "01.01.2001"
AddBackupJobFoldertoArray "server16" "folder16" "jobname16" "mandant02" "1000" "100" "10" "backupserver02" "1" "01.01.2001"
AddBackupJobFoldertoArray "server17" "folder17" "jobname17" "mandant02" "1000" "100" "10" "backupserver02" "1" "01.01.2001"
AddBackupJobFoldertoArray "server18" "folder18" "jobname18" "mandant02" "1000" "100" "10" "backupserver02" "1" "01.01.2001"
AddBackupJobFoldertoArray "server19" "folder19" "jobname19" "mandant02" "1000" "100" "10" "backupserver02" "1" "01.01.2001"
AddBackupJobFoldertoArray "server20" "folder20" "jobname20" "mandant02" "1000" "100" "10" "backupserver02" "1" "01.01.2001"
$global:BackupJobObjectArray | ForEach-Object {$_.psobject.Properties | Group-Object { $_.Jobname } | Format-Table}
CodePudding user response:
Use Script-scoping
instead of Global as that should be more than enough.
Then use $script:BackupJobObjectArray
only inside the fuction, because outside the list is known anyway.
Instead of all those Add-Member
lines, you can simply use [PsCustomObject]
and add that straight away to the list.
Then finally, if I understand your question correctly, you woud like to get a table formatted list of the duplicate objects based on the JobName, and this you can do like below
$BackupJobObjectArray = [System.Collections.Generic.List[object]]::new()
function AddBackupJobFoldertoArray {
param(
[string]$jobservername,
[string]$backupjobfolder,
[string]$backupjobname,
[string]$jobmandant,
[int64]$size,
[int64]$sizeGB,
[int64]$sizeTB,
[string]$BackupServer,
[int]$vbkcount,
[string]$lastmodifiedfile
)
$script:BackupJobObjectArray.Add([PsCustomObject]@{
Jobname = $backupjobname
Mandant = $jobmandant
Servername = $jobservername
Filepath = $backupjobfolder
Size = $size
SizeGB = $sizeGB
SizeTB = $sizeTB
Backupserver = $BackupServer
VBKCount = $vbkcount
LastModified = $lastmodifiedfile
})
}
AddBackupJobFoldertoArray "server01" "folder01" "jobname01" "mandant01" 1000 100 10 "backupserver01" 1 "01.01.2001"
AddBackupJobFoldertoArray "server02" "folder02" "jobname02" "mandant01" 1000 100 10 "backupserver01" 1 "01.01.2001"
AddBackupJobFoldertoArray "server03" "folder03" "jobname03" "mandant01" 1000 100 10 "backupserver01" 1 "01.01.2001"
AddBackupJobFoldertoArray "server04" "folder04" "jobname04" "mandant01" 1000 100 10 "backupserver01" 1 "01.01.2001"
AddBackupJobFoldertoArray "server05" "folder05" "jobname05" "mandant01" 1000 100 10 "backupserver01" 1 "01.01.2001"
AddBackupJobFoldertoArray "server06" "folder06" "jobname06" "mandant02" 1000 100 10 "backupserver01" 1 "01.01.2001"
AddBackupJobFoldertoArray "server07" "folder07" "jobname07" "mandant02" 1000 100 10 "backupserver01" 1 "01.01.2001"
AddBackupJobFoldertoArray "server08" "folder08" "jobname08" "mandant02" 1000 100 10 "backupserver01" 1 "01.01.2001"
AddBackupJobFoldertoArray "server09" "folder09" "jobname09" "mandant02" 1000 100 10 "backupserver01" 1 "01.01.2001"
AddBackupJobFoldertoArray "server10" "folder10" "jobname10" "mandant02" 1000 100 10 "backupserver01" 1 "01.01.2001"
AddBackupJobFoldertoArray "server21" "folder16" "jobname16" "mandant03" 1000 100 10 "backupserver03" 1 "01.01.2001"
AddBackupJobFoldertoArray "server22" "folder10" "jobname16" "mandant03" 1000 100 10 "backupserver03" 1 "01.01.2001"
AddBackupJobFoldertoArray "server22" "folder03" "jobname16" "mandant03" 1000 100 10 "backupserver03" 1 "01.01.2001"
AddBackupJobFoldertoArray "server11" "folder11" "jobname11" "mandant01" 1000 100 10 "backupserver02" 1 "01.01.2001"
AddBackupJobFoldertoArray "server12" "folder12" "jobname12" "mandant01" 1000 100 10 "backupserver02" 1 "01.01.2001"
AddBackupJobFoldertoArray "server13" "folder13" "jobname13" "mandant01" 1000 100 10 "backupserver02" 1 "01.01.2001"
AddBackupJobFoldertoArray "server14" "folder14" "jobname14" "mandant01" 1000 100 10 "backupserver02" 1 "01.01.2001"
AddBackupJobFoldertoArray "server15" "folder15" "jobname15" "mandant01" 1000 100 10 "backupserver02" 1 "01.01.2001"
AddBackupJobFoldertoArray "server16" "folder16" "jobname16" "mandant02" 1000 100 10 "backupserver02" 1 "01.01.2001"
AddBackupJobFoldertoArray "server17" "folder17" "jobname17" "mandant02" 1000 100 10 "backupserver02" 1 "01.01.2001"
AddBackupJobFoldertoArray "server18" "folder18" "jobname18" "mandant02" 1000 100 10 "backupserver02" 1 "01.01.2001"
AddBackupJobFoldertoArray "server19" "folder19" "jobname19" "mandant02" 1000 100 10 "backupserver02" 1 "01.01.2001"
AddBackupJobFoldertoArray "server20" "folder20" "jobname20" "mandant02" 1000 100 10 "backupserver02" 1 "01.01.2001"
($BackupJobObjectArray | Group-Object Jobname | Where-Object { $_.Count -gt 1 }).Group | Format-Table
Output:
Jobname Mandant Servername Filepath Size SizeGB SizeTB Backupserver VBKCount LastModified
------- ------- ---------- -------- ---- ------ ------ ------------ -------- ------------
jobname16 mandant03 server21 folder16 1000 100 10 backupserver03 1 01.01.2001
jobname16 mandant03 server22 folder10 1000 100 10 backupserver03 1 01.01.2001
jobname16 mandant03 server22 folder03 1000 100 10 backupserver03 1 01.01.2001
jobname16 mandant02 server16 folder16 1000 100 10 backupserver02 1 01.01.2001
Personally I would go for a List object, but an ArrayList would also do.