Skip to content

Instantly share code, notes, and snippets.

@ninmonkey
Last active March 13, 2024 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ninmonkey/0aedb7b6f0c42db8e393c01d3a6a8d47 to your computer and use it in GitHub Desktop.
Save ninmonkey/0aedb7b6f0c42db8e393c01d3a6a8d47 to your computer and use it in GitHub Desktop.
Pwsh Copy Some Properties Constructor.md

This is for the dbatools discord thread at: https://discord.com/channels/180528040881815552/800214699463409704/1217419649793130649

class RawIncident {
     [int]$IncidentId
     [string]$MailId
     [string]$Status
     [string]$Priority
     [string]$ConfigName
     [string]$SenderAddress
     [string]$SenderName
     [string]$MachineName
     [string]$InstanceName
     [DateTime]$RecievedDate
     [DateTime]$SentDate
     [string]$BodyHTML
     [string]$ObjectCategory
     [string]$ObjectName
     [string]$RawSubject
     [string]$RawContent
     [string]$RawContentType
}

How do you use it? You have a bunch of options

Using the implicit hashtable constructor.

[RawIncident]@{ 
    IncidentId = 234
    BodyHTML = '<h1>hi world</h1>'
}

That default constructor is shorthand for manually assigning properties like this:

$record = [RawIncident]::new()
$record.Incident = 234
$record.BodyHTML = '<h1>hi world</h1>'

For default values, you can use either of these.

[RawIncident]@{}
[RawIncident]::new()

Some of those formats will change if you write your own constructor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment