Skip to content

Instantly share code, notes, and snippets.

@andrewmatveychuk
Created February 23, 2024 20:54
Show Gist options
  • Save andrewmatveychuk/f66e81864f2a96c8ff3a772b38cdc009 to your computer and use it in GitHub Desktop.
Save andrewmatveychuk/f66e81864f2a96c8ff3a772b38cdc009 to your computer and use it in GitHub Desktop.
A sample PowerShell class for defining custom log entry objects
enum OperationResultList : byte {
Disabled
Deleted
Detected
}
class LogEntry {
[OperationResultList] $OperationResult
[ValidateNotNullOrEmpty()] [string] $OperationDetails
# Default constructor
LogEntry() { $this.Init(@{}) }
# Convenience constructor from hashtable
LogEntry([hashtable]$Properties) { $this.Init($Properties) }
# Common constructor for title and author
LogEntry([OperationResultList]$OperationResult, [string]$OperationDetails) {
$this.Init(@{OperationResult = $OperationResult; OperationDetails = $OperationDetails })
}
# Shared initializer method
hidden [void] Init([hashtable]$Properties) {
foreach ($Property in $Properties.Keys) {
$this.$Property = $Properties.$Property
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment