0

I'm developing a script that connects to Azure AD and extracts failed login for any user so I'm probably going to get more than a row for user. I have this code in a foreach (there is anything after part of code):

$ConvertedOutput  | Select-Object   @{Label="UserId"; Expression={$_.UserId}},
@{Label="CreationTime"; Expression={$_.CreationTime}},
@{Label="UserAgent"; Expression={$FinalOutput[0]."Value"}},
@{Label="Operation"; Expression={$_.Operation}},
@{Label="LogonError"; Expression={$_.LogonError}},
@{Label="ClientIP"; Expression={$_.ClientIP}} | Format-Table

How can I prevent from forming multiple tables? I only wanted the table for the first record, then additional records under the same table. Thanks here is the output

2 Answers2

0
# Create an empty array to store your results in
[array]$results = @()

# This is your existing loop
foreach (...) {
    ...
    $ConvertedOutput = <your existing code>
    ...
    # Append your object to the results array
    $results += $ConvertedOutput | Select-Object ... <your existing code>
}

# Now your results object contains all of the values from inside your loop
# So let's display that!
Write-Output $results
gvee
  • 16,732
  • 35
  • 50
0

Welcome to stack overflow. I general, it is recommended to supply sample (fake) data in text format rather then pictures (of just headers), the makes life easier for us to answer your question.

Reading your code part, it doesn't add much value unless you planned to further extend it. All expressions generate the same keys and values as the original object, meaning that you can simplify this to just: Select-Object UserId, CreationTime, UserAgent, Operation, LogonError, LogonError, ClientIP or even: Select-Object * (or just omit the complete Select-Object), if you do not select a column subset.

With regards to your question, By default PowerShell normally concatenates the output by it self, meaning that there is probably something else (that you are not sharing, e.g. a Write-Host command) that causes the data to be released preliminary from the pipeline.
Let me show this with fictive object lists created on the fly from three separate CSV lists:

$Result = &{
ConvertFrom-Csv @'
Id,FirstName,LastName
1,Nancy,Davolio
2,Andrew,Fuller
3,Janet,Leveling
'@

ConvertFrom-Csv @'
Id,FirstName,LastName
4,Margaret,Peacock
5,Steven,Buchanan
6,Michael,Suyama
'@

ConvertFrom-Csv @'
Id,FirstName,LastName
7,Robert,King
8,Laura,Callahan
9,Anne,Dodsworth
'@
}

With the above command, $Result contains the following data:

PS C:\> $Result

Id FirstName LastName
-- --------- --------
1  Nancy     Davolio
2  Andrew    Fuller
3  Janet     Leveling
4  Margaret  Peacock
5  Steven    Buchanan
6  Michael   Suyama
7  Robert    King
8  Laura     Callahan
9  Anne      Dodsworth

One important thing to mention here, is that the columns of the three list should be have the same columns (or at least the first row should contain all expected columns), see: Not all properties displayed

If this doesn't help you further, I recommend you to add you more details to your question.

iRon
  • 20,463
  • 10
  • 53
  • 79