Joseph Hill
Joseph Hill

Reputation: 1

Add Property to Array#1 and populate with variables from Array#2

Hello I have been having trouble populating a property of an array I have created.

I start by running a query for logged on users from with in a specific list of computers. I take the output and pull a few properties (UserName, ComputerName, Logontime) and pump this into an array.

With this list of logged on users I take the logontime and calculate the duration using new-timespan

Unfortunately this populates only the last time duration calculated instead of placing each value into new duration property I created.

I have read lots of other articles and tried many configurations but nothing seems to work. I am sure I am missing something simple.

Here is what I running

Clear-History
Clear-Host

$endDate = (Get-Date -Format g)
$computers = Get-Content "\\<Server>\D$\<Folder1>\<Folder2>\ComputerList.txt"
$dat = $null
$loggedonuserArray = @()
$dat = @()

$loggedonuserArray = .\get-loggedonuser.ps1 -ComputerName $computers |
    Where-Object -Property "SessionName" -EQ "console" |
    Select-Object -Property UserName,ComputerName,LogonTime

$loggedonuserArray2 = $loggedonuserArray.Clone()

foreach ($logtime in $loggedonuserArray2.LogonTime) {
    $dat += (New-TimeSpan -Start $logtime -End $endDate).TotalHours 

    foreach ($d in $dat) {
        $loggedonuserArray2 | Add-Member -MemberType NoteProperty -Name Duration -Value $d -Force
    }
}
$loggedonUserArray2 | ft -a

Here is an example of the output I am getting:

UserName ComputerName   LogonTime           Duration
-------- ------------   ---------           --------
User1    Computer1      09/16/2015 11:17 AM      0.9
User2    Computer2      09/16/2015 12:08 PM      0.9
User3    Computer3      09/16/2015 6:35 AM       0.9
User4    Computer4      09/16/2015 12:10 PM      0.9
User5    Computer5      09/16/2015 11:42 AM      0.9
User6    Computer6      09/16/2015 11:46 AM      0.9
User7    Computer7      09/16/2015 11:52 AM      0.9
User8    Computer8      09/16/2015 6:53 AM       0.9
User9    Computer9      09/16/2015 12:08 PM      0.9
User10   Computer10     09/16/2015 6:04 AM       0.9
User11   Computer11     09/16/2015 6:57 AM       0.9

Upvotes: 0

Views: 83

Answers (2)

beatcracker
beatcracker

Reputation: 6920

Basically you're setting the same Duration property for each member of the array every time you execute this line:

$loggedonuserArray2 | Add-Member -MemberType NoteProperty -Name Duration -Value $d -Force

So, eventually, at the end of the foreach ($d in $dat) loop, all the members of the $loggedonuserArray2 array will have the Duration property of the last element in the $dat array.

Upvotes: 1

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200313

Looks to me like what you actually want is something like this:

$endDate = (Get-Date -Format g)
$computers = Get-Content "\\<Server>\D$\<Folder1>\<Folder2>\ComputerList.txt"

$loggedonuserArray = .\get-loggedonuser.ps1 -ComputerName $computers |
    Where-Object { $_.SessionName -eq 'console' } |
    Select-Object UserName, ComputerName, LogonTime, 
        @{n='Duration';e={(New-TimeSpan -Start $_.LogonTime -End $endDate).TotalHours}}

Upvotes: 1

Related Questions