1

I have an array which holds the logon info of the users as objects. Here are the printings of that array:

PS> $Write-Out $data1
System.Object System.Object System.Object System.Object System.Object System.Object System.Object

PS> $data1 
ComputerName       User               Time                      Action                            
------------       ----               -------------------       ------
DC1                usr1               05/06/2013 11:51:35       logoff                         
DC1                usr1               05/06/2013 11:46:24       logon                         
DC1                usr1               05/06/2013 11:42:05       logoff
DC2                usr2               05/06/2013 11:44:08       logon
DC2                Administrator      05/06/2013 11:43:50       logoff
DC2                Administrator      05/06/2013 11:42:53       logon
DC2                Administrator      05/06/2013 11:40:27       logoff

I want to convert this array so that I can see logon and logoff times on the same line like this:

PS> $data2
ComputerName      User               Time LOGON           Time LOGOFF      
------------      ----               -------------------  -------------------                  
DC1               usr1               05/06/2013 11:46:24  05/06/2013 11:51:35                         
DC1               usr1                                    05/06/2013 11:42:05       
DC2               usr2               05/06/2013 11:44:08                        
DC2               Administrator      05/06/2013 11:42:53  05/06/2013 11:43:50
DC2               Administrator                           05/06/2013 11:40:27       

Can you help me with converting the $data1 array to $data2 array?

1
  • Can you give the Command line that generate $data1 ? Commented Jun 5, 2013 at 15:04

2 Answers 2

1

I think you should create your own object and assign the values from your $data1. You could do something like :

    $readableData1| Select ComputerName,User #Just an example to create a simple object
    $data1|%{ #Foreach line in $data1

    $readabledata.ComputerName = $_.ComputerName.ToString()
    $readabledata.User= $_.User.ToString()

    $array+= $readableData1
    }

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

1 Comment

Custom objects from $data1 would be a way;) The code doesn't work but I got the idea. Thanks!
0

Try this. It will give you the same $data2, based on your $data1, but might contain bugs.

$data2 = $data1 | Group-Object -Property ComputerName, User | foreach { # group
    foreach ($r in $_.Group) { # record
        if ($r.Action -eq "logon") {
            New-Object PSObject -Property @{"Time LOGOFF"=$off; "Time LOGON"=$r.Time; User=$r.User; ComputerName=$r.ComputerName};
            $off = $null;
            continue;
        } else {$off = $r.Time;}
    }
    if ($off) {New-Object PSObject -Property @{"Time LOGOFF"=$off; "Time LOGON"=""; User=$r.User; ComputerName=$r.ComputerName}; $off=$null;}
}

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.