0

I have an object with a column I need to convert from Unix time to a "human" time. My object looks like this:

PS C:\Windows\system32\WindowsPowerShell\v1.0> $AllAgents.agents[0..2] | Format-Table

last_scanned ip              distro     platform name            uuid                                                  id
------------ --              ------     -------- ----            ----                                                  --
1460167223   192.168.118.101 win-x86-64 WINDOWS  COMPUTER-1      648f8f4f-8afa-029d-424f-fb27a8e345f8e2fdef184343058e 101
1460167223   192.168.118.145 win-x86-64 WINDOWS  COMPUTER-2      0a33a831-fa47-1fdc-2c21-2a079c728a88bcf6186e275a9135 152
1460167223   192.168.118.26  win-x86-64 WINDOWS  COMPUTER-3      738c0d3a-d2d5-447c-c671-b248180c3b3f75efb734be3d547d 359   

The "last_scanned" column is what I need to change. I have the following code:

$Origin = New-Object -Type DateTime -ArgumentList 1970, 1, 1, 0, 0, 0, 0
$AllAgents.agents.last_scanned = $AllAgents.agents.last_scanned | ForEach-Object {
    $_ = $Origin.AddSeconds($_)
    $_
}

Executing this loop results in the following error:

The property 'last_scanned' cannot be found on this object. Verify that the property exists and can be set.
At U:\Powershell\Scripts\Nessus API - Get All Agents From a Group.ps1:55 char:1
+ $AllAgents.agents.last_scanned = $AllAgents.agents.last_scanned | For ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

I'm not sure why PowerShell thinks the last_scanned property doesn't exist because it's clearly there. How can I modify the last_scanned property to a more readable date and put that value back into the object?

1 Answer 1

1

What version of powershell are you using? It looks like you have an array of objects under the .agents property which then each have their own .last_scanned property, version 3+ allows you access child properties of array members like that but version 2 does not, does this work for you?

$Origin = New-Object -Type DateTime -ArgumentList 1970, 1, 1, 0, 0, 0, 0
$AllAgents.agents = $AllAgents.agents | ForEach-Object {
    $_.LastScanned = $Origin.AddSeconds($_.LastScanned)
    $_
} 
Sign up to request clarification or add additional context in comments.

3 Comments

I'm using PS 5.0. I'm trying your suggestion right now and I'll let you know if it works.
that did the trick! You and I basically did the same thing but you were one level higher than I was (AllAgents.agents, whereas I was at AllAgents.agents.last_scanned). Can you explain why your way works but mine didn't?
Ok I thought it was only a limitation with earlier versions but there may be some other reason it's not working for those objects, basically your trying to access the .last_scanned property of $AllAgents.agents but $AllAgents.agents is an array and does not have an .agents property. Doing what you were doing actually does work with most powershell objects so I was guessing you were on an older version but if your using 5 then I can only assume it's something odd about the $AllAgents objects but I don't have much idea what. Hope that helps.

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.