Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Merging PSObjects in seperate arrays with Similar Values

$
0
0

Good Afternoon,

     I have been beating my head against a wall trying to figure this one out. I am trying to take 2 arrays with sperate information, one from Admin accounts and one from user accounts and combine the PS objects based off a similar value in each object.

So lets say in Array 1 i have object 1, it has a value of 12, I want to merge that PS Object with the object in Array 2 with the same value of 12.

In the script below i attempted to use the Group-object command and target then "Similar" Property. This is the property that i need to match up. However when i export the results i end up with Array 1 + Array 2 and no items are combines. I have verified that some values match but at this point i am lost. I would appreciate any help you can provide.

Here is my script.

$Users = get-aduser -filter * -SearchBase "User OU" -Properties employeenumber | select name,UserPrincipalName,Employeenumber
$Admins = get-aduser -filter * -SearchBase "Admin OU"  | select samaccountname,UserPrincipalName
$UArray = @()
$Aarray = @()




foreach($User in $Users){

    $EN = $User.UserPrincipalName
    $Group = $EN.Substring(0,10) 

$myObject = [PSCustomObject]@{
    #Component = $OU.Name
    UserName     = $User.name
    "User Account UPN" = $User.UserPrincipalName"User PIV Number" = $User.EmployeeNumber"Admin Account Name" = $Null"Admin Account UPN" = $Null"Similar" = $Group
    }

    $UArray+=$myObject

}


foreach($Admin in $Admins){
    $AN = $admin.UserPrincipalName
    $group = $AN.Substring(0,10)

$myObject = [PSCustomObject]@{
    #Component = $OU.Name
    UserName     = $Null
    "User Account UPN" = $Null"User PIV Number" = $Null"Admin Account Name" = $admin.SamAccountName"Admin Account UPN" = $admin.UserPrincipalName"Similar" = $Group
    }

    $AArray+=$myObject

}



$combin = $AArray + $UArray | Group-Object -Property Similar

foreach ($I in $combin){

$i.GROUP | EXPORT-CSV outfile -Append -NoTypeInformation



}

Thanks again


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>