Powershell property 'timestamp' cannot be found on this object

Viewed 586

Using Powershell version 5, I am writing a Powershell module, HandleReports.psm1 that has a class, CodeReview. Separately, I am writing a Powershell script, HandleReports.tests.ps1 to test the module. The issue I am facing, is the following error message:

The property 'timestamp' cannot be found on this object. Verify that the 
property exists and can be set.
At C:\MyFolder\HandleReports\HandleReports.psm1
:18 char:5
+     $this.timestamp = $timestamp
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

HandleReports.psm1 has the following class definition:

class CodeReview {
  CodeReview ([string] $timestamp) {
    $this.timestamp = $timestamp
  }
}

HandleReports.tests.ps1 has the following content:

Using module C:\MyFolder\HandleReports\HandleReports.psm1

$MyTimestamp = Get-Date -Format o | ForEach-Object { $_ -replace ":", "." }

$MyCodeReview = [CodeReview]::new($MyTimestamp)

I have tried resolving this issue by:

  • casting the timestamp to string type
  • modifying the class constructor
  • Instantiating the class inside the module itself

Also, comparing to:

https://learningintheopen.org/2020/05/07/powershell-error-the-property-cannot-be-found-on-this-object-verify-that-the-property-exists/

The property 'Name' cannot be found on this object. Verify that the property exists

https://community.spiceworks.com/topic/391316-powershell-custom-object-not-settable

They seem to be the same error, but happening for slightly different reasons.

1 Answers

You must declare the property first:

class CodeReview {
  [string]$Timestamp
  CodeReview ([string]$timestamp) {
    $this.Timestamp = $timestamp
  }
}

Read more about class properties here.

Related